0

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
)
  • 1
    Why do you need a batch file? Just run the command you want directly from PowerShell. – Bill_Stewart Nov 29 '17 at 18:05
  • I need the batch because it triggers a sequence of sql procedures and functions. It would be a huge task to convert all in to PS. Is there a way to call the batch from my PS? – m.joe Nov 29 '17 at 18:40
  • 1
    You can just run the batch file as an executable – DeveloperTK Nov 29 '17 at 18:44
  • 1
    Also used the below – m.joe Nov 29 '17 at 18:44
  • Start-Process "D:\load.bat" $PasswordStart -Process : Process with an Id of 391020 is not running. At D:\Oracle\Scripts\Obi\OBIEE_Load_Process.ps1:4 char:1 + Start-Process "D:\load.bat" $Password + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Start-Process], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.StartProcessCommand – m.joe Nov 29 '17 at 18:45
1

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.

0

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.

0

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.