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