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 the 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!