【发布时间】:2020-06-13 19:01:55
【问题描述】:
我有 ./main.ps1 调用 ./worker1.ps1 和 worker2.ps1 参数,来自 main.ps1 的行:
# other stuff in main script
$args = @()
$args += ("-PARAM1", "$VAR1")
$args += ("-PARAM2", "$VAR2")
$worker1 = "./workers/worker1.ps1"
Invoke-Expression "$worker1 $args" -ErrorAction Stop
# other stuff in main script
$worker2 = "./workers/worker2.ps1"
Invoke-Expression "$worker2 $args" -ErrorAction Stop
如果worker1.ps1 失败,则有exit 1 行,
问题是即使worker1.ps1 失败worker2.ps1 也会被main.ps1 调用
一旦被调用的脚本之一失败,我怎么能避免这种情况并让主脚本失败?
【问题讨论】:
-
一方面,不要使用自定义变量名
$args,因为这是一个Automatic variable
标签: powershell