Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell remoting via PSSESSION and variables

Status
Not open for further replies.

kmcferrin

MIS
Jul 14, 2003
2,938
US
So I have some code that allows me to open a remote session via PSSession and execute some code in a scriptblock. I can pass local variables to the scriptblock for use in the remote execution via "-Args". That works great. But now I need to retrieve a variable that is created in the scriptblock on the remote host so I can check it's value locally. Sample code below:

Code:
$RemoteHost = "server.fqdn.com"

# This function calls external executables, waits for them to complete and returns the exit code
function Run-RemoteProcess ($cmd, $Parameters) {
    $RemoteSession = New-PSSession -ComputerName $Remotehost
    #Enter-PSSession -session $RemoteSession
    Invoke-Command -Session $RemoteSession -Scriptblock {param($cmd,$Parameters)
        $Process = New-Object System.Diagnostics.Process;
        $Process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo;
        $ExitCode = $false;
        $Process.StartInfo.FileName = $cmd;
        $Process.StartInfo.Arguments = $Parameters;
        $Process.StartInfo.UseShellExecute = $shell;
        $Process.StartInfo.WindowStyle = 1;
        $null = $Process.Start();
        $Process.WaitForExit();
        $Global:ExitCode = $Process.ExitCode;
        $Process.Dispose();
    } -Args $cmd,$Parameters
    #Exit-PSSession
    #Remove-PSSession $RemoteSession
    If ($Process.ExitCode -ne 0) {
        #Log an error
        #Sets performance counter State to Warning
        #$PerfCounterState = new-object 
    }
    Else {
        #Logs success
    }
    #$PerfCounterData.Increment()
    return $ExitCode
}

"Returncode = " + $Returncode
$ReturnCode = Run-RemoteProcess ping.exe "-n 10 192.168.199.199"

I can't seem to get the $Exitcode value to work. I have very similar code that works great as a local function. If I leave the PSSession open after the script completes, I can "Enter-PSSession $RemoteSession" and check the value of $Exitcode, and it has the correct data there. So I know that it is getting set in the remote session to the correct exit code value for the .EXE command that it runs. I just need to get that value back from the remote host somehow.

________________________________________
CompTIA A+, Network+, Server+, Security+
MCTS:Windows 7
MCSE:Security 2003
MCITP:Server Administrator
MCITP:Enterprise Administrator
MCITP:Virtualization Administrator 2008 R2
Certified Quest vWorkspace Administrator
 
I figured it out. I had to make three changes:

Change1:
Code:
Invoke-Command -Session $RemoteSession -Scriptblock

becomes

Code:
$Result = Invoke-Command -Session $RemoteSession -Scriptblock

Change2:
Insert "$ExitCode;" on a new line before the line that says
Code:
$Process.Dispose();

Change3:
Change the line that reads

Code:
return $ExitCode

to read

Code:
return $Result

Apparently using PSSession the $Result variable is assigned the output of the "Invoke-Command" command. In order for $Result to get the exit code you just need to echo the value of $ExitCode to the console in the remote session, then it get's assigned to $Result and can be returned from the function to the main script.


________________________________________
CompTIA A+, Network+, Server+, Security+
MCTS:Windows 7
MCSE:Security 2003
MCITP:Server Administrator
MCITP:Enterprise Administrator
MCITP:Virtualization Administrator 2008 R2
Certified Quest vWorkspace Administrator
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top