VMware Cloud Community
Vel_VMware
Enthusiast
Enthusiast

Editing custom field value for VM's using PowerCLI in vCenter 5.5

Hi,

I am looking for PowerCLI script to edit/change the specific custom field value for list of VM's.

VM's list has to take from either CSV format sheet.

Can  anyone help me on the script please.

Thanks and Regards

0 Kudos
27 Replies
LucD
Leadership
Leadership

I think the best way to handle this, is with a try-catch construct.

Something like this

$csv = Import-Csv C:\Server_Tags.csv -delimiter ";"

$csv | ForEach-Object {

    $vm = $_.ServerName

    $Bak = $_.TagBackup

    $Envirn = $_.TagEnvironment

    New-TagAssignment -Tag $Bak -Entity $vm

    New-TagAssignment -Tag $Envirn -Entity $Vm

    $AppCode = $_.TagAppCode

    try{

        $tag = Get-Tag -Name $AppCode -ErrorAction Stop

    }

    catch{

        $tag = New-Tag -Name $AppCode -Category "XXXXXX"

        Write-Host "Tag created"

    }

    New-TagAssignment -Tag $tag -Entity $vm

    Write-Host "Tag assigned"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Vel_VMware
Enthusiast
Enthusiast

Thanks LuCD,

Will it assign already exists tag also??

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

I am getting below error

New-TagAssignment : 15-01-2020 18:57:30    New-TagAssignment        Tag

urn:vmomi:InventoryServiceTag::GLOBAL is ineligible to attach to

urn:vmomi:VirtualMachine: because of a cardinality violation.

At C:\Script_Tags.ps1:30 char:5

+     New-TagAssignment -Tag $tag -Entity $vm

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [New-TagAssignment], CisException

    + FullyQualifiedErrorId : ViCore.Impl.V1.Service.Tagging.Cis.TaggingServiceCisImpl.CheckBatchResult.Error,VMware.

   imAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

0 Kudos
LucD
Leadership
Leadership

That means that you defined a Tag in a Category that has cardinality Single.
In other words, you are trying to assign a 2nd Tag from that same Category to a VM.
In a Category with Cardinality Single, that is not allowed.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Oh Ok, thats fine..

Super, LuCD. You are the one who is always helping me on this scripting part.

Thanks a lot......

0 Kudos
LucD
Leadership
Leadership

Great you appreciated your own answer :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Oops...!

That was wrong click.....

0 Kudos
LucD
Leadership
Leadership

No problem Smiley Happy


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos