0

I have a script on Machine A that checks to see if a port is open on Machine B. What I'm looking to do is somehow pass to Machine B which port Machine A will be testing. I've looked into things such as invoke-command as well as telnet but I'm curious what you guys think the best way to communicate between these two servers. same network, different domain. I do have admin access on both boxes

1 Answer 1

2

If you are simply looking to "pass in" a variable with value ($testport) to a remote session, then you can accomplish that like this:

invoke-command -session $session -scriptblock {param($testport)
  #create variable and assign it a value
  set-variable -name:"testport" -value:$testport -force  -erroraction silentlycontinue;
}

To send many variables at once (for example to initialize a remote session with all Global variables from parent session), gather them into a $hash and use something like this:

invoke-command -session $session -scriptblock {param($hash, $setAsGlobal)
#create global variable and assign value for each hash table entry
if ($setAsGlobal -eq $true) {
  foreach ($key in $hash.keys) {
      set-variable -name:$key -value:$hash.$key -scope:'global' -force  -erroraction silentlycontinue;
  }
} else {
  foreach ($key in $hash.keys) {
    set-variable -name:$key -value:$hash.$key -force  -erroraction silentlycontinue;
  }
}

To get a variable back from a remote session and assign to $result:

 $varname = "testport";
 $result = invoke-command -session $session -scriptblock {param($varname)
    get-variable -name $varname -valueonly
 } -argumentlist $varname
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.