Powershell ForEach对象:无法绑定参数

Powershell ForEach对象:无法绑定参数,powershell,Powershell,我正在尝试获取进程的所有者,代码: (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string** 这在win8下非常有效,但在win7中,我得到以下消息: ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "user

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

(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string**
这在win8下非常有效,但在win7中,我得到以下消息:

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
ForEach对象:无法绑定参数“Process”。无法转换“用户”val
类型为“System.String”的ue到类型为“System.Management.Automation.ScriptBlock”。
位于C:\Program Files(x86)\Advanced Monitoring Agent GP\scripts\9660.ps1:1字符:
108
+(获取WmiObject-class win32|u进程|其中{$\进程名-eq'explorer.exe'

}).getowner()| Foreach对象使用
选择-展开用户,而不是
Foreach对象用户
。这相当于执行
foreach对象{$\.user}
,这可能就是您的本意。语法灵活性的改进允许您首次尝试更高版本的powershell

旧版本的Powershell无法使用简化的语法。这应适用于以下任一情况:

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

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

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