Can't kill off an .exe file using Powershell V4.0

家住魔仙堡 提交于 2019-12-11 19:31:52

问题


Please see below. For whatever reason I can't kill off any on the .exe process running. Randomly I have seen this script work which is why I think there is something else going on.

All that I see is the whole script has worked when testing but then I promote to prod it just fires of the email part. Email part is fine.

Will this code guarantee a kill of the processes even if they are hung.

$process = Get-Process -Name "IVR1","IVR2","IVR3"
$IVR1path = "C:\IVR1"
$IVR2path = "C:\IVR2"
$IVR3path = "C:\IVR3"
Get-Process $process -ErrorAction SilentlyContinue
if ($process) {
    Get-Process -Name $process | kill -PassThru
    Start-Sleep -s 5
    cd $IVR1path
    Start-Process ".\IVR1.exe"
    cd IVR2path
    Start-Process ".\IVR2.exe"
    cd IVR3path
    Start-Process ".\IVR3.exe"
    cd ..
    cd ..
    $From = "IVR1@example.com.au"
    $To = "myemail@example.com.au"
    $cc = "myemail@example.com.au"
    $Subject = "**TEST** - IVR1 has been recovered"
    $Body = "The IVR has been successfully recovered"
    $SMTPServer = "mail.example.com.au"
    Send-MailMessage -From $From -to $To -cc $cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer
}

回答1:


Use below code.

$process = "IVR1","IVR2","IVR3" #Creates array of process names
$IVR1path = "C:\IVR1"
$IVR2path = "C:\IVR2"
$IVR3path = "C:\IVR3"
if ($process) {
    Get-Process -Name $process | kill -PassThru
    Start-Sleep -s 5
    Start-Process "$IVR1path\IVR1.exe"
    Start-Process "$IVR2path\IVR2.exe"
    Start-Process "$IVR3path\IVR3.exe"
    $From = "IVR1@example.com.au"
    $To = "myemail@example.com.au"
    $cc = "myemail@example.com.au"
    $Subject = "**TEST** - IVR1 has been recovered"
    $Body = "The IVR has been successfully recovered"
    $SMTPServer = "mail.example.com.au"
    Send-MailMessage -From $From -to $To -cc $cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer
}


来源:https://stackoverflow.com/questions/54699835/cant-kill-off-an-exe-file-using-powershell-v4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!