Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: Add Custom Function to Runspace Pool

$
0
0

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)


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>