user3585839
user3585839

Reputation: 1519

How to pass parameters to a batch file from Powershell

I've tried multiple iterations of the following and nothing worked. The .bat file needs to accept 2 parameters. $IN and $TIC which are initials and ticket number.

Variables:

$IN = $textboxInitials.Text
$TIC = $textboxTicket.Text

Commands (None of these work):

start "\\Path\InstallOffice.bat" $IN $TIC
start "\\Path\InstallOffice.bat $IN $TIC"
start "\\Path\InstallOffice.bat" KL 562355
cmd.exe /C "\\Path\InstallOffice.bat" KL 562355
& "\\Path\InstallOffice.bat" $IN $TIC

This does work, but it is without parameters.

start "\\Path\InstallOffice.bat"

I am writing a program to install a bunch of programs after our standard build process so the helpdesk can pick and choose what needs installed. This is just the first command to be run, there will be a bunch after it as well, so powershell will need to wait for the .bat file to finish before moving on the the next command.

Upvotes: 2

Views: 9658

Answers (2)

user6811411
user6811411

Reputation:

With this small demonstration batch EchoArgs.cmd:

@Echo off&SetLocal EnableDelayedExpansion
Set Cnt=0
Echo:Args=%*
:loop
If "%~1" Neq "" Set /A Cnt+=1&Echo Arg!Cnt!=%1&Shift&Goto :loop

This slightly revised versions of your 5 commands:

$IN = 'KL'
$TIC = '562355'
$Bat = ".\EchoArgs.cmd"

Clear
"--Variant 1"
start -wait -NoNewWindow $Bat -args "$IN $TIC"
"--Variant 2"
start -wait  -NoNewWindow $bat "$IN $TIC"
"--Variant 3"
start -wait  -NoNewWindow $Bat "KL 562355"
"--Variant 4"
cmd.exe /C $Bat KL 562355
"--Variant 5"
& $Bat $IN $TIC

I get this sample output:

--Variant 1
Args=KL 562355
Arg1=KL
Arg2=562355
--Variant 2
Args=KL 562355
Arg1=KL
Arg2=562355
--Variant 3
Args=KL 562355
Arg1=KL
Arg2=562355
--Variant 4
Args=KL 562355
Arg1=KL
Arg2=562355
--Variant 5
Args=KL 562355
Arg1=KL
Arg2=562355

Upvotes: 2

mvrma
mvrma

Reputation: 143

Use Start-Process. You can pass arguments with -ArgumentList $A,$B,....

Additionally you can call it with -PassThru to receive the process object and call WaitForExit() on it.

Here's the msdn page.

Upvotes: 2

Related Questions