Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the Return Value of a ScriptBlock invoked with Powershell's Invoke-Command

My question is very similar to this one, except I'm trying to capture the return code of a ScriptBlock using Invoke-Command (so I can't use the -FilePath option). Here's my code:

Invoke-Command -computername $server {\\fileserver\script.cmd $args} -ArgumentList $args exit $LASTEXITCODE 

The problem is that Invoke-Command doesn't capture the return code of script.cmd, so I have no way of knowing if it failed or not. I need to be able to know if script.cmd failed.

I tried using a New-PSSession as well (which lets me see script.cmd's return code on the remote server) but I can't find any way to pass it back to my calling Powershell script to actually DO anything about the failure.

like image 486
Jay Spang Avatar asked Dec 18 '11 01:12

Jay Spang


People also ask

How does invoke-command work?

The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.

What does Scriptblock do in PowerShell?

In the PowerShell programming language, a script block is a collection of statements or expressions that can be used as a single unit. A script block can accept arguments and return values. A script block returns the output of all the commands in the script block, either as a single object or as an array.

What is invoke expression in PowerShell?

Description. The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression , a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope.

How do I run a PowerShell script from the command line?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.


2 Answers

$remotesession = new-pssession -computername localhost invoke-command -ScriptBlock { cmd /c exit 2} -Session $remotesession $remotelastexitcode = invoke-command -ScriptBlock { $lastexitcode} -Session $remotesession $remotelastexitcode # will return 2 in this example 
  1. Create a new session using new-pssession
  2. Invoke your scripblock in this session
  3. Fetch the lastexitcode from this session
like image 124
jon Z Avatar answered Sep 23 '22 04:09

jon Z


$script = {     # Call exe and combine all output streams so nothing is missed     $output = ping badhostname *>&1      # Save lastexitcode right after call to exe completes     $exitCode = $LASTEXITCODE      # Return the output and the exitcode using a hashtable     New-Object -TypeName PSCustomObject -Property @{Host=$env:computername; Output=$output; ExitCode=$exitCode} }  # Capture the results from the remote computers $results = Invoke-Command -ComputerName host1, host2 -ScriptBlock $script  $results | select Host, Output, ExitCode | Format-List 

Host : HOST1
Output : Ping request could not find host badhostname. Please check the name and try again
ExitCode : 1

Host : HOST2
Output : Ping request could not find host badhostname. Please check the name and try again.
ExitCode : 1

like image 45
Matthew MacFarland Avatar answered Sep 23 '22 04:09

Matthew MacFarland