Windows PowerShell Home
PowerShell.exe can hang if STDIN is redirected
7/1/2010 5:52:56 PM
User(s) can reproduce this bug
The Microsoft.PowerShell.ConsoleHost class constructs a M.PS.WrappedDeserializer passing the STDIN TextReader as one of the parameters. By default, the WrappedDeserializer will call ReadLine() on this STDIN TextReader and wait indefinitely, effectively hanging PowerShell and the calling process.
How often does this happen?
Have you seen this problem before in this product?
I don't know if this issue existed previously
1. Create a simple PowerShell script.
2. Create a batch file which calls the PowerShell script via:
powershell.exe -noninteractive -file <path to ps1 file>
3. Create a C# console application that runs the batch file using ProcessStartInfo with the RedirectStandardInput property set to true.
The batch file and the console app should exit successfully when the PowerShell script completes. Instead PowerShell hangs waiting for input on the redirected STDIN stream and the cmd.exe hosting the batch file and the C# application will both wait indefinitely.
to post a comment.
Please enter a comment.
on 11/17/2012 at 8:34 AM
With this workaround you don't have to use use undocumented features:
powershell.exe -file ScriptFile.ps < CON
on 11/21/2010 at 12:40 PM
I have the same problem when executing PowerShell from MSDeploy:
Using the suggested "-inputformat none" seems to resolve the problem.
to post a workaround.
Please enter a workaround.
Patrick R Nielsen
on 6/3/2013 at 11:04 AM
If you upgrade to PowerShell V3 the process works correctly from my testing. My not be an option for everyone but seems good option if you have a lot of legacy script wrappers.
on 11/17/2012 at 8:32 AM
InputFormat None is undocumented while piping (|) workaround won't allow passing arguments to PS script file.
This workaround addresses both of these issues:
powershell.exe -file <path to ps1 file> -args < CON
on 6/21/2011 at 2:24 PM
to work around this bug:
type yourinputfile.ps1 | Powershell -Command ^-
on 7/1/2010 at 5:55 PM
Passing "none" to the "inputformat" parameter of powershell.exe causes the WrappedDeserializer to skip the ReadLine() call on the redirected STDIN. For example:
powershell.exe -inputformat none -file <path to ps1 file>
However, "none" is not documented as a valid value for the "inputformat" parameter and one would have expected the "noninteractive" parameter switch to achieve a similar result.
© 2013 Microsoft