Forum Discussion

Alan2022's avatar
Alan2022
Iron Contributor
May 05, 2022
Solved

Any idea on how to query Get-AzTag using both Name & Resource ID?

Any idea on how to query Get-AzTag using both Name & Resource ID? Thanks.

 

Example. Get-AzTag -Name TagName -ResourceId $vm.Id | Select -ExpandProperty properties

  • Alan2022 

     

    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-AzVM

     

     

    Cheers,

    Lain

4 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Alan2022 

     

    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

    • Alan2022's avatar
      Alan2022
      Iron Contributor
      LainRobertson

      Hi

      Since the VM has 3 tags i just want to filter the 1st tag without doing a loop. Is that even possible?
      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        Alan2022 

         

        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-AzVM

         

         

        Cheers,

        Lain

Resources