I call a Powershell script from a c# program, getting back an collection of PSObjects as a result of pipeline.Invoke(). As a part of the PS script, an email is sent using Net.Mail.SmtpClient. However, sending an email takes some time, so I moved the email sending code into a script block that is executed using Start-Jon -ScriptBlock… And here the problem started: email is not sent. When I execute a script from a command line, it sends an email.
What can be the reason?
Actually, after adding
Wait-Jobto the code (making it synchronous) it worked again. I think that I got it.I was calling
runspace.Close()at the end of my code that terminated the host so the Powershell background job had no chance to be executed. So I looking in the wrong place, unfortunately.