Я хотел бы убить все процессы, которые не находятся в текстовом файле $fileServices
, я использую цикл, чтобы получить PID для отправки его на Stop-Process
.Работает без ошибок.Но процесс не прерван, и мой цикл не работает.
Как получить PID, чтобы завершить этот процесс с помощью PowerShell, пожалуйста?
$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"
}