2

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.

1
  • 2
    In your bat script are you referencing %1 as $IN and %2 as $TIC? Does your bat script run natively if you execute it from the cmd console, bypassing the PowerShell script? Commented Apr 27, 2017 at 13:38

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.