VMware Cloud Community
mpverr
Enthusiast
Enthusiast
Jump to solution

Updating custom fields in VC without using Get-VM

Greetings -

I'm trying to determine a means of updating a custom field in Virtual Center via powershell without using Get-VM. I'm able to extract custom field using Get-View

$VMView = Get-View -ViewType virtualmachine -Filter @{"Name"=$input_machinename}

return $VMview.customvalue[7].value

However, every example I have found related to updating/populating custom fields requires the usage of Get-VM. While this is fine and all but everyone knows at this point that Get-VM is something you want to avoid due to the speed issues

http://communities.vmware.com/message/1122166#1122166

This is an example of the code snippet i'm struggling with to try and update a custom field.

#This is the line that takes about 2 minutes

$VMName = Get-VM | where {$_.name -eq "adv0010vpvc01"}

$VMView = $VMName | Get-View

$VMName.CustomFields.values[3] # Prints the custom field name

$VMName.CustomFields.keys[3] # Print the custom field value

$VMView.setCustomValue($VMName.CustomFields.keys[3],"10/10/2000") #Updates the custom field with the value "10/10/2000"

Many Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The ultimate alternative is to use the SDK method to set a custom field.

Can you try this and see if it is faster ?

$input_machinename = <VM-name>
$CustomVarName = <Custom-field-name>
$CustomVarValue = <Custom-field-new-content>

$vm = Get-View -ViewType virtualmachine -Filter @{"Name"=$input_machinename}
$vm.setCustomValue($CustomVarName, $CustomVarValue)


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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect you know the name of the custom field you want to update ?

If yes, you can do something like this

$input_machinename = <VM-name>
$CustomVarName = <Custom-field-name>
$CustomVarValue = <Custom-field-new-content>

$vm = Get-View -ViewType virtualmachine -Filter @{"Name"=$input_machinename} | Get-VIObjectByVIView
$vm | Set-CustomField -Name $CustomVarName -Value $CustomVarValue

Let us know if this is faster then a script that uses Get-VM.


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

Reply
0 Kudos
mpverr
Enthusiast
Enthusiast
Jump to solution

Thanks for the feedback but there is a discrepancy with it that has me perplexed.

This line takes like 5 seconds

$vm = Get-View -ViewType virtualmachine -Filter @{"Name"=$input_machinename} | Get-VIObjectByVIView

However, the set line takes 2 minutes to finish

$vm | Set-CustomField -Name $CustomVarName -Value $CustomVarValue

What is interesting is that when i run the Set-customField line i see the change takes place in about 5-10 seconds but the line doesn't "finish" (come back to the PS&gt; prompt for 2 minutes. what could it be doing during this time?

This is the output:

Took 6 seconds to do this line

C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; date;$vm = Get-View -ViewType virtualmachine -Filter @{"Name"=$input_machinename} | Get-VIObjectByVIView;date

Sun Apr 26 15:04:40 EDT 2009

Sun Apr 26 15:04:46 EDT 2009

Took 2 minutes to do this line, but the actual change to the custom filed happened in about 6 or so seconds [VI Toolkit] C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; date;$vm | Set-CustomField -Name $CustomVarName -Value $CustomVarValue;date

Sun Apr 26 15:05:03 EDT 2009

Name PowerState Num CPUs Memory (MB)

-


-


-


-


&lt;machinename&gt; PoweredOn 2 4096

Sun Apr 26 15:07:28 EDT 2009

C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt;

Reply
0 Kudos
mpverr
Enthusiast
Enthusiast
Jump to solution

this is same using verbose (doesn't say much).

C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt; $vm | Set-CustomField -Name $CustomVarName -Value $Custom

VarValue -verbose

VERBOSE: 4/26/2009 3:19:59 PM Set-CustomField Started execution VERBOSE: Set custom field with the parameters: Entity: adv0010vpvc01, Name: Date Handed To Customer, Value: 30/30/1000,

Name PowerState Num CPUs Memory (MB)

-


-


-


-


&lt;machinename&gt; PoweredOn 2 4096

VERBOSE: 4/26/2009 3:21:21 PM Set-CustomField Finished execution

C:\Program Files\VMware\Infrastructure\VIToolkitForWindows&gt;

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The ultimate alternative is to use the SDK method to set a custom field.

Can you try this and see if it is faster ?

$input_machinename = <VM-name>
$CustomVarName = <Custom-field-name>
$CustomVarValue = <Custom-field-new-content>

$vm = Get-View -ViewType virtualmachine -Filter @{"Name"=$input_machinename}
$vm.setCustomValue($CustomVarName, $CustomVarValue)


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

Reply
0 Kudos
mpverr
Enthusiast
Enthusiast
Jump to solution

That worked thanks!

I'm no programmer so i'm pretty confused as to why these different methods have such differences in speed. I do understand from reading some blogs here that the Get-VM (for example) must read everything from the client to do the filtering so that can take quite a while - but it seems that you would really want all of these filters to be server side for efficiency. Of course, you wouldn't want to pulverize your VC box(es) but otherwise, a lot of these commandlets are rather uselss (no offense to the development team - as they have done a wonderful job).

Reply
0 Kudos