我有一个PowerShell函数返回一个可执行名称列表(带有文件扩展名),如果它们正在运行,我试图杀死其中任何一个,但没有取得多大成功。这是我正在使用的命令:
Get-Executable-Names `
| where { $_ -match ".exe" } `
| foreach { $_ -replace ".exe" } `
| foreach { ps $_ } `
| kill
如果我将Get-Executable-Names的输出存储在变量中并显示其内容,则显示为:
Path
----
A.exe
B.exe
C.exe
PowerShell报告此错误:
Get-Process:找不到名为“@ {Path = A}”的进程。验证进程名称并再次调用cmdlet。
+ $ Get-Executable-Names |其中{$ _ -match“.exe”} | foreach {$ _ -replace“.exe”} | foreach {ps<<<< $ _} |杀了 + CategoryInfo:ObjectNotFound:(@ {Path = A}:String)[Get-Process],ProcessCommandException
似乎-replace
操作将管道数据更改为以下格式:
@(Path=A)
@(Path=B)
@(Path=C)
我不明白。我确定我只是误解了PowerShell的对象模型,但是我忽略了什么?
答案 0 :(得分:2)
尝试在调用GetExecutableNames
之后添加以下内容%{ $_.Path }
完整答案
Get-Executable-Names
| where { $_ -match ".exe" }
| %{ $_.Path }
| %{ $_ -replace ".exe" }
| %{ ps $_ }
| kill