Forum Discussion
Any idea on how to query Get-AzTag using both Name & Resource ID?
- May 06, 2022
I'm not sure I understand what it is you're trying to achieve.
Are you trying to retrieve tag statistics, which is what you get from Get-AzTag, or are you trying to find virtual machines by tag name, which is what Get-AzResource/Get-AzVM helps with?
Or is it some other scenario I'm not guessing?
I'm going to make an uninformed guess as to what you're trying to do here and then provide an example. Maybe it will help. Otherwise, let us know exactly what it is you're trying to achieve.
I'm going to provide a command that demonstrates how you can fetch Azure virtual machines by tag name and value (which is what I'm assuming you're trying to do) without having to enumerate all the Azure virtual machines and then inspect them client-side.
Get-AzResource -TagName "MyTagName" -TagValue "SomeTagValue" -ResourceType "Microsoft.Compute/virtualMachines" | Get-AzVMCheers,
Lain
Hi,
What you're trying to do isn't supported by the commandlet's definition, as "-Name" and "-ResourceID" are in opposing parameter sets.
Get-AzTag (Az.Resources) | Microsoft Docs
You can verify as much using the following command:
Get-Command Get-AzTag -ShowCommandInfo | fl -Property Definition, ParameterSets
You have to choose between either -Name or -ResourceId and then use client-side filtering (i.e. using a " | where { <whatever criteria> }" block) against opposite attribute in the result set if necessary.
Cheers,
Lain
Hi
Since the VM has 3 tags i just want to filter the 1st tag without doing a loop. Is that even possible?
- LainRobertsonMay 06, 2022Silver Contributor
I'm not sure I understand what it is you're trying to achieve.
Are you trying to retrieve tag statistics, which is what you get from Get-AzTag, or are you trying to find virtual machines by tag name, which is what Get-AzResource/Get-AzVM helps with?
Or is it some other scenario I'm not guessing?
I'm going to make an uninformed guess as to what you're trying to do here and then provide an example. Maybe it will help. Otherwise, let us know exactly what it is you're trying to achieve.
I'm going to provide a command that demonstrates how you can fetch Azure virtual machines by tag name and value (which is what I'm assuming you're trying to do) without having to enumerate all the Azure virtual machines and then inspect them client-side.
Get-AzResource -TagName "MyTagName" -TagValue "SomeTagValue" -ResourceType "Microsoft.Compute/virtualMachines" | Get-AzVMCheers,
Lain
- Alan2022May 06, 2022Iron ContributorLainRobertson
Hi
Thanks for pointing out the Get-AzResource. This is the result that i want.
Keep up the good job.
$result = (Get-AzResource -Name VM001).Tags
$result['Tag01']