2017-01-27 3 views
1
Powershell -Command "cat .\tmp.txt | %{$_ -replace '\D', ''}" 

.batスクリプトのPowershellコマンドがうまく動作しないのはなぜですか?.batスクリプトからPowershellコマンドを実行すると機能しませんが、コマンドラインで直接入力すると機能します。

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 

答えて

3

powershell /?だけで、関連するテキストを表示するには、以下のトリミングこのヘルプテキストを(与える:.BATスクリプトから実行している私は、コマンドラインで直接入力するときにのみ動作します ...

は、次のようなメッセージが生成さ)。

-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に置き換えられて、余分な中括弧に注意してください)

+0

いや、まだ同じエラー – Quak

+1

DOSが解釈しようとしていたのは%でした。私の修正を参照してください。 – TechSpud

+0

nice、ありがとうございます – Quak

関連する問題