retrive variable value inside ps1 script

This topic contains 3 replies, has 3 voices, and was last updated by  Bin Ary 2 years, 7 months ago.

  • Author
  • #30408

    Bin Ary

    i have a script that run from a source computer:
    $myip = read-host "enter remote ip"
    # file on remote computer
    function test {
    & 'c:\test\test.ps1'
    Invoke-Command -ComputerName $myip -scriptblock ${function:test} -ArgumentList $myip

    #on remote computer i have c:\test\test.ps1 with these lines
    param ($ip)
    Write-Host "you are on:$ip"
    #do task

    the problem is that i cannot retrieve $ip, the result on my source station is:
    you are on:

    thank you

  • #30411


    I'm sure you'll get much more elegant answers, but you could always have the remote pc write to file, and the source get-content that file then delete it, or something similar.

  • #30419

    Rob Simmers

    After you pass -ArgumentList $myIP, you need to reference it in your function as $using:myIP. See about_Remote_Variables

    Additionally, there is a free eBook under Resource "Secrets of Remoting" that would provide examples for you.

  • #30450

    Bin Ary

    thank you

You must be logged in to reply to this topic.