Elisabeth
Elisabeth

Reputation: 21206

How to run a powershell script file from within a bat file and pass multiple parameters

I want to call test.bat which again triggers the Powershell.exe with the file test

test.bat:

Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1 -Param1 Value1 -Param2 Value2"

test1.ps1:

param{$Tag,$CommitId}

Write-Host $Tag
Write-Host $CommitId

Both files are put on the same directory.

At the moment I get an error that my file does not have a .ps1 extension but thats not true... but I guess that is because I pass the parameters in a wrong way...

So how do I correctly pass the parameters to the call in the My.bat?

Upvotes: 0

Views: 18706

Answers (3)

hdhruna
hdhruna

Reputation: 866

In your test.bat

powershell -command "&{C:\temp\script.ps1 -Tag Value1 -CommitId Value2"}"

Upvotes: 0

Jower
Jower

Reputation: 575

I would just change test.bat to

Powershell.exe -ExecutionPolicy Bypass -Command "test1.ps1 -Tag Value1 -CommitId Value2"

Of course changing the Value1 and Value2 to the actual values you want to pass.

Upvotes: 0

Shovers_
Shovers_

Reputation: 507

OK, so from batch test.bat might look like this

@echo off
set Param1="some text"
set Param2="Some more text"

test1.ps1 %Param1% %Param2%

Thats how I would pass parameters to a powershell script. You dont necessarily have to run powershell.exe with all the other parameters

In the PS1 file I would then do:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$Tag,

[Parameter(Mandatory=$True,Position=2)]
[string]$commitId
)

write-host $tag
write-host $CommitId

If you absoloutley needed the other switches against powershell.exe you could maybe do..

Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1" %Param1% %Param2%

Upvotes: 1

Related Questions