powershell - 如何让 PID 使用 PowerShell 终止进程
问题描述
我想杀死所有不在文本文件中的进程$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"
}
解决方案
List.txt 内容
记事本
chrome
计算器
$list = Get-Content -Path C:\Users\username\Desktop\TEMP\list.txt -Raw
Get-Process | Where-Object ProcessName -Like $list | kill -Force
我几乎有这个工作,只需要弄清楚如何让 txt 文件中的多行像数组一样被迭代
推荐阅读
- laravel - 在数组数组中插入对象
- asp.net-core - 在 vscode 调试控制台中禁用符号加载的详细日志记录
- javascript - 使用微调器单击按钮时的 BlockUI
- c# - 在 Twilio 中,如何记录 IVR 中每个步骤的持续时间?
- python - 我该如何修复“没有名为 tensorflow.python.saved_model.model_utils 的模块”
- xquery - XQuery 合并两个 xml 文件
- botframework - 如何停止在 AppInsights 中记录的 CustomEvent“活动” - Enterprise Bot Enterprise
- aws-lambda - 如何在 DynamoDB 中动态查找特定 emailID 是否存在?
- c# - Ajax POST 请求未命中控制器方法
- java - 形式的 Java 泛型
> 和 Eclipse 编译器