0

I am attempting to run a executable with arguments from a remote computer in PowerShell, and while I have gotten the PowerShell command to work, it is not properly handling errors that the executable throws.

So the below script executes as expected as long as the file $filename points to exists, but the script will also appear to execute successfully if the file does not, which leads to problems in the scheduler I am trying to tie this into. My expectation would be that PowerShell throws an error such as "File not found", but that is not happening. So is there anything I can add to this script to make that behavior happen?

$ErrorActionPreference = "stop"
$rSession = New-PSSession -ComputerName ServerName
$fileName = c:\file.txt
Invoke-Command -ScriptBlock {Start-Process -FilePath "D:\CAMRA\PFX\PFLNS.EXE" -ArgumentList " /SD:\CAMRA\CAMINI\Z_PRODNP_SQL_TEST.INI GLSWEEP 50 {DN}~$fileName~{T}" -Wait} -Session $rSession
Remove-PSSession $rSession

It is also possible that I am not calling the .exe correctly through PowerShell to do what I am looking to, so I would be open to any suggestions on how I cam modify this script.

Thanks, Phil

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait](http://stackoverflow.com/questions/10262231/obtaining-exitcode-using-start-process-and-waitforexit-instead-of-wait) – jveazey Jan 18 '17 at 22:28

2 Answers2

1

I think that this is something like what you want.

First off, I broke off the command into a separate script block so that we could add in some extra code to catch the bad exit code.

Second, in order to capture the exit code, your Start-Process needs to have the -PassThru parameter. Once we can capture the exit code, we will evaluate it, and if it is not 0 (good) throw an error that will crash the higher up Invoke-Command.

Third, we have to use the special $? variable to catch whether or not the previous command (Invoke-Command) was successful, and if it was unsuccessful, it will throw an error, which could be caught by the scheduling program.

$ErrorActionPreference = "stop"
$rSession = New-PSSession -ComputerName ServerName
$arg = "c:\file.txt"

$ScriptBlock = {
     param ([String]$fileName)
     $output = Start-Process -FilePath "D:\CAMRA\PFX\PFLNS.EXE" -ArgumentList " /SD:\CAMRA\CAMINI\Z_PRODNP_SQL_TEST.INI GLSWEEP 50 {DN}~$fileName~{T}" -Wait -PassThru
     if ($output.ExitCode -ne 0) {
          throw $output.ExitCode
     }  
}

Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arg -Session $rSession
if (!$?) {
     #Failure Throw an error code
     throw 123
}

Remove-PSSession $rSession
HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • This seems to work for what I am looking to do. I seem to be getting a little confused on the variable assignment in powershell though. Epically as to how the $fileName variable is getting a assigned a value. I am also not quite sure how the $output variable is working, but I am sure I can research that on my own. – Philip Karpowich Jan 19 '17 at 14:23
0

Are you passing the argument to the script block? Replace $FileName with $($Args[0]) and add -ArgumentList $FileName to the parameters for Invoke-Command.

... GLSWEEP 50 {DN}~$($args[0])~{T}" -Wait} -ArgumentList $FileName -Session $rSession
Shawn Esterman
  • 2,292
  • 1
  • 10
  • 15