When I create a script automation using PowerShell 5.1, I had a problem - in the script job block, the code after the start-up process will not have a chance of execution. Heres a simple reproduction:
Step 1 → Prepare the .cmd file for the Start-Process, the code in callee.cmd:
@echo off echo "Callee is 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 the PowerShell script, the output on the screen:
Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 1 Job1 BackgroundJob Completed True localhost ... Completed
The expected value should be "Failed". Does my code have problems or Im using jobs in the wrong way?
source share