locked
Pass a local variable to a remote session using the invoke-command cmdlet RRS feed

  • Question

  • Hello,

    I'm new to powershell and am trying to learn how to pass a local variable to a command on a remote computer.

    For example, I have a variable called $temp which is defined on my local powershell session. I want to take that variable and be able to use it when i use the invoke-command cmdlet.

    I have read the help but I don't really understand what it means.

    My end goal is to have a script which runs locally and asks the user to input a file path. That file path is then used in a script which is invoked by the invoke-command cmdlet.

    Thanks

    Friday, April 20, 2012 2:15 PM

Answers

  • -argumentlist expects an array, so you could simply do
     
    -argumentlist $item1,$item2,$item3
     
    then access them with $args[0] $args[1] and $args[2]
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Bigteddy Friday, April 20, 2012 3:51 PM
    • Marked as answer by Yan Li_ Tuesday, April 24, 2012 2:00 AM
    Friday, April 20, 2012 3:15 PM

All replies

  • Are you having a particular issue?  There is no special handling needed to pass a variable to a scriptblock.  For example, this code works as expected:

    $var = 'Hello World'
    Invoke-Command -ScriptBlock { Write-Host $var }


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, April 20, 2012 2:29 PM
  • he wants to send it to a remote session (ie wsman)
     
    so you need to use the -argumentlist
     
    $text = "hello world"
     
    invoke-command -computername server1 -scriptblock {"the text is $($args[0])"
    | out-file c:\test.txt} -argumentlist $text
     
    didn’t test but something like that
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Bigteddy Friday, April 20, 2012 3:40 PM
    Friday, April 20, 2012 2:33 PM
  • That looks to be about right. Thanks.

    Can you just explain the part below?

    {"the text is $($args[0])" | out-file c:\test.txt} -argumentlist $text

    I understand the scriptblock however I dont quote get what the $($args[0]) part means. Can you break it down for me so i can understand it better?

    Friday, April 20, 2012 3:00 PM
  • two things there, the use of $args, and sub expressions in strings
     
    "test $(5+3)"
     
    basically anything inside of  $() that’s inside of double quotes will be run
    and the output in its place in the string
     
    the other part is args, a script block is no different than a function
    without a name, so if you don’t specify param() then you can only access
    arguments via the default $args variable...
     
    more on both topics here
     
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, April 20, 2012 3:03 PM
  • Many thanks for your help. If i wanted to pass more than one variables to the remote session, what would need to be changed in that statement?

    also, if i want to add commands into the script block, there shoudl the be put? Before or after the ARGs statement?


    • Edited by Aeropars Friday, April 20, 2012 3:14 PM
    Friday, April 20, 2012 3:13 PM
  • -argumentlist expects an array, so you could simply do
     
    -argumentlist $item1,$item2,$item3
     
    then access them with $args[0] $args[1] and $args[2]
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Bigteddy Friday, April 20, 2012 3:51 PM
    • Marked as answer by Yan Li_ Tuesday, April 24, 2012 2:00 AM
    Friday, April 20, 2012 3:15 PM
  • This worked for me as expected, thanks!  No change was needed, it works as is.
    Friday, March 31, 2017 5:15 PM