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
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
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
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> 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> 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> date;$vm | Set-CustomField -Name $CustomVarName -Value $CustomVarValue;date
Sun Apr 26 15:05:03 EDT 2009
Name PowerState Num CPUs Memory (MB)
-
-
-
-
<machinename> PoweredOn 2 4096
Sun Apr 26 15:07:28 EDT 2009
C:\Program Files\VMware\Infrastructure\VIToolkitForWindows>
this is same using verbose (doesn't say much).
C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> $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)
-
-
-
-
<machinename> PoweredOn 2 4096
VERBOSE: 4/26/2009 3:21:21 PM Set-CustomField Finished execution
C:\Program Files\VMware\Infrastructure\VIToolkitForWindows>
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
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).