简体   繁体   中英

Powershell script with a background job inside called from c#

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-Job to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM