I'm currently trying to multi-thread a powershell (7) script. I have a lot of function that iterates over lists and i wanted to use -Parallel to go through them.
The problem is that inside the loop i call other functions to retrieve data for each object. I know that the loop create a process in a seperate thread (thus without the functions).
So i was wondering if there is a workaround for that, do i need to redefine all my function inside $code = @'code here'
variables ?
My code is :
$Results | Where-Object {$_} | ForEach-Object -Parallel {
$Computer = $_
$Up = $True
if ($PSBoundParameters['Ping']) {
$Up = Test-Connection -Count 1 -Quiet -ComputerName $_.properties.dnshostname
}
if ($Up) {
if ($PSBoundParameters['Raw']) {
# return raw result objects
$Computer = $_
$Computer.PSObject.TypeNames.Insert(0, 'func.Computer.Raw')
}
else {
$Computer = Convert-LDAPProperty -Properties $_.Properties
$Computer.PSObject.TypeNames.Insert(0, 'func.Computer')
}
$Computer
}
}
Error :
The loop is trying to access the function : Convert-LDAPProperty
And when i execute my code i get the following error :
InvalidOperation:
$Computer.PSObject.TypeNames.Insert(0, 'func.Compute …
You cannot call a method on a null-valued expression.
Convert-LDAPProperty:
$Computer = Convert-LDAPProperty -Properties …
The term 'Convert-LDAPProperty' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Thank you for your time !