Tomasz Plonka
Tomasz Plonka

Reputation: 305

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?

Upvotes: 0

Views: 951

Answers (1)

Tomasz Plonka
Tomasz Plonka

Reputation: 305

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.

Upvotes: 1

Related Questions