0

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
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    you use `Get-Process -name $target | kill -PassThru` but don't seem to define `$Target`. where is that defined? ///// also, why are you running `Get-Process` twice? you run it @ line #1 & line #5 - they both seem to be running the same command. – Lee_Dailey Feb 14 '19 at 23:44
  • Hey! thanks for taking a look. I have changed the Get-Process -name $target | kill -PassThru to Get-Process -name $process | kill -PassThru Regarding your query around line #1 & #5, i see what you mean. how would i "get" those processes in a variable and then pipe it to a kill? – rohan groombridge Feb 15 '19 at 00:13
  • you already have them in `$Process` & that works well in line #7. ///// this line `Get-Process $process -ErrorAction SilentlyContinue` seems entirely unneeded. it doesn't assign anything to anything ... and is apparently just repeating part of line #1. ///// this line `Get-Process -Name $process | kill -PassThru` seems to be all you need. --- even there, tho, the `Get-Process` is likely not needed. just `$Process | Stop-Process -Force` should work. take a look at `Get-Help Stop-Process -Parameter force` for more info. – Lee_Dailey Feb 15 '19 at 00:48
  • Thanks. I made your suggestion changes. I ran it but it seems to not want to kill the .exe file programs. Could they be hung? And this won't kill it then or should I add a time delay after the first kill and then try another? – rohan groombridge Feb 15 '19 at 01:37
  • To give some context. The .exe files that are running all day are outputting an event that states a .DLL file associated with one of the .exe files has failed. Then I can see that the whole application doesn't take SIP calls after that. The .DLL fille is related to a SIP driver. – rohan groombridge Feb 15 '19 at 01:45
  • 1
    there are times when `Stop-Process` will not stop a process. i don't know why, tho. [*blush*] however, this post seems to have a workaround for you >> PowerShell won't terminate hung process - Stack Overflow — https://stackoverflow.com/questions/40585754/powershell-wont-terminate-hung-process – Lee_Dailey Feb 15 '19 at 04:04

1 Answers1

0

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
}
Mudit Bahedia
  • 246
  • 2
  • 11