PowerTip: Add Custom Function to Runspace Pool

Doctor Scripto

Summary: Boe Prox shows how to add a custom function to a runspace pool.

Hey, Scripting Guy! Question How can I use Windows PowerShell to add a custom function to a runspace pool?

Hey, Scripting Guy! Answer Use the following approach:

#Custom Function

Function ConvertTo-Hex {

    Param([int]$Number)

    ‘0x{0:x}’ -f $Number

}

#Get body of function

$Definition = Get-Content Function:\ConvertTo-Hex -ErrorAction Stop

#Create a sessionstate function entry

$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry
-ArgumentList ‘ConvertTo-Hex’, $Definition

#Create a SessionStateFunction

$InitialSessionState.Commands.Add($SessionStateFunction)

 #Create the runspacepool by adding the sessionstate with the custom function

$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5,$InitialSessionState,$Host)

0 comments

Discussion is closed.

Feedback usabilla icon