2

I'm implementing a GitHub workflow and I need to run a batch (*.bat) script under PowerShell with a variable part. This doesn't seem to work. A simple batch script is running fine. But when I try to insert a variable into its name it doesn't

I've tried these with no success:

run: |
  $i=2
  call "myscript_$i.bat"

run: |
  $i=2
  .\myscript_$i.bat

run: |
  $i=2
  .\myscript_${env:i}.bat

Is there any Obi-Wan who can help on this? )

4
  • What happens if you use & '.\script.bat'? Commented May 31, 2020 at 21:22
  • @NekoMusume, please see the updated description. Commented May 31, 2020 at 21:30
  • Try removing env and just using ${i} and see if that works? Maybe try $($i), it might work Commented May 31, 2020 at 21:38
  • @NekoMusume, thanks. Please see below. I've found a solution to how to make it work. Commented May 31, 2020 at 21:47

2 Answers 2

2

While your own solution works, Invoke-Expression should generally be avoided.

To invoke a command by a name / path based on a quoted string and/or containing a variable reference or expression, use &, the call operator:

run |
  $i=2
  & ".\myscript_$i.bat"

(In this particular case, you could even omit the enclosing ".)

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

Comments

0

Well, after some experimenting this solution works:

run |
  $i=2
  Invoke-Expression ".\myscript_$i.bat"

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.