如问题标题中所述,我有一个 Powershell 单行程序,用于按名称杀死应用程序/进程及其所有相关子进程:
powershell -c "Get-Process -Name `"Chrome`" | Select-Object -Property Id | ForEach-Object -Process { Stop-Process -Id $_.Id -Force }"
当我通过删除进程终止代码并仅打印进程 ID 进行测试时,它可以正常工作,如下所示:
powershell -c "Get-Process -Name `"Chrome`" | Select-Object -Property Id"
这会将与 Chrome 实例相关的所有 ID 显示为:
372
1232
1776
1884
2024
2676
3008
3240
但是当我试图用这篇文章中的第一个代码块杀死这些进程时,它会抛出这个错误:
Stop-Process : Cannot bind parameter 'Id'. Cannot convert value ".Id" to type "System.Int32". Error: "Input string was not in a correct format."
所以我确实应用了 [Int32] 进行类型转换,认为这足以将 ID 以有效格式带入 Stop-Process 部分并杀死它,但这也不起作用:
powershell -c "Get-Process -Name `"Chrome`" | Select-Object -Property Id | ForEach-Object -Process { Stop-Process -Id [Int32]($_.Id) -Force }"
这会引发错误:
The term '.Id' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At line:1 char:108
+ ... y Id | ForEach-Object -Process { Stop-Process -Id [Int32](.Id) -Force ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (.Id:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
任何人都可以帮助解决这个问题并通过不要过度拉长单线来使单线工作吗?