【问题标题】:Unable to pipe cmdlet objects in cmd无法在 cmd 中通过管道传输 cmdlet 对象
【发布时间】:2016-08-22 17:46:05
【问题描述】:

我正在使用这个 powershell 命令来获取特定的用户配置文件

"Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"

但是当我通过调用 powershell 在 cmd 中使用相同的命令时,我得到了 'Where-Object 未被识别为内部或外部命令、可运行程序或批处理文件'

我在cmd中运行的命令如下:- "powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"

我只需要从 cmd 运行此命令,我没有任何其他选项。 所以请给我一个“Where-Object”的替代方案

【问题讨论】:

  • powershell "powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}"
  • @PetSerAl 正确答案是escape管道^|
  • @DavidPostill 但是,如果您不使用引号,那么您会丢失相邻的空格:powershell echo 'multiple spaces'powershell "echo 'multiple spaces'"。如果| 在引号内,则不需要对其进行转义。 (multiplespaces之间应该是多个空格,但是在cmets中好像没有保留)
  • @PetSerAl 注意。但是在这种情况下转义是有效的(不需要担心多个空格)。
  • 使用 powershell "Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela'}" 工作...谢谢

标签: windows powershell batch-file cmd


【解决方案1】:

所以请给我一个“Where-Object”的替代方法

powershell Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq 'C:\Users\Pela

您不需要替代品。上述命令失败,因为管道 | 正在由 cmd shell 而非 PowerShell 解释。

如果您 escape 管道 ^| 则管道由 PowerShell 命令按预期完成:

powershell Get-WmiObject -Class Win32_UserProfile ^| Where-Object {$_.LocalPath -eq 'C:\Users\Pela

例子:

F:\test>powershell Get-WmiObject -Class Win32_UserProfile ^| Where-Object {$_.LocalPath -eq 'C:\Users\DavidPostill'}

__GENUS           : 2
__CLASS           : Win32_UserProfile
__SUPERCLASS      :
__DYNASTY         : Win32_UserProfile
__RELPATH         : Win32_UserProfile.SID="S-1-5-21-1699878757-1063190524-3119395976-1000"
__PROPERTY_COUNT  : 12
__DERIVATION      : {}
__SERVER          : HAL
__NAMESPACE       : root\cimv2
__PATH            : \\HAL\root\cimv2:Win32_UserProfile.SID="S-1-5-21-1699878757-1063190524-3119395976-1000"
LastDownloadTime  :
LastUploadTime    :
LastUseTime       : 20160822200129.697000+000
Loaded            : True
LocalPath         : C:\Users\DavidPostill
RefCount          : 146
RoamingConfigured : False
RoamingPath       :
RoamingPreference :
SID               : S-1-5-21-1699878757-1063190524-3119395976-1000
Special           : False
Status            : 0
PSComputerName    : HAL

进一步阅读

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2015-03-30
    相关资源
    最近更新 更多