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
I think the best way to handle this, is with a try-catch construct.
Something like this
$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
Thanks LuCD,
Will it assign already exists tag also??
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
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
Oh Ok, thats fine..
Super, LuCD. You are the one who is always helping me on this scripting part.
Thanks a lot......
Great you appreciated your own answer :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oops...!
That was wrong click.....
No problem
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference