VMware Cloud Community
Grzesiekk
Expert
Expert

upgradeVM_task method question

Hello,

$myvmid=(get-vm 'myvm').id

(get-view -id $myvmid).UpgradeVM_Task('vmx-08'

This 2 lines will upgrade virtual hardware in vm to version 8. it works, i tested it. But when i was writing it first, i wrote it like this:

$myvmid=(get-vm 'myvm').id

(get-view -id $myvmid).UpgradeVM_Task()

Task was created , no error in powercli console. But error was displayed in VC, saying that this is impossible.

I read the guide about thie upgradeVM_task and it says:

Parameters

NameTypeDescription
_thisManagedObjectReferenceA reference to the                                VirtualMachine used to make the method call.
version*xsd:stringIf specified, upgrade to that specified version. If not specified,                 upgrade to the most current virtual hardware supported on the host.

So the parameter version is not needed becase of the " * " next to property version. It should assume vmx-08 by defalut as this is the newest version(esxi 5). This parameter version from what it says  does not require $null , i can ommit it right ? OR this is another example that i should use get_type,get_method,invoke and somehow push $null to it ?

--- @blog https://grzegorzkulikowski.info
0 Kudos
2 Replies
Grzesiekk
Expert
Expert

Right....

$id=(get-vm 'myvm').id

$vmview=get-view -id $id

$param = @($null)

$vmview.gettype().GetMethod("UpgradeVM_Task").Invoke($vmview,$param)

.... 20 minutes of trying and finally did it Smiley Wink

#13:52:59> $vmview.gettype().GetMethod("UpgradeVM_Task").GetParameters()


ParameterType   : System.String
Name            : version
DefaultValue    :
RawDefaultValue :
Position        : 0
Attributes      : None
Member          : VMware.Vim.ManagedObjectReference UpgradeVM_Task(System.String)  -> 1 parameter

But this 1 parameter should be in array , so i i wanted to give $null, it should be $param=@($null)

as per

Invoke(System.Object obj, System.Object[] parameters)

Can others confirm my thinking ?

--- @blog https://grzegorzkulikowski.info
0 Kudos
LucD
Leadership
Leadership

You stumbled upon a known PowerShell problem.

When a parameter for a SDK method is defined as type [string], you can't use a $null value to indicate that the parameter is not present.

The only workaround is the Invoke method which will allow you to pass the method parameters as an array. And that way you can use $null to specify the absence of a parameter.


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

0 Kudos