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

Reply
0 Kudos
27 Replies
Vel_VMware
Enthusiast
Enthusiast

Adding few more,

I just want to change only if the value set to 'Yes'

Example: 'Backup = Yes' , It needs to change 'No' or blank. If any value specified other than 'Yes' or blank its shouldn't change anything...

Reply
0 Kudos
LucD
Leadership
Leadership

Something like this?

Note: remove the WhatIf if you are sure the script is changing the correct custom attributes.

$ca = Get-CustomAttribute -TargetType VirtualMachine

Import-Csv -Path .\vmnames.csv -UseCulture |

ForEach-Object -Process {

    $vm = Get-VM -Name $_.Name


    $ca | ForEach-Object -Process {

        Get-Annotation -Entity $vm -CustomAttribute $_ | where{$_.Value -eq 'Yes'} |

        ForEach-Object -Process {

            Set-Annotation -Entity $vm -CustomAttribute $_.Name -Value '' -WhatIf

        }

    }

}


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Thank you LucD..

I have one more query. I created a small script to set Tags in vCenter 6.5 using powerCLI. But getting some error which I could fix it. Can you help me on this please.

$Data = Import-csv T:\Script\Script_Tags\Server_Tags_VDC3.csv

Foreach($Row in $Data)

{

#Variable Declaration

$VmHost = $Row.ServerName

$Tag=$Row.NewTag

$vm = Get-VM -Name $VmHost

New-TagAssignment -Tag $Tag -Entity $vm

}

and getting below error

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or

empty. Provide an argument that is not null or empty, and then try the command

again.

At T:\script\Script_Tags\Script_Tags_VDC3.ps1:7 char:20

+ $vm = Get-VM -Name $VmHost

+                    ~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid

   ationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

   ation.ViCore.Cmdlets.Commands.GetVM

New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.

At T:\script\Script_Tags\Script_Tags_VDC3.ps1:8 char:24

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

+                        ~~~~

    + CategoryInfo          : InvalidData: (:) [New-TagAssignment], ParameterB

   indingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,V

   Mware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

Reply
0 Kudos
LucD
Leadership
Leadership

Could there be an empty line in your CSV file?


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

why its pointing in $VM and $TAG? I checked it.. I dont see any space in this.

Reply
0 Kudos
LucD
Leadership
Leadership

The 1st error states that $vmhost is empty
Which seems to indicate the ServerName in the CSV is empty.
That could be a missing value in a row in the CSV or an empty line in the CSV (perhaps at the end of the CSV)


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

I have checked clearly, I couldn't find any empty spaces.

Can you check the script is anything problem in the script.

Reply
0 Kudos
LucD
Leadership
Leadership

Without the CSV file I only have the error message to analyse.


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Can you create the CSV file and give it to me. I will check with that..

Reply
0 Kudos
LucD
Leadership
Leadership

From your last script, I would deduce that the layout of the CSV should something like this

ServerName,NewTag

vm1,tag1

vm2,tag2


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi,

I have created the CSV as you have given me. Still I am getting the same error.. Can you help me to create the script in your style of format and share it to me..

Reply
0 Kudos
LucD
Leadership
Leadership

I think I already did that, see Re: Editing custom field value for VM's using PowerCLI in vCenter 5.5


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

I am bit confused here. Thats for adding/Editing annotation right. Here I need to add tags in vCenter 6.5. Isn't both are different?

Reply
0 Kudos
LucD
Leadership
Leadership

That is correct, the question about tags is an entirely new question.


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

somehow with your help I have made it work. Its working fine now..

Thanks a lot LucD... I may open another thread for different scripting in next few hours. Please help in that also.

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

$csv = Import-csv T:\Server_Tags_VDC3.csv -delimiter ";"

$csv | foreach {

$vm = $_.ServerName

$Bak = $_.TagBackup

$Code = $_.TagCode

$Envirn = $_.TagEnvironment

New-TagAssignment -Entity $vm -Tag $Bak

New-TagAssignment -Tag $AppCode -Entity $vm

New-TagAssignment -Tag $Envirn -Entity $Vm

}

Hope you can understande the above script. In this script, tagBackup and TanEnvironment is already created in vCenter by default so, it can be assign directly using New-TagAssignment.

But, the TagCode  it would be changing, code for few VM's are already there so it can be assigned. I just want to add condition, if TagCode for few VM's which are not available, condition has to identify and create the tags under specific category, assign it to respective VM's.

Thanks in advance.

Reply
0 Kudos
LucD
Leadership
Leadership

Under which TagCategory should these Tags exist or be created?


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

Few tags are exist that can be assigned, tags which are not exist need to create under the TagCategory and get it assigned.

Thanks in advance

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Something I tried with below,

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

$csv | foreach {

$vm = $_.ServerName

$Bak = $_.TagBackup

$Envirn = $_.TagEnvironment

New-TagAssignment -Tag $Bak -Entity $vm

New-TagAssignment -Tag $Envirn -Entity $Vm

$AppCode = $_.TagAppCode

$CodeChk = Get-tag | Select Name

If ($AppCode -match $CodeChk)

{

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

  New-TagAssignment -Tag $AppTag -Entity $vm

  Write-Host "Tag newly created and assigned" 

}

  else

   {

   New-TagAssignment -Tag $AppCode -Entity $vm

   Write-Host "Already tag created and only assigned"

   }

}

But, doesn't work completely..

Reply
0 Kudos