m.joe
m.joe

Reputation: 115

How to call a batch file with parameters from a command prompt

Below is the line in my power shell script where I call the batch file(with parameters) and I get a positional parameter error.

Could someone please help on how to call a batch file with parameters from a powershell script and how to capture that input parameter from batch file. Thanks

Line in my Power shell script:

Start-Process "cmd.exe" "/c D:\load.bat" "$Password"

--Error:
**PS D:\Oracle\Scripts\Obi> .\XYZ.ps1
Start-Process : **A positional parameter cannot be found that accepts argument 'password'.**
At D:\.ps1:4 char:1
+ Start-Process "cmd.exe" "/c D:\load.bat"  "$Password"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand**

Script in Batch file (load.bat):

set "Var=%1"
for %%i in (*.csv) do (
    sqlldr USERID=userid/%Var%@server:prot/domain, CONTROL=control.ctl, LOG=logfile.log,ERRORS=9999 data='%%i' direct='true'
    move %%i D:\backup
)

Upvotes: 2

Views: 21933

Answers (3)

JosefZ
JosefZ

Reputation: 30183

In doubt, read and follow Start-Process documentation.

start-process cmd.exe -ArgumentList "/c", "D:\load.bat", "$password"

Above would run on the assumption that $password variable does not contain cmd poisonous characters (see Redirection) like

& Ampersand
< Less-Than Sign
> Greater-Than Sign
| Vertical Line

Upvotes: 2

Bacon Bits
Bacon Bits

Reputation: 32210

You should be able to run:

D:\load.bat "$Password"

Or:

cmd.exe /c D:\load.bat "$Password"

However, you should also be able to run:

Start-Process -FilePath 'C:\Windows\System32\cmd.exe' -ArgumentList '/c', 'D:\load.bat', "$Password"

Note that /c and 'D:\load.bat' are considered separate arguments.

Start-Process can be useful if you want to hide the window (-WindowStyle Hidden) or force PowerShell to wait for the script to complete (-Wait). You may need the -PassThru parameter if you need a value returned from the batch file.

Upvotes: 1

Bill_Stewart
Bill_Stewart

Reputation: 24585

To run a batch file (cmd.exe shell script) from a PowerShell prompt, just type the batch file's name, followed by its parameters, and press Enter. Remember: PowerShell is a shell, which means it runs command you type, just like cmd.exe does. For example:

D:\load.bat [param1 [param2 [...]]

One difference from cmd.exe is that if the command you want to run contains spaces, enclose the command in quotes and invoke it with the & (call or invocation) operator. For example:

& "D:\Script Tools\load.bat" [param1 [param2 [...]]

Just as in cmd.exe, if a parameter contains spaces, don't forget to quote it.

Upvotes: 1

Related Questions