簡體   English   中英

Powershell taskkill /F /IM firefox.exe /T 不殺同名的開發者版?

[英]Powershell taskkill /F /IM firefox.exe /T without killing the developer version with the same .exe name?

我有一個宏循環 powershell,在運行結束時清理它會殺死並重新啟動瀏覽器,標准 FireFox 版本 firefox.exe

使用:

taskkill /F /IM firefox.exe /T

我怎樣才能防止它也殺死 FireFox 的開發版本,它具有相同的可執行文件名稱?

編輯:

我被要求發布代碼

從 github https://github.com/A9T9/RPA/blob/2501e8cbc504160e2aab89a7e35ece9fcf2873e1/command-line/powershell/run%20one%20macro%20forever.ps1鏈接到原始腳本

此腳本顯示如何通過檢查命令行返回值並在需要時終止/重新啟動瀏覽器來“永遠”運行宏

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)
    }

}

假設 Developer 版本安裝在默認目錄“C:\Program Files\Firefox Developer Edition\firefox.exe”

Get-Process Firefox | ?{$_.path -notmatch "dev"} | Stop-Process -Force

或者,您可以使用 exe 文件描述字段

Get-Process Firefox | ?{$(Get-ItemProperty -Path $_.Path).VersionInfo.FileDescription -notmatch "Developer"} | Stop-Process -Force

編輯:按照這篇文章殺死整個進程樹:

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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM