I have a scenario where I want to put a function in a powershell Module, and then in one situation load the module and call the function in a loop, but in another situation I want to load the module and use the function in parallel using a Runspace. But, so far as I can tell, I can neither assign a variable the function as a value, nor create a Job using a function. Is that correct, and I am just going to need to maintain the code in two places, in one script as a function, and in another as a script block, or is there an alternative I am missing?
Asked
Active
Viewed 446 times
1 Answers
2
Function Get-Example
{
Write-Verbose -Verbose "This is an example"
}
$block = (Get-Command Get-Example).ScriptBlock
Start-Job -ScriptBlock $block

Matthew Wetmore
- 958
- 8
- 18
-
Nice. I'm curious: What would happen if the function happened to be part of a custom module, and was dependent on other functions within the same module (assuming the custom module is available in $env:PSModulePath)? I'd assume that the spawned jobs would be able to autoload the module if needed? – Robin Mar 19 '17 at 10:39
-
I believe you get exactly the script block from the function - no more, no less. And only if it was a script module. You'd need to do something like `Start-Job -InitializationScript` to import other modules. – Matthew Wetmore Mar 19 '17 at 17:50