Relying on the Microsoft .Net Framework, you can use the same Asynchronous technique to invoke multiple PowerShell Script Blocks or functions synchronously in background.
I had designed the PowerShell function Invoke-AsyncScript to do the same job with less efforts.
It can display the results synchronously, or put them into different Global variables or cumulate them into a one, or even merge the properties of different outputs together in one global variable.
You can download the function from here.
Usage Examples:
-
Invoking a Script Block and storing the outputs into a new/existing global variable $MyItems
$GetFoldersList =
{
Param ([Parameter(Mandatory=$True)][String]$Path, [Int] $SecondsToWait = 1)
Sleep -Seconds $SecondsToWait;
Return Get-ChildItem -Path $Path;
}
$par1 = New-Object System.Management.Automation.Runspaces.CommandParameter ("Path", "C:\Windows");
$par2 = New-Object System.Management.Automation.Runspaces.CommandParameter ("SecondsToWait", 5);
Invoke-AsyncScript -ScriptBlock $GetFoldersList -OutputGlobalVariable "MyItems" -Parameters $par1, $par2 | Out-Null;
$MyItems; #The value should be assigned to the variable in 5 seconds.
-
Executing a Function and storing the outputs into a new/existing global variable $MyItems
Function Get-FoldersList
{
Param ([Parameter(Mandatory=$True)][String]$Path, [Int] $SecondsToWait = 1)
Sleep -Seconds $SecondsToWait;
Return Get-ChildItem -Path $Path;
}
$par1 = New-Object System.Management.Automation.Runspaces.CommandParameter ("Path", "C:\Windows");
$par2 = New-Object System.Management.Automation.Runspaces.CommandParameter ("SecondsToWait", 5);
Invoke-AsyncScript -ScriptBlock ${Function:Get-FoldersList} -OutputGlobalVariable "MyItems" -Parameters $par1, $par2 | Out-Null;
$MyItems; #The value should be assigned to the variable in 5 seconds.
-
Executing a Function and storing the outputs into the returned IAsyncResult
Function Get-FoldersList
{
Param ([Parameter(Mandatory=$True)][String]$Path, [Int] $SecondsToWait = 1)
Sleep -Seconds $SecondsToWait;
Return Get-ChildItem -Path $Path;
}
$par1 = New-Object System.Management.Automation.Runspaces.CommandParameter ("Path", "C:\Windows");
$par2 = New-Object System.Management.Automation.Runspaces.CommandParameter ("SecondsToWait", 5);
$syncResult = Invoke-AsyncScript -ScriptBlock ${Function:Get-FoldersList} -ReturnCallBack -Parameters $par1, $par2;
$syncResult.IsCompleted; #You can check if the execution is completed.
$syncResult.Output; #The value should be assigned to that property after 5 seconds.
-
Executing a Function and storing the outputs into the returned IAsyncResult, plus displaying the results automatically into the Shell Window once the execution is completed
Function Get-FoldersList
{
Param ([Parameter(Mandatory=$True)][String]$Path, [Int] $SecondsToWait = 1)
Sleep -Seconds $SecondsToWait;
Return Get-ChildItem -Path $Path;
}
$par1 = New-Object System.Management.Automation.Runspaces.CommandParameter ("Path", "C:\Windows");
$par2 = New-Object System.Management.Automation.Runspaces.CommandParameter ("SecondsToWait", 5);
$syncResult = Invoke-AsyncScript -ScriptBlock ${Function:Get-FoldersList} -ReturnCallBack -WriteOutput -Parameters $par1, $par2;