0

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?

2
  • Check [this out][1]... [1]: stackoverflow.com/questions/31804858/… ...The [cmdletbinding] might help you out Commented Aug 20, 2015 at 15:12
  • Does not help "yourscript.ps1 %IN% %OUT%d" I need to see the full statement. I am a powershell beginner and already repent it... Commented Aug 20, 2015 at 15:13

3 Answers 3

1

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%
Sign up to request clarification or add additional context in comments.

5 Comments

The name of the variable %Param1% is it bound to the name inside the .ps1 file or is the order determining wether %Param1% is passed to $Tag which is defined at first?
If I understand you right the order is determined by the "Position" part in the cmdlet binding. so in the above snippet $Tag would be assigned the value of %param1% as %param1% appears at position 1 on the cmdline. If the script was called using test1.ps1 %param2% %param1% then $tag would be assigned the value of %param2% Is that what you mean ?
Yes this is what I meant and it works as expected. There is a natural order! Thanks a bunch Shovers_!
This is not the right way to pass the arguments and it's not going to work if Param1 or Param2 contain blank spaces
Powershell.exe -ExecutionPolicy Bypass -File "test1.ps1" Awesome!
0

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.

1 Comment

Do you have proof that this works? It fails to run anything for me.
0

In your test.bat

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.