如何使用PowerShell使用PID杀死进程

时间:2019-05-23 13:18:42

标签: powershell

我想杀死所有不在文本文件$fileServices中的进程, 我使用循环来获取PID并将其发送到Stop-Process。 运行无错误。但是这个过程并没有被杀死,我的循环也不好。

请问如何使用PowerShell使用PID杀死该进程?

$ServiceName = Get-WmiObject Win32_Process -Filter "name = 'service.exe'" |
               Select-Object CommandLine
$procId = Get-WmiObject Win32_Process -Filter "name = 'service.exe'" |
          Select-Object ProcessId
$fileServices = Get-Content C:\serviceNotStop.txt

$totalcount = Get-Content C:\serviceNotStop.txt | Measure-Object -Line
$number = $totalcount.Lines

$get_path = $ServiceRunName.CommandLine
$output_file = 'C:\service_name.txt'
$get_path | Out-File -FilePath $output_file

if ($fileServices -ne $null) {
    foreach ($process in $get_path ) {
        foreach ($id in $procId ) {
            $nameExtracted = ($process -split '//')[2]
            $counter = 1
            foreach ($line in $fileServices) {
                if ($line -ne $nameExtracted) {
                    $counter++
                    "continue search file, $counter"
                    if ($counter -gt $number) {
                        " kill process after all search"
                    } else {
                        continue
                    }
                } elseif ($line -eq $nameExtracted) {
                    "keep process $line "
                    break
                }
            }
        }
    }
} else {
    "the file is empty"
}

1 个答案:

答案 0 :(得分:0)

List.txt内容
    记事本
    铬
    计算器

test.getStyleClass().addAll(series.getNode().getStyleClass());

我几乎可以正常工作了,只需要弄清楚如何在txt文件中获取多行像数组一样遍历