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,480 points

recategorized Nov 12, 2018 by Priyaj 23 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,000 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
• 35,640 points
150 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,520 points
36 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,520 points
152 views
+1 vote
2 answers

Unable to build TFS project because files not found

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

answered 2 days ago in Other DevOps Questions by Shashikesh Mishra
213 views
+13 votes
2 answers
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,000 points
38 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,000 points
140 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.