1

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.

Niaz
  • 11
  • 1
  • 1
    Objects from remote sessions are deserialized; they're not "live". I don't understand why you're calling `Invoke-Command` twice on the same device. I would suggest creating a `PSSession` to your `VMM01` and import the module you need from it into your local session: `Import-Module -Name vmm-module-name -PSSession $session` – Maximilian Burszley Mar 08 '19 at 20:46
  • There is a lot of going in between these two `Invoke-Command`, so they can't be just one. I'll try your solution with `PSSession` and importing the module, didn't know it's possible. Thanks! – Niaz Mar 09 '19 at 21:26
  • I tried TheIncorrigible1's method and it ends up the same. Is there a way to serialize the deserialized objects? – Niaz Mar 10 '19 at 20:46

0 Answers0