Wonky Start-Process errors after the script has been running a while
I have a PowerShell 2.0 script that works on large file shares from the top down, correcting permissions and such by launching another executable via Start-Process and waiting for the results and exit codes, with code like this:
Start-Process -FilePath $exe -ArgumentList $allargs -Wait -Passthru -NoNewWindow -RedirectStandardOutput $tempoutputfile -RedirectStandardError $temperrorfile;
The script will work great for a few hours, processing thousands of entries, and then eventually it will start giving odd errors like the following:
"Start-Process : Cannot process request because the process (PID) has exited." -- So the process closed so fast that no status was returned?
"Start-Process : This command cannot be executed due to the error: Not enough storage is available to process this command." -- There is more than 20GB free on the file share I'm working on, as well as on the system drive on the 2 different systems I've tested from (not to mention copious free RAM & virtual memory).
Note that I am using the builtin Start-Process cmdlet, not the PSCX one--but incidentally it behaves the same way (since they're both based on .NET's Process class anyway).
Note also that it doesn't look at all like a memory management problem--the PowerShell process peaks at less than 100MB working set early in the process, and never grows beyond that over hours of running.
Anyone have ideas how to stop these errors? Or, how to debug them, for that matter?
-----Solution-----
Thanks to @JPBlanc for pointing towards the following solutions:
1.) To solve the "Not enough storage is available to process this command" errors, I ran the PowerShell script on the local server which hosts the file share (rather than over the CIFS share, which is affected by some memory limitations).
2.) To solve the "Cannot process request because t开发者_StackOverflowhe process (PID) has exited" errors, I had to add additional testing of the HasExited property of the System.Diagnostics.Process object, as follows:
$ps = Start-Process -FilePath $exe -ArgumentList $allargs -Wait -Passthru -NoNewWindow -RedirectStandardOutput $tempoutputfile -RedirectStandardError $temperrorfile;
do {} until ($ps.HasExited); # wait...
$ps.ExitCode; # <-- this is what I really needed from the output
The script is now running error-free!
As you are running your script on a share, this can be caused by the server service running out of process memory, by a process on your system consuming memory handles and not releasing them properly, there are some test advices on this post. By the past I used to have a similar error on an application that was opening a lot of file on a share, and modifying IRPStackSize
registry key seems to help.
----Edited------
As far as I understand "Cannot process request because the process (PID) has exited" is due to the fact that under Start-Process
there is a [Diagnostics.Process]
used and the -Wait parameter is played whithout testing if the hasexited
propertie is true or false. To corect this error, I would instrument my code to discover exactly on which file this appends. For me this error is due to a particular error of your $exe
called with $allargs
.
精彩评论