Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass batch arguments with dots into a powershell script?

I have a batch script that will be run by an an external process that is not under my control. The external process can supply a variable amount of arguments to the batch script. I then want to pass these variables to a powershell script. The problem is, is that some of these variables look like:

-Dfoo.bar=baz

Powershell for some reason breaks it up into two args. On the command line, I can just add quotes around the arg and call it a day. But how would I get batch to pass it that way? Here is my script:

@echo off
SET CMD=C:\Scripts\foo.ps1

PowerShell.Exe -Command "%CMD%" %*

I noticed this question is very similar to this one. Here he's escaping the $ character. I tried doing something similar for the dot and/or the dash char, but have no luck. Anyone has any ideas?

like image 988
Jason Thompson Avatar asked Oct 28 '14 17:10

Jason Thompson


People also ask

How do I pass multiple command line arguments in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do you pass arguments to a PowerShell script?

Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

What does the DOT do in PowerShell?

The PowerShell dot-source operator brings script files into the current session scope. It is a way to reuse script. All script functions and variables defined in the script file become part of the script it is dot sourced into. It is like copying and pasting text from the script file directly into your script.

What is Dot backslash PowerShell?

To run a script in the current directory, type the path to the current directory, or use a dot to represent the current directory, followed by a path backslash ( . \ ). For example, to run the ServicesLog.ps1 script in the local directory, type: PowerShell Copy.


1 Answers

If you call your script from CMD, it works just fine as-is:

C:\>.\foo.bat -Dfoo.bar=baz
args are -Dfoo.bar=baz

To fix the issue when you run the batch script from PowerShell use the stop parsing operator --% e.g.:

C:\ PS> .\foo.bat --% -Dfoo.bar=baz
args are -Dfoo.bar=baz
like image 176
Keith Hill Avatar answered Nov 01 '22 08:11

Keith Hill