检查正在运行的进程,如果找不到它,则启动它

时间:2016-11-29 11:40:47

标签: powershell

我正在尝试编写一个简短的脚本,每天早上在一台机器上运行以检查两个程序,如果它们没有运行,则启动它们。我做到了这个:

$process = "ProcessToStart"
$check = Get-Process $process
$executable = "ProcessFilePath"

if (!($check)) {
    Start-Process $executable
    Send-MailMessage -From "Machine" -To "IT" -Subject "Program was restarted" -Body "The program wasnt running" -SMTP "mail.blah.com"
}

$process = "outlook"
$executable = "C:\Program Files\Microsoft Office 15\root\office15\outlook.exe"

if (!($check)) {
    Start-Process $executable
    Send-MailMessage -From "Machine" -To "IT" -Subject "Program was restarted" -Body "The program wasnt running" -SMTP "mail.blah.com"
}

我遇到的问题是,上周它有效,只有在进程没​​有运行时才启动/通过电子邮件发送。本周我已经重新测试了它,即使进程已经运行,它也会启动并发送电子邮件,我无法弄清楚有什么不同。

0 个答案:

没有答案