简体   繁体   English

如何在 GitHub 工作流/操作中的 PowerShell 下运行名称中带有变量的脚本

[英]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.我正在实施 GitHub 工作流程,我需要在 PowerShell 下运行带有可变部分的批处理 (*.bat) 脚本。 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 .虽然您自己的解决方案有效, 但通常应避免使用Invoke-Expression

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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