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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With