Powershell variable is not transfering correctly in the Invoke-Command Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Determine installed PowerShell versionPowerShell says “execution of scripts is disabled on this system.”Passing Powershell variables into a scriptblockUsing PowerShell's Add-Member results in an errorInvoke-Command powershell troubleTrouble using Invoke-Command with ScriptBlock and -ArgumentListInvoke-WebRequest : Cannot bind parameter 'Headers'Passing parameters in a variable PowershellInvoke-Command with parameters as a jobHow to use variable as an argument for command in “Invoke-Expression”
SF book about people trapped in a series of worlds they imagine
How were pictures turned from film to a big picture in a picture frame before digital scanning?
How does light 'choose' between wave and particle behaviour?
Is there hard evidence that the grant peer review system performs significantly better than random?
Is grep documentation about ignoring case wrong, since it doesn't ignore case in filenames?
Is there a kind of relay only consumes power when switching?
Why aren't air breathing engines used as small first stages?
NumericArray versus PackedArray in MMA12
Localisation of Category
Maximum summed subsequences with non-adjacent items
What does it mean that physics no longer uses mechanical models to describe phenomena?
Does the Weapon Master feat grant you a fighting style?
How would a mousetrap for use in space work?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
Is it possible for SQL statements to execute concurrently within a single session in SQL Server?
Why is Nikon 1.4g better when Nikon 1.8g is sharper?
What is a fractional matching?
Should I use a zero-interest credit card for a large one-time purchase?
Selecting user stories during sprint planning
Project Euler #1 in C++
What do you call the main part of a joke?
A term for a woman complaining about things/begging in a cute/childish way
Combinatorics problem on counting.
What is the difference between globalisation and imperialism?
Powershell variable is not transfering correctly in the Invoke-Command
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Determine installed PowerShell versionPowerShell says “execution of scripts is disabled on this system.”Passing Powershell variables into a scriptblockUsing PowerShell's Add-Member results in an errorInvoke-Command powershell troubleTrouble using Invoke-Command with ScriptBlock and -ArgumentListInvoke-WebRequest : Cannot bind parameter 'Headers'Passing parameters in a variable PowershellInvoke-Command with parameters as a jobHow to use variable as an argument for command in “Invoke-Expression”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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.
powershell invoke-command
add a comment |
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.
powershell invoke-command
1
Objects from remote sessions are deserialized; they're not "live". I don't understand why you're callingInvoke-Commandtwice on the same device. I would suggest creating aPSSessionto yourVMM01and import the module you need from it into your local session:Import-Module -Name vmm-module-name -PSSession $session
– TheIncorrigible1
Mar 8 at 20:46
There is a lot of going in between these twoInvoke-Command, so they can't be just one. I'll try your solution withPSSessionand importing the module, didn't know it's possible. Thanks!
– Niaz
Mar 9 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 at 20:46
add a comment |
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.
powershell invoke-command
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.
powershell invoke-command
powershell invoke-command
asked Mar 8 at 20:17
NiazNiaz
61
61
1
Objects from remote sessions are deserialized; they're not "live". I don't understand why you're callingInvoke-Commandtwice on the same device. I would suggest creating aPSSessionto yourVMM01and import the module you need from it into your local session:Import-Module -Name vmm-module-name -PSSession $session
– TheIncorrigible1
Mar 8 at 20:46
There is a lot of going in between these twoInvoke-Command, so they can't be just one. I'll try your solution withPSSessionand importing the module, didn't know it's possible. Thanks!
– Niaz
Mar 9 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 at 20:46
add a comment |
1
Objects from remote sessions are deserialized; they're not "live". I don't understand why you're callingInvoke-Commandtwice on the same device. I would suggest creating aPSSessionto yourVMM01and import the module you need from it into your local session:Import-Module -Name vmm-module-name -PSSession $session
– TheIncorrigible1
Mar 8 at 20:46
There is a lot of going in between these twoInvoke-Command, so they can't be just one. I'll try your solution withPSSessionand importing the module, didn't know it's possible. Thanks!
– Niaz
Mar 9 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 at 20:46
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– TheIncorrigible1
Mar 8 at 20:46
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– TheIncorrigible1
Mar 8 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 9 at 21:26
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 9 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 at 20:46
I tried TheIncorrigible1's method and it ends up the same. Is there a way to serialize the deserialized objects?
– Niaz
Mar 10 at 20:46
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070433%2fpowershell-variable-is-not-transfering-correctly-in-the-invoke-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070433%2fpowershell-variable-is-not-transfering-correctly-in-the-invoke-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Objects from remote sessions are deserialized; they're not "live". I don't understand why you're calling
Invoke-Commandtwice on the same device. I would suggest creating aPSSessionto yourVMM01and import the module you need from it into your local session:Import-Module -Name vmm-module-name -PSSession $session– TheIncorrigible1
Mar 8 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 withPSSessionand importing the module, didn't know it's possible. Thanks!– Niaz
Mar 9 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 at 20:46