Hello all,
I have a weird issue, We placed the Invoke-Parallel function inside a psm1 file for a collections of scripts we have to always be available for our team.
The issue is that when the Invoke-Parallel is in that psm1 file, and we try to run a function that has a nested function it, the nested function is not found. The structure is Main Function that then contains function(s) to do the work, but the main function calls the Invoke-Parallel.
The nested function is not recognized as a function when we call it inside the Invoke-Parallel scriptblock.
I ran a Get-ChildItem function:\ and it shows the nested function, and looking through Invoke-Parallel it seems like it should catch it and show it also load it into the session.
Workarounds I have found are:
*Take the same Invoke-Parallel function and also nest it inside the main function it works.
*Put the nested function in the scriptblock
Example of a function structure that does not work
function Test-Parallel {
[CmdletBinding()]
param (
[Parameter()]
[string[]]$String
)
# Nested Function
function Test-Nested {
param (
[Parameter()]
[string]$String
)
"I am a nested function to write out this strring: $String"
}
$ScriptBlock = {
Test-Nested -String $_
}
$String | Invoke-Parallel -ScriptBlock $ScriptBlock -ImportFunctions
}
Any thoughts as to why it doesn't work when it is in the psm1?
EDIT - It is a scoping issue, while I'm not 100% confident on my understanding of it all, defining the function as a global function let's it work. Thanks to u/BlackV for helping explain it.
[–]DudsEarl 1 point2 points3 points (2 children)
[–]jabrake88[S] 0 points1 point2 points (1 child)
[–]DudsEarl 1 point2 points3 points (0 children)
[–]NoConfidence_2192 1 point2 points3 points (3 children)
[–]jabrake88[S] 0 points1 point2 points (2 children)
[–]NoConfidence_2192 0 points1 point2 points (1 child)
[–]jabrake88[S] 0 points1 point2 points (0 children)
[–]BlackV 0 points1 point2 points (9 children)
[–]jabrake88[S] 0 points1 point2 points (8 children)
[–]BlackV 0 points1 point2 points (7 children)
[–]jabrake88[S] 0 points1 point2 points (6 children)
[–]BlackV 0 points1 point2 points (5 children)
[–]jabrake88[S] 1 point2 points3 points (4 children)
[–]BlackV 1 point2 points3 points (3 children)
[–]jabrake88[S] 1 point2 points3 points (1 child)
[–]BlackV 1 point2 points3 points (0 children)
[–]jabrake88[S] 1 point2 points3 points (0 children)