VMware Cloud Community
THEL1ZARDKING
Contributor
Contributor

Warning: 'Set-CustomField' cmdlet Obsolete.

Hello All,

I am currently using Set-CustomField cmdlet in my script to set a custom attribute and recieve the following warning only on the first use of the cmdlet.

WARNING: 'Set-CustomField' cmdlet is obsolete. Use 'Set-CustomAttribute'instead.

I examined the Set-CustomAttribute cmd but it appears that it is only for renaming rather than seeting a custom field.  I have also tried many methods to supress the warning, non of which were successful.  The warning seems to be coming from PowerCLI rather than the command itself.  If anyone has any syntax examples on how to coerce Set-CustomAttribute into setting custom fields for how to suppress this type of warning I would very much like to hear it.  Thanks in advance for any posts.

-WarningAction SilentlyContinue

-WarningAction 0

I know these are longshots but I tried anyways.

-ErrorAction Continue

-Confirm:$False

Cheers!

-LzK

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

I think they want you to use the Set-Annotation cmdlet.

Something like this

$vm = Get-VM -Name MyVM

$ca = Get-Annotation -CustomAttribute MyCustomAttributeName -Entity $vm

Set-Annotation -Entity $vm -CustomAttribute $ca.Name -Value MewValue


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

Reply
0 Kudos
THEL1ZARDKING
Contributor
Contributor

Brilliant!  I had a feeling you would come through for me LucD, thanks for the tip.

Reply
0 Kudos
malti
Contributor
Contributor

I have one small issue with the set-annotation cmdlet.  I have several scripts that clear out these custom field values for VMs by calling:

Get-VM -name "SomeVMName" | Set-CustomField -name "SomeCustomField" -value ""

However, the Set-Annotation cmdlet will not let you pass a blank string as the value.  If I specify a value it works just fine, but how can I delete the contents of a custom field for a specific VM/Entity?

Get-VM -name "SomeVMName" | Set-Annotation -customattribute "SomeCustomField" -value ""

Set-Annotation : Cannot validate argument on parameter 'Value'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

I could pass the value as a single space ( -value ' ' ), however, for scripting purposes that's not really the same.  A newly created VM with a blank custom field will be a blank string and one that I've cleared will be a space.  So if VM1 is a new VM with a truely blank custom field called CF1, and VM2 has a blank custom field CF1 that I've cleared this way, the following would evaluate to false, but I need it to evaluate as true:

((get-vm -name VM1 | Get-Annotation -CustomAttribute "CF1").value) -eq ((get-vm -name VM2 | Get-Annotation -CustomAttribute "CF1").value)

If anyone can help me blank out a custom field I'd appreciate it.

Reply
0 Kudos
LucD
Leadership
Leadership

The problem with an empty Value on the Set-Annotation cmdlet is a know flaw.

I'm pretty sure it will be fixed in a future build.

In the meantime keep using the Set-CustomField cmdlet or use the following

$cfMgr = Get-View CustomFieldManager

$key = (Get-VM MyVM).Extensiondata.AvailableField | where {$_.Name -eq "SomeCustomField"}).Key

$cfMgr.SetField($vm.Extensiondata.MoRef,$key,"")


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

Reply
0 Kudos
malti
Contributor
Contributor

OK, that's no problem.  Thank you.

Reply
0 Kudos