'Powershell taskkill /F /IM firefox.exe /T without killing the developer version with the same .exe name?
I've got a macro looping with powershell and at the end of a run to clean up it kills and restarts the browser, Standard FireFox version firefox.exe
Using:
taskkill /F /IM firefox.exe /T
How can I prevent it from killing the dev version of FireFox too, which has the same executable name?
EDIT:
I was asked to post the code
Link to the original script from github https://github.com/A9T9/RPA/blob/2501e8cbc504160e2aab89a7e35ece9fcf2873e1/command-line/powershell/run%20one%20macro%20forever.ps1
This script shows how to run a macro "forever" by checking on the command line return value and killing/restarting Browser if needed
function PlayAndWait ([string]$macro)
{
$timeout_seconds = 300
$path_downloaddir = "C:\Users\xxxx\ui-logs\"
$path_autorun_html = "C:\Users\xxxxx\UIVision Powershell Script\ui.vision.html"
$log = "log_" + $(get-date -f MM-dd-yyyy_HH_mm_ss) + ".txt"
$path_log = $path_downloaddir + $log
$browser = 2
Switch ($browser) {
1 {$cmd = "${env:Program Files(x86)}\Google\Chrome\Application\chrome.exe"; break}
2 {$cmd = "${env:ProgramFiles}\Mozilla Firefox\firefox.exe"; break} #For FIREFOX
}
$arg = """file:///"+ $path_autorun_html + "?macro="+ $macro + "&direct=1&closeRPA=1&closeBrowser=1&savelog="+$log+""""
Start-Process -FilePath $cmd -ArgumentList $arg #Launch the browser and run the macro
$status_runtime = 0
Write-Host "Log file will show up at " + $path_log
while (!(Test-Path $path_log) -and ($status_runtime -lt $timeout_seconds))
{
Write-Host "Waiting for macro to finish, seconds=" $status_runtime
Start-Sleep 10
$status_runtime = $status_runtime + 10
}
if ($status_runtime -lt $timeout_seconds)
{
$status_text = Get-Content $path_log -First 1
$status_int = -1
If ($status_text -contains "Status=OK") {$status_int = 1}
}
else
{
$status_text = "Macro did not complete within the time given:" + $timeout_seconds
$status_int = -2
}
remove-item $path_log #clean up
return $status_int, $status_text, $status_runtime
}
$testreport = "C:\xxxx\ui-logs\uireport.txt"
For ($i=0; $i -le 9999; $i++) {
Write-Host "Loop Number:" $i
$result = PlayAndWait MyMacro-04-2022 #run the macro
$errortext = $result[1] #Get error text or OK
$runtime = $result[2] #Get runtime
$report = "Loop:" + $i + " Return code: " + $result[0]+ " Macro runtime: "+$runtime+" seconds, Result: "+ $errortext
Write-Host $report
Add-content $testreport -value ($report)
if ($result[0] -ne 1)
{
taskkill /F /IM firefox.exe /T
$report = "Loop:" + $i + " Firefox closed"
Add-content $testreport -value ($report)
}
}
Solution 1:[1]
Assuming Developer edition is installed in the default directory of "C:\Program Files\Firefox Developer Edition\firefox.exe"
Get-Process Firefox | ?{$_.path -notmatch "dev"} | Stop-Process -Force
Alternatively you can use the exe file description field
Get-Process Firefox | ?{$(Get-ItemProperty -Path $_.Path).VersionInfo.FileDescription -notmatch "Developer"} | Stop-Process -Force
edit: to kill the whole process tree as per this post:
function Kill-Tree {
Param([int]$ppid)
Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $ppid } | ForEach-Object { Kill-Tree $_.ProcessId }
Stop-Process -Id $ppid
}
Get-Process Firefox | ?{$(Get-ItemProperty -Path $_.Path).VersionInfo.FileDescription -notmatch "Developer"} | %{Kill-Tree $_.Id}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |