February 2019
Intermediate to advanced
626 pages
15h 51m
English
As an individual instance is executing asynchronously with BeginInvoke, several may be started. In each case, both the PowerShell object and the IASyncResult object should be preserved:
$jobs = 1..5 | ForEach-Object { $instance = [PowerShell]::Create().AddScript(' Start-Sleep -Seconds (Get-Random -Minimum 10 -Maximum 121) ') [PSCustomObject]@{ Id = $instance.InstanceId Instance = $instance AsyncResult = $instance.BeginInvoke() } | Add-Member State -MemberType ScriptProperty -PassThru -Value { $this.Instance.InvocationStateInfo.State }}
Each job will continue for a random number of seconds and then complete. As each job completes, the State property created by Add-Member will change to reflect that:
PS> $jobs | Select-Object ...
Read now
Unlock full access