60822

Code in Job's Script Block after Start-Process Does not Execute

When I create a automation script with PowerShell 5.1, I got an issue – in a script block of a job, the code after Start-Process will not get chance to execute. Here’s a simple repro:

<strong>Step 1 >> Prepare a .cmd file for Start-Process, the code in callee.cmd is:</strong>

@echo off echo "Callee is executing ..." exit /B 0

<strong>Step 2 >> Prepare the PowerShell code,</strong>

$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)

<strong>Step 3 >> Run the PowerShell script, the output on screen is:</strong>

Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 1 Job1 BackgroundJob Completed True localhost ... Completed

The expected value should be “Failed”. Does my code have problem or I’m using jobs in a wrong way?

Answer1:

Start-Job run job in separate PowerShell process in so-called server mode. In this mode PowerShell job process use standard input and output streams to exchange messages with the parent process.

-NoNewWindow parameter of Start-Process cmdlet instruct it to connect spawned console child process to the standard streams of its parent.

Thus, using Start-Process -NoNewWindow inside of PowerShell job, you connect spawned cmd.exe process to the same streams, which PowerShell job process use to exchange messages with its own parent process.

Now, when spawned cmd.exe write something into its standard output stream, it disturb normal message exchange between PowerShell job process and its parent, which leads to some unexpected behavior.

Recommend

  • Compare a string in one array, with a wildcard string in another
  • Powershell call batch within scriptblock
  • Powershell Start-job invoke function argument with parameter
  • Powershell 2.0 Runtime exception; could not load file or assembly
  • Powershell Remote Invoke-Command Start-Process App Immediately Closes After Launch
  • Is there a way to pass serializable objects to a PowerShell script with start-process?
  • PHP disable_functions in .user.ini file gives “no value” in phpinfo() output
  • Invoke-Command script block not generating output
  • Passing a byte pointer to a C# method via reverse PInvoke
  • Does javascript function know its name
  • Pointers in c++ pass by value/reference
  • Find the associated program to open a file using Java
  • Fancy dynamic list in Android: TableLayout vs ListView
  • Global session variable in express.js route?
  • C++ slows over time reading 70,000 files
  • Change Divider Color Android DatePicker Dialog
  • angular2 http.post() to local json file
  • Translate animation to correct position in Android
  • Unity registration fails after iisreset
  • Paramiko SSHException Channel Closed
  • Loop animation drawable
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • The symbol you provided is not a function
  • Parallel sieve of Eratosthenes - Java Multithreading
  • In Akka, is ActorContext thread safe?
  • Gray line inside cardview :)?
  • Express JS Display Data By ID
  • How to use the resource module to measure the running time of a function?
  • How to resolve permission denied maybe missing internet permission?
  • netsh acl setting (need alternative method - registry settings?)
  • Multiple producers single consumer locking schema
  • aapt.exe'' finished with non-zero exit value 1
  • Ubuntu and bcrypt
  • Using JRuby with Rails 3.2
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Tamper-proof configuration files in .NET?
  • Obtain ObjectIdHex value from mgo query
  • javascript inside java/jsp code
  • Running a C# exe file