【问题标题】:Powershell ForEach-Object : Cannot bind parameterPowershell ForEach-Object:无法绑定参数
【发布时间】:2018-05-15 04:25:12
【问题描述】:

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

(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

请帮忙!谢谢你的时间。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    使用select -expand user 代替foreach-object user。这相当于做foreach-object { $_.user },这可能是你打算做的。语法灵活性的改进允许您在更高版本的 powershell 中首次尝试。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:
        (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | select user
        

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-01
            • 1970-01-01
            相关资源
            最近更新 更多