mhopkins321
mhopkins321

Reputation: 3073

Passing data and executing remote powershell script on remote machine (possibly telnet?)

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

Upvotes: 0

Views: 1493

Answers (1)

SlightlyBent
SlightlyBent

Reputation: 119

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

Upvotes: 2

Related Questions