Currently Browsing: devops

Referencing local variables on a remote server using Invoke-Command

Beginning with PowerShell 3.0 it is much easier to reference local variables in scripts executed remotely via the Invoke-Command cmdlet.

On the host you are initiating the Invoke-Command from you simply set any variable you want. On the remote host you reference the variable with the $USING:var-name scope definition.


$ps_script is the path to the PowerShell script to be executed.

[initiating host]

PS C:>$example_var = “hi there”
PS C:> Invoke-Command -ComputerName $server -AsJob -JobName $job_name -FilePath $ps_script -Credential $cred

[contents of $ps_script]

$dereferenced_example_var = $USING:example_var

$dereferenced_example_var contains the string “hi there”

Powered by WordPress | Designed by Elegant Themes