{"id":75401,"date":"2015-11-28T11:59:00","date_gmt":"2015-11-28T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/28\/powertip-add-custom-function-to-runspace-pool\/"},"modified":"2019-02-18T09:20:52","modified_gmt":"2019-02-18T16:20:52","slug":"powertip-add-custom-function-to-runspace-pool","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-add-custom-function-to-runspace-pool\/","title":{"rendered":"PowerTip: Add Custom Function to Runspace Pool"},"content":{"rendered":"<p><b>Summary<\/b>: Boe Prox shows how to add a custom function to a runspace pool.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;How can I use Windows PowerShell to add a custom function to a runspace pool?<\/p>\n<p style=\"margin-left:30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Use the following approach:<\/p>\n<p style=\"margin-left:90px\">#Custom Function<\/p>\n<p style=\"margin-left:90px\">Function ConvertTo-Hex {<\/p>\n<p style=\"margin-left:90px\">&nbsp;&nbsp;&nbsp; Param([int]$Number)<\/p>\n<p style=\"margin-left:90px\">&nbsp;&nbsp;&nbsp; &#8216;0x{0:x}&#8217; -f $Number<\/p>\n<p style=\"margin-left:90px\">}<\/p>\n<p style=\"margin-left:90px\">#Get body of function<\/p>\n<p style=\"margin-left:90px\">$Definition = Get-Content Function:\\ConvertTo-Hex -ErrorAction Stop<\/p>\n<p style=\"margin-left:90px\">#Create a sessionstate function entry<\/p>\n<p style=\"margin-left:90px\">$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry <br>-ArgumentList &#8216;ConvertTo-Hex&#8217;, $Definition<\/p>\n<p style=\"margin-left:90px\">#Create a SessionStateFunction<\/p>\n<p style=\"margin-left:90px\">$InitialSessionState.Commands.Add($SessionStateFunction)<\/p>\n<p style=\"margin-left:90px\">&nbsp;#Create the runspacepool by adding the sessionstate with the custom function<\/p>\n<p style=\"margin-left:90px\">$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5,$InitialSessionState,$Host)<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Boe Prox shows how to add a custom function to a runspace pool. &nbsp;How can I use Windows PowerShell to add a custom function to a runspace pool? &nbsp;Use the following approach: #Custom Function Function ConvertTo-Hex { &nbsp;&nbsp;&nbsp; Param([int]$Number) &nbsp;&nbsp;&nbsp; &#8216;0x{0:x}&#8217; -f $Number } #Get body of function $Definition = Get-Content Function:\\ConvertTo-Hex -ErrorAction Stop [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[162,356,650,3,45],"class_list":["post-75401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-boe-prox","tag-powertip","tag-runspace","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Boe Prox shows how to add a custom function to a runspace pool. &nbsp;How can I use Windows PowerShell to add a custom function to a runspace pool? &nbsp;Use the following approach: #Custom Function Function ConvertTo-Hex { &nbsp;&nbsp;&nbsp; Param([int]$Number) &nbsp;&nbsp;&nbsp; &#8216;0x{0:x}&#8217; -f $Number } #Get body of function $Definition = Get-Content Function:\\ConvertTo-Hex -ErrorAction Stop [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=75401"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=75401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}