Parallel processing - cant find function

Hi guys, I wrote a piece of code and it works fine when I try to run in serial order. When I added additional code to run the same function twice at the same time, it says cant find function. Any idea ?

#This function is to maintain log
function WriteLog
{
Param ([string]$LogString)
$Stamp = (Get-Date).toString(“yyyy/MM/dd HH:mm:ss”)
$LogMessage = “$Stamp $LogString”
$LogPath=“D:\Logs"
$LogFile=(Get-Date).ToString(“yyyyMMdd”)+”_Logs.log"
if (!(“$LogPath$LogFile”))
{
New-Item -path “$LogPath” -name $LogFile -type “file”
}
else
{
Add-content “$LogPath$LogFile” -value $LogMessage
}
}

function MyFavFunction($FileType)
{

$ExportDropPath = "C:\Drop\"  
$ExportPickupPath = "C:\Pick\"  
$FeedMonth = @("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")  
$FeedYear = "2024"  

Set-Location -Path "C:\"  

#Cycle through all 12 months
for ($fm = 0; $fm -lt $FeedMonth.Length; $fm++)  
{  
    WriteLog "($FileType) Extracting feed for $FileType, Year is $FeedYear, Month is $($FeedMonth[$fm])"    
    $NewFileName = "$FileType"+"_$($FeedMonth[$fm])-$FeedYear"
    rename-item "$FileType" $NewFileName
    Move-Item -Path "$ExportPickupPath\$NewFileName" $ExportDropPath  
}  

}

WriteLog “Start script

#Parameters for login
$fdmee_user=“user”
$fdmee_pwd_file=“password”
$fdmee_identity=“company1”
$cloud_url=“https://somesite.com
$cloud_pod=“POD”

Run the functions in parallel

“Running fuctions”
$job1 = Start-Job -ScriptBlock { MyFavFunction “1stParameter” }
$job2 = Start-Job -ScriptBlock { MyFavFunction “2ndParameter” }

Wait for the jobs to complete

#Wait-Job $job1, $job2

Retrieve the results from the jobs

$job1 | Receive-Job
$job2 | Receive-Job

#epmautomate logout
WriteLog “End of script

I get an error message

The term ‘MyFavFunction’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (MyFavFunction:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : localhost

I spent 2 days on this and can’t seem to figure out where am I going wrong

Hi, welcome back :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.

How to format code on PowerShell.org

When you use Start-Job it runs in a different context to the rest of your script, and the job doesn’t interact with the calling session. The problem you have is that the context the job is running in knows nothing about the function that you declared.

While you could define the function in the ScriptBlock parameter, a better way is to use an initialization script:

$jobFunctions = {
    function myFunction($foo) {
    'bar'
    }
}


$job1 = Start-Job -InitializationScript $jobFunctions -ScriptBlock { myFunction 'foo' }

Wait-Job $job1 | Out-Null
Receive-Job $job1
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.