I want to create bulk mails from a file. If I run the code synchronously everything works. In one thread, but it works, but when I try with Job, it gives an error
The name "New-MailContact" is not recognized as the name of a cmdlet, function, script file, or executable program. Checkthe accuracy of the spelling of the name, as well as the presence and correctness of the path, and then try again.
$pass="password"|ConvertTo-SecureString -AsPlainText -Force $creds = New-Object System.Management.Automation.PsCredential('xx@yyy.fun',$pass) $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $creds -Authentication Basic -AllowRedirection -AllowClobber Import-PSSession $Session $block = { Param($line,$Session) $name = $line.Split('@')[0] New-MailContact -Name $name -ExternalEmailAddress $line } foreach($line in Get-Content c:\mail.txt) { Start-Job -Scriptblock $block -ArgumentList $line $Session } Get-Job While (Get-Job -State "Running") { Start-Sleep 1 } # Getting the information back from the jobs Get-Job | Receive-Job Disconnect-ExchangeOnline