简体   繁体   中英

How run a script with variable in the name under PowerShell in GitHub workflow / actions

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? )

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 " .)

Well, after some experimenting this solution works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM