powershell - Code in Job's Script Block after Start-Process Does not Execute -
when create automation script powershell 5.1, got issue – in script block of job, code after start-process not chance execute. here’s simple repro:
step 1 >> prepare .cmd file start-process, code in callee.cmd is:
@echo off echo "callee executing ..." exit /b 0 step 2 >> prepare powershell code,
$scriptblock = { $res = start-process -filepath "cmd.exe" -wait -passthru -nonewwindow -argumentlist "/c .\callee.cmd" throw "error!" } $job = start-job -scriptblock $scriptblock wait-job $job receive-job $job write-host($job.state) step 3 >> run powershell script, output on screen is:
id name psjobtypename state hasmoredata location command -- ---- ------------- ----- ----------- -------- ------- 1 job1 backgroundjob completed true localhost ... completed the expected value should “failed”. code have problem or i’m using jobs in wrong way?
start-job run job in separate powershell process in so-called server mode. in mode powershell job process use standard input , output streams exchange messages parent process.
-nonewwindow parameter of start-process cmdlet instruct connect spawned console child process standard streams of parent.
thus, using start-process -nonewwindow inside of powershell job, connect spawned cmd.exe process same streams, powershell job process use exchange messages own parent process.
now, when spawned cmd.exe write standard output stream, disturb normal message exchange between powershell job process , parent, leads unexpected behavior.
Comments
Post a Comment