简体   繁体   English

从C#调用内部具有后台作业的Powershell脚本

[英]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() . 我从c#程序中调用Powershell脚本,由于pipeline.Invoke()的结果而返回了PSObjects的集合。 As a part of the PS script, an email is sent using Net.Mail.SmtpClient. 作为PS脚本的一部分,使用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. 但是,发送电子邮件需要花费一些时间,因此我将电子邮件发送代码移到了一个脚本块中,该脚本块使用Start-Jon -ScriptBlock执行...在这里,问题开始了:未发送电子邮件。 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. 实际上,在将Wait-Job添加到代码(使其同步)之后,它又可以工作了。 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. 我在终止主机的代码结尾处调用runspace.Close() ,因此Powershell后台作业没有机会执行。 So I looking in the wrong place, unfortunately. 因此,很不幸,我在错误的位置看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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