简体   繁体   中英

Passing parameters from batch file to PowerShell

I want to automate a task where I need to call a PowerShell script and pass few parameters to it (I am not the owner of that script, so I can't modify it). Is there any way I can pass parameters from batch file by calling that PowerShell script? Please find the below text when I tried to manually call the .ps1 file:

C:\Users\spa8\Desktop\V6_Git>git_powershell.bat

cmdlet New-MergeRequest.ps1 at command pipeline position 1
Supply values for the following parameters:
From_Branch: Xdev
To_Branch: master
Merge_Title: test
ADL_Issues[0]:

Here I am calling git_powershell.bat file which is calling New-MergeRequest.ps1 file. Is there any way I can pass Xdev, master, etc. parameters in git_powershell.bat file so that New-MergeRequest.ps1 can read it and execute it?

You're prompted for input because the PowerShell script defined the parameters as mandatory , but was called without them. To avoid this you can simply pass the parameters as arguments to the PowerShell script, either as positional parameters:

New-MergeRequest.ps1 Xdev master "a test" ...

or named parameters:

New-MergeRequest.ps1 -From_Branch Xdev -To_Branch master -Merge_Title "a test" ...

If you don't want to hard-code the parameters in the batch file you need to pass batch file parameters to the PowerShell script, like this:

New-MergeRequest.ps1 "%~1" "%~2" "%~3" ...

or (if you want to pass all batch file arguments to the PowerShell script), like this:

New-MergeRequest.ps1 %*

and call the batch file like this:

git_powershell.bat Xdev master "a test" ...

Note that batch scripts support only positional parameters, not named parameters, so you must supply all arguments in the correct order.

In your .bat file:

powershell -File "file" %*

and call it like:

git_powershell.bat -Xdev arg1 -master uri

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM