I'm writing a script where in one part I want to export some settings of the SCVMM VM and set it in another. I have to run it from another machine, which doesn't have SCVMM installed so I have to call our VMM with Invoke-Command
.
Unfortunately, variables I'm using in the code behave unexpectedly (don't want to say wrong, I assume it's by design). When they are used in parameter they don't transfer whole object that's in them, but just the Name
.
$vm01 = Get-VM -Name VM01
$vm02 = Get-VM -Name VM02
$vm01name =$vm01.Name
$vm02name =$vm02.Name
$VMMparam = Invoke-Command –Computername VMM01 –ScriptBlock {
$VMMvm01=Get-SCVirtualMachine -VMMServer "VMM01.pandora.corp" -Name $using:vm01name
$vmcloud = $VMMvm01.Cloud
$vmos = $VMMvm01.OperatingSystem
$vmuserrole = $VMMvm01.UserRole
$vmowner = $VMMvm01.Owner
return $vmcloud,$vmos,$vmuserrole,$vmowner
}
$VMMcloud = $VMMparam[0]
$VMMos = $VMMparam[1]
$VMMuserrole = $VMMparam[2]
$VMMowner = $VMMparam[3]
Invoke-Command -ComputerName VMM01 -ScriptBlock {
if ($using:VMMcloud -eq $null){
Set-SCVirtualMachine -vm $using:vm02name -OperatingSystem $using:VMMos.Name
}
else{
Set-SCVirtualMachine -vm $using:vm02name -Cloud $using:VMMcloud -OperatingSystem $using:VMMos.Name -UserRole $using:VMMuserrole -Owner $using:VMMowner
}
}
This runs well until it's supposed to enter the Cloud object into the -cloud
parameter. It ends in error:
Cannot bind parameter 'Cloud'. Cannot convert the "CLOUD01" value of type "Deserialized.Microsoft.SystemCenter.VirtualMachineManager.Cloud" to type "Microsoft.SystemCenter.VirtualMachineManager.Cloud".
+ CategoryInfo : InvalidArgument: (:) [Set-SCVirtualMachine], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.SetV
MCmdlet
+ PSComputerName : VMM01
If I call just the $using:VMMcloud
variable inside of the Invoke-Command
, it returns correctly. But when it's in parameter, just the Name
value is returned. I tried it with arguments instead of prefixed variables, but same output.
Can you help me?
P.S. This is my first question in here. Hope the formatting is right and the problem described understandably. If not, ask away.