i'm trying run program powershell, wait exit, access exitcode, not having luck. don't want use -wait start-process, need processing carry on in background.
here's simplified test script:
cd "c:\windows" # exitcode available when using -wait... write-host "starting notepad -wait - return code available" $process = (start-process -filepath "notepad.exe" -passthru -wait) write-host "process finished return code: " $process.exitcode # exitcode not available when waiting separately write-host "starting notepad without -wait - return code not available" $process = (start-process -filepath "notepad.exe" -passthru) $process.waitforexit() write-host "process exit code should here: " $process.exitcode running script cause notepad started. after closed manually, exit code printed, , start again, without using -wait. no exitcode provided when quit:
starting notepad -wait - return code available process finished return code: 0 starting notepad without -wait - return code not available process exit code should here: i need able perform additional processing between starting program , waiting quit, can't make use of -wait. idea how can , still have access .exitcode property process?
two things think...
- create system.diagnostics.process object manually , bypass start-process
- run executable in background job (only non-interactive processes!)
here's how either:
$pinfo = new-object system.diagnostics.processstartinfo $pinfo.filename = "notepad.exe" $pinfo.redirectstandarderror = $true $pinfo.redirectstandardoutput = $true $pinfo.useshellexecute = $false $pinfo.arguments = "" $p = new-object system.diagnostics.process $p.startinfo = $pinfo $p.start() | out-null #do other stuff here.... $p.waitforexit() $p.exitcode or
start-job -name dosomething -scriptblock { & ping.exe somehost write-output $lastexitcode } #do other stuff here get-job -name dosomething | wait-job | receive-job
Comments
Post a Comment