【问题标题】:Run PowerShell command (with pipeline) in CMD在 CMD 中运行 PowerShell 命令(带管道)
【发布时间】:2013-12-27 13:56:57
【问题描述】:

当我们在 PowerShell 中运行以下命令时:

get-date | sc C:\temp\date.log

它使用当前日期创建 date.log 文件。

但是如果我们通过 CMD 运行同样的:

powershell get-date | sc C:\temp\date.log

它抱怨:

错误:无法识别的命令

DESCRIPTION:
        SC is a command line program used for communicating with the
        Service Control Manager and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...

显然,CMD 将用于 POSH 的管道与它自己的管道混淆了。

谁能告诉我如何让它通过 CMD 运行?

感谢期待。

【问题讨论】:

    标签: powershell command-prompt


    【解决方案1】:
    powershell -Command " & {Get-Date | sc c:\tmp\date.log}"
    

    CMD and PowerShell

    【解决方案2】:

    只要管道符号用双引号从 cmd 中转义,它就可以工作。 Sc 是一个不幸的别名,因为 windows 中有一个 sc.exe 命令。 ?或者 where-object 可能比尝试在查询中嵌入一组额外的双引号更容易。

    powershell "get-date | set-content c:\tmp\date.log"
    
    powershell "get-ciminstance win32_volume | ? label -like 'month *' | select label"
    

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 1970-01-01
      • 2020-04-02
      • 2017-10-04
      • 2018-03-14
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多