簡體   English   中英

從.bat腳本運行Powershell命令不起作用,但是當我直接在命令行中鍵入它時它可以工作

[英]Running Powershell command from .bat script doesn't work, however it works when I type it directly in Command Line

Powershell -Command "cat .\tmp.txt | %{$_ -replace '\D', ''}"

為什么從.bat腳本運行Powershell命令不起作用? 它僅在我直接在命令行中輸入時才有效...

從.bat腳本運行會生成以下消息:

    Expressions are only allowed as the first element of a pipeline.
    At line:1 char:39
+ cat .\tmp.txt | {$_ -replace '\D', ''} <<<<
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

powershell /? 提供此幫助文本(下面修剪以僅顯示相關文本)。

-Command
...
To write a string that runs a Windows PowerShell command, use the format:
    "& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.

所以你的批處理文件powrshell行需要閱讀:

powershell -Command "&{ cat .\tmp.txt | ForEach-Object {$_ -replace '\D', ''} }"

(注意命令周圍的額外花括號, %ForEach-Object替換)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM