February 2019
Intermediate to advanced
626 pages
15h 51m
English
The Invoke method used with each of the following examples executes the code immediately and synchronously. The BeginInvoke method is used to execute asynchronously, that is, without waiting for the last operation to complete.
Both the PowerShell instance object and the IASyncResult returned by BeginInvoke must be captured. Assigning the values allows continued access to the instances and is required to retrieve output from the commands:
$psInstance = [PowerShell]::Create().AddCommand('Start-Sleep').AddParameter('Seconds', 300)$asyncResult = $psInstance.BeginInvoke()
While the job is running, the InvocationStateInfo property of the PowerShell object will show as Running:
PS> $psInstance.InvocationStateInfo ...
Read now
Unlock full access