Skip to content

Instantly share code, notes, and snippets.

@nullbind
Created June 3, 2016 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nullbind/39cdd9dde5ff22f0cc62a37ef572ce7d to your computer and use it in GitHub Desktop.
Save nullbind/39cdd9dde5ff22f0cc62a37ef572ce7d to your computer and use it in GitHub Desktop.
# Modified Example From : https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/28/beginning-use-of-powershell-runspaces-part-3/
# Added import of all current session functions into the sessionstate for the runspacepool
# --------------------------------------------------
#region - Setup custom functions
# --------------------------------------------------
# Create custom function to import into runspace session state
Function ConvertTo-Hex {
Write-Output "Function Ran"
}
#endregion
# --------------------------------------------------
#region - Import Session Functions
# --------------------------------------------------
# Import functions from the current session into the RunspacePool sessionstate
# Create runspace session state
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
# Import all session functions into the runspace session state from the current one
Get-ChildItem Function:\ | Where-Object {$_.name -notlike "*:*"} | select name -ExpandProperty name |
ForEach-Object {
# Get the function code
$Definition = Get-Content "function:\$_" -ErrorAction Stop
# Create a sessionstate function with the same name and code
$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList "$_", $Definition
# Add the function to the session state
$InitialSessionState.Commands.Add($SessionStateFunction)
}
#endregion
# --------------------------------------------------
#region - Runspace Pool Setup
# --------------------------------------------------
# Max runspace threads
$Limit = 10
# Create a Runspace Pool that uses the defined session state
$RunspacePool = [RunspaceFactory]::CreateRunspacePool($InitialSessionState)
$RunspacePool.SetMinRunspaces(1) | Out-Null
$RunspacePool.SetMaxRunspaces($Limit) | Out-Null
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
$RunspacePool.Open()
#endregion
# --------------------------------------------------
#region - Perform Tasks in Background Threads
# --------------------------------------------------
$jobs = New-Object System.Collections.ArrayList
1..50 | ForEach {
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
# Set parameters and values to import into the runspace that will be used in the script
$ParamList = @{
Param1 = "Hello World"
Param2 = "$_"
}
# Set the script to be run
$MyScript = {
Param (
$Param1,
$Param2
)
$ThreadID = [appdomain]::GetCurrentThreadId()
write-output“ThreadID: $ThreadID” -Verbose
write-output“PID: $PID” -Verbose
[pscustomobject]@{
Param1 = $param1
Param2 = $param2
ProcessID = $PID
Thread = $ThreadID
FunctionData = ConvertTo-Hex
}
}
[void]$PowerShell.AddScript($MyScript)
[void]$PowerShell.AddParameters($ParamList)
$Handle = $PowerShell.BeginInvoke()
$temp = '' | Select PowerShell,Handle
$temp.PowerShell = $PowerShell
$temp.handle = $Handle
[void]$jobs.Add($Temp)
<#
write-output (“Available Runspaces in RunspacePool: {0}” -f $RunspacePool.GetAvailableRunspaces())
write-output (“Remaining Jobs: {0}” -f @($jobs | Where {
$_.handle.iscompleted -ne ‘Completed’
}).Count)
#>
}
#endregion
# --------------------------------------------------
#region - Clean Up
# --------------------------------------------------
#Verify completed
<#write-output (“Available Runspaces in RunspacePool: {0}” -f $RunspacePool.GetAvailableRunspaces())
write-output (“Remaining Jobs: {0}” -f @($jobs | Where {
$_.handle.iscompleted -ne ‘Completed’
}).Count)#>
# Empty the jobs
$return = $jobs | ForEach {
$_.powershell.EndInvoke($_.handle)
$_.PowerShell.Dispose()
}
$jobs.clear()
#endregion
# --------------------------------------------------
#region - Return data
# --------------------------------------------------
$return
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment