06 November 2020

Invoke-Command and local variables in PowerShell

Just use "using:" before the variable name

$abc= 'some text'
icm servername {'----';echo $abc;'----'}
icm servername {'====';echo $using:abc;'===='}

Here we set the local variable with the name $abc to some value. When using Invoke-Command (alias icm), then first version uses remote version of that variable, which probably is empty and second version kinda drags the local variable to remote session and you can use it there.


No comments:

Post a Comment