PowerCLI

 View Only
Expand all | Collapse all

Code to push Folder tags down to VMs

  • 1.  Code to push Folder tags down to VMs

    Posted May 09, 2022 08:54 PM

    I have a rather large vCenter environment that I am tasked with aligning it with our Azure tagging policy.  I have multiple tags assigned to each folder in my environment, no small feat because I did that manually through the web portal.  So now the thing is I need the tags assigned to these folders to also be assigned to the VMs inside that folder.  

    I'd prefer to do this through PowerCLI, but I'm open to suggestions.



  • 2.  RE: Code to push Folder tags down to VMs

    Posted May 09, 2022 09:01 PM

    Basically, you would need the Get-Folder cmdlet, the Get-TagAssignment and the New-TagAssigmnet cmdets.

    But you will need to provide some more info.
    Are there already Tags on some VMs?
    Do these need to be removed when not present in any of the Folders in the tree?
    Is this to be done for all VM folders in your environment, or only a selected set of folders?
    If there are nested folders, do the VMs inherit the tags from all Folders in the tree?
    Are the same Tags present in multiple Categories?



  • 3.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 12:12 PM

    There are no tags on the VMs as of now

    I'm looking to do this with all folders in the environment

    There are nested folders, but the tags I need to push are assigned to the lowest folder

    I have 2 tag categories that I'm trying to push down

    No duplicate tags in different categories.



  • 4.  RE: Code to push Folder tags down to VMs
    Best Answer

    Posted May 10, 2022 12:49 PM

    Something like this should get you started - this one assumes all VMs are in tagged folders but won't assign tags if there are none found from your target categories

     

    #Array containing target categories
    $categories = "Cat 1","Cat 2"
    
    #Get all VMs
    $vmlist = Get-VM
    
    #Loop and assign Tags
    ForEach ($vm in $vmlist){
    
        #get tags matching the category
        $tags = (Get-Folder -Id $vm.FolderId | Get-TagAssignment).Tag | Where {$_.Category -in $categories}
    
        #check if tags are present and assign tags to vm
        If ($tags) {New-TagAssignment -Entity $vm -Tag $tags}
    }

     

    EDIT - corrected mistake in the "If" statement



  • 5.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 01:30 PM

    Getting this error when I attempt to run the code

     

    + If $tags {New-TagAssignment -Entity $vm -Tag $tags}
    + ~
    Missing '(' after 'If' in if statement.
    At line:1 char:13
    + If $tags {New-TagAssignment -Entity $vm -Tag $tags}
    + ~
    Unexpected token '{' in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement



  • 6.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 01:44 PM

    sorry about that - typo on my part should read:
    If ($Tags){New-TagAssignment -Entity $vm -Tag $tags}



  • 7.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 01:48 PM

    And now it's this

    Get-TagAssignment : 5/10/2022 9:46:58 AM Get-TagAssignment com.vmware.vapi.std.errors.operation_not_found {'messages': [com.vmware.vapi.std.localizable_message {'id': vapi.method.input.invalid.interface, 'default_message': Cannot find service
    'com.vmware.cis.data.svc.resource_model'., 'args': [com.vmware.cis.data.svc.resource_model], 'params': , 'localized':}], 'data': , 'error_type': OPERATION_NOT_FOUND}
    At line:11 char:44
    + $tags = (Get-Folder -Id $vm.FolderId | Get-TagAssignment).Tag | W ...
    + ~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-TagAssignment], CisException
    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Impl.V1.Service.Tagging.Cis.TaggingServiceCisImpl.GetTagAssignment.Error,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTagAssignment



  • 8.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 02:54 PM

    What are you vcenter, powershell and powercli versions?



  • 9.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 03:04 PM

    vSphere Client version 7.0.3.00300

    Powershell 5.1

    VMware PowerCLI 12.0.0 build 15947286



  • 10.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 07:32 PM

    I'm suspecting possibly a problem with the PowerCLI version.

    I'd run powershell as admin, then do an 

    Update-Module VMware.Powercli -Force

    And try again after it's been updated



  • 11.  RE: Code to push Folder tags down to VMs

    Posted May 11, 2022 12:52 PM

    That did it!  The script is running and tagging all my VMs after the PowerCLI update.  Thank you for all your help!



  • 12.  RE: Code to push Folder tags down to VMs

    Posted May 10, 2022 08:19 PM

    This is a known issue with vSphere 7.0.3, upgrade your PowerCLI version.
    See Re: get-tagassignment error after vcenter upgrade - VMware Technology Network VMTN