Command not working in .bat script but works in CLI

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

I'm trying to run the above command in a batch script. It works perfectly fine if I run it on command line but this shows up if I try and run it from the script:

    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
Jul 19, 2018 in Other DevOps Questions by shubham
• 6,890 points

recategorized Nov 12, 2018 by Priyaj 41 views

1 answer to this question.

0 votes
powershell /?

If you run the above command this help text will show

-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.

Therefore you need to make the following changes in your batch file

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

(note the extra curly braces wrapped around your command and % being replaced with ForEach-Object)

answered Jul 19, 2018 by ajs3033
• 7,280 points

Related Questions In Other DevOps Questions

0 votes
1 answer

How to stop sqlplus command in unix shell script when any sql fails

One idea would be to launch the ...READ MORE

answered Jul 16, 2018 in Other DevOps Questions by Kalgi
• 42,410 points
797 views
0 votes
1 answer

Using conditional to execute a command in salt

You can use unless inside your state ...READ MORE

answered Jul 23, 2018 in Other DevOps Questions by DareDev
• 6,810 points
107 views
0 votes
1 answer

How to get issues count based on rules in a sonar project?

There are API docs in the footer ...READ MORE

answered May 4, 2018 in Other DevOps Questions by DareDev
• 6,810 points
446 views
+1 vote
2 answers

Unable to build TFS project because files not found

Please make a folder in solution Nd ...READ MORE

answered May 16 in Other DevOps Questions by Shashikesh Mishra
670 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
215 views
0 votes
1 answer
0 votes
1 answer

Execute puma workers only in prod but not in dev environment

You can us RACK_ENV here instead of ...READ MORE

answered Aug 16, 2018 in Other DevOps Questions by ajs3033
• 7,280 points
63 views
0 votes
1 answer

Puppet not recieving hiera data

Puppet does not consider the variables defined ...READ MORE

answered May 7, 2018 in Other DevOps Questions by ajs3033
• 7,280 points
371 views