On Feb 21, 11:05 pm, Scott Bass <
sas_l_...@yahoo.com.au> wrote:
> (Hoping to get an answer here...)
>
> Summary:
> Seehttp://
social.technet.microsoft.com/Forums/en-AU/winserverpowershell/....
Hello,
It seems to me that Start-Job, Get-Job and Receive Job provide the
control of the (background) processes that you are looking for.
"Exit code" is somewhat outmoded in the PS environment. If it's
really needed, then you will have to wrap the exe calls in PS scripts
or functions that would capture it and return it to the calling shell;
then run these scripts via Start-Job.
For exits that really are errors, you could create your own
ErrorDetail object, onto which you could add an 'ExitCode' property,
and return that. This then would be available in the calling shell
via the $error object reference.
It's pretty hard to script any complex tasks in PS when you're just
passing back a bit of text like the value of $LASTEXITCODE. Another
option might be:
$job = Get-Job -Name "ReallyImportantJob" | Select-Object
Name,Id,State,ExitCode
while($job.State -eq "Running"){
$drinkmorecoffee = "yes"
}
# after $job.State = "Failed" or $job.State = "Completed"
$job.ExitCode = $LASTEXITCODE
There is no "ExitCode" property on the job object returned by Get-Job,
but happily, Select-Object will silently add it to the new object
created for the $job reference.
(Note that in PS, it is possible to modify directly object types, via
the Adaptive Type System. Thus, on the system on which you run your
scripts, you could modify the System.Management.Automation.Job object
to include a custom property, ExitCode.)
Your wrapper scripts return the $job objects instead of just the exit
code.
If you collect all these $job objects (in another object, presumably),
you now can process them according to your exit codes using the
standard PS pipelines. e.g.,
$joblist.JOBS = @($job,$job,$job,$job)
$joblist.JOBS | %{ if($_.ExitCode -eq 0 {#do something here}
elseif($_.ExitCode -eq 20){#do something different here}}
Yes, polling a job state in a while loop can be very expensive but
it's uncomplicated and easy for a half-dozen or so jobs.
Thanks.
mp