首页 > 解决方案 > 带有 -Force 的 cmdlet Stop-Process 不起作用

问题描述

我在 PowerShell 窗口中输入以下命令:

(Start-Process powershell {.\scripts\skiptest\lockfile.bat} -Passthru).ID > .\pid.txt

假设 pid.txt 中保存的 PID 为 1234。其内容lockfile.bat为:

@ECHO OFF

powershell.exe -command "$lock=[System.IO.File]::Open('Parse.java','Open','ReadWrite','None');Write-Host -NoNewLine 'Press any key to release the file...';$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')"

现在,当我尝试使用以下命令杀死较新的 PowerShell 时:

Stop-Process -Force -Id 1234

它不关闭。我怎样才能关闭它?

标签: powershell

解决方案


$ParentPid = '1234'
$ChildProcs = Get-WmiObject -Class Win32_Process -Filter "ParentProcessID=$ParentPid" -Property ProcessId
Stop-Process -Force -id $ChildProcs.ProcessId

推荐阅读