Share via


powershell invoke-expression java program get status

Question

Friday, October 13, 2017 3:26 AM

I'm running a jar from powershell using invoke-expression:

$line="c:\temp\my file.txt"
$cmd="java -jar C:\temp\post.jar `"" +  $line + "`""
invoke-expression $cmd

If I test $? it will only give me the status of invoke-expression which will always be a success as the java executeable & post.jar can be found.

Also I've thought of running the java program directly and test $? but sometimes the file name can be very complex and sometimes I put extra options. Also I can use $cmd for logging to a file so I can get the exact command that was run.

How do I get the status of the run of the post.jar program?

All replies (1)

Friday, October 13, 2017 3:44 AM

$file = 'c:\temp\my file.txt'
$p = Start-Process java -ArgumentList '-jar C:\temp\post.jar',$file -Wait -PassThru
$p.ExitCode

\(ツ)_/