Install ADFS on Azure VM step by step

Working with RunSpace in Powershell

Runspace is good a method to run multiple PowerShell jobs without putting an overhead on system as the runspace run jobs under the same process.

In this post I will be talking about how we can create a simple powershell runspace to run the scripts which takes much time in the background and keep our session free for the other work.

You can also go through my other post in this series.

 
Run the below CMD to run the RunSpace Instance.

$Runspace=[runspacefactory]::CreateRunspace()


Now lets create a new Job Under the Runspace instance we created above

$Job=[System.Management.Automation.PowerShell]::Create()


Now Lets open the RunSpace.

$Runspace.Open()


Now when our Runspace is open, lets see what methods are available with the PowerShell.


We can add our script using the below method.

[Void]$powershell.AddScript{(get-date)}

Note: If your script required parameters you can use the addArgument() method, I will talk about it in my Next Post.


$AsyncObject=$Powershell.BeginInvoke()
$Data = $Powershell.EndInvoke($AsyncObject)
$Powershell.Dispose()

That’s it for today, Below is the full Sample Code used in this Post.

$Runspace=[runspacefactory]::CreateRunspace()
$Job=[System.Management.Automation.PowerShell]::Create()
$job.runspace=$Runspace
$Runspace.Open()
$scriptBlock={Get-Process}
[void]$job.AddScript($scriptBlock)
$AsyncObject=$job.BeginInvoke()
$Data = $job.EndInvoke($AsyncObject)
$job.Dispose()
$data

Comments