Skip to content

Instantly share code, notes, and snippets.

Created November 24, 2014 08:25
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# Basic Script Template for Runspace Jobs against an array of computers
# This is just an example to show the basic layout of a script using the Runspace functions
# If you are creating a script to be run in production, please add logging and error handling
# Also consider writing it to support parameters for input instead of hardcoding it like in this example
#region verify that runspace functions are present
$missingFunctions = $false
$functions = (
foreach ($function in $functions) {
try {
$thisFunction = Get-Item -LiteralPath "function:$function" -ErrorAction Stop
catch {
$missingFunctions = $true
Write-Warning "$function not found"
if ($missingFunctions) {
# computers to run against
# re-write to read from script parameter, file, Active Directory etc as needed
$computers = (
# define timeout for Receive-RunspaceJob (in seconds)
$timeout = 30
# code to run in each runspace
$code = {
# code goes here
# create new runspace pool
$thisRunspacePool = New-RunspacePool
# define results array
$results = @()
# iterate through each computer and create new runspace jobs
# also run Receive-RunspaceJob to collect any already finished jobs
foreach ($computer in $computers) {
New-RunspaceJob -RunspacePool $thisRunspacePool -ScriptBlock $code -Parameters @{ComputerName = $computer}
$results += Receive-RunspaceJob
# if any jobs left, wait until all jobs are finished, or timeout is reached
if ([bool](Show-RunspaceJob)) {
$results += Receive-RunspaceJob -Wait -TimeOut $timeout
Write-Output $results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment