繁体   English   中英

Powershell ForEach-Object:无法绑定参数

[英]Powershell ForEach-Object : Cannot bind parameter

我正在尝试获取进程的所有者,代码:

(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string**

这在win8下很好用,但是在win7中我得到了这个msg:

ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "user" val
ue of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Program Files (x86)\Advanced Monitoring Agent GP\scripts\9660.ps1:1 char:
108
+ (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'
}).getowner() | Foreach-Object <<<<  user | out-string
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], Parameter 
   BindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh 
   ell.Commands.ForEachObjectCommand

请帮助! 谢谢您的时间。

可以使用select -expand user代替foreach-object user select -expand user 这等效于执行foreach-object { $_.user } ,这可能就是您要执行的操作。 语法灵活性的提高使您可以尝试在更高版本的Powershell中进行尝试。

Powershell的较旧版本无法使用简化的语法。 这应该适用于任何一个:

(Get-WmiObject -class win32_process | 
 where{$_.ProcessName -eq 'explorer.exe'}).getowner() |
 Foreach-Object { $_.user | out-string }
(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | select user

我遇到了类似的问题,但就我而言,脚本中有一个不可打印的字符,出现在一个}之后。 ASCII代码03。通过在二进制编辑器(Textpad8)中打开脚本,我发现了这一点。 我删除了这个字符,它为我解决了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM