我知道这听起来很普遍,但仍然无法为我的用例找到解决方案。我想要做的是,我从一个文件中调用一个又一个的powershell,说“测试”,脚本A和脚本B
......
ScriptA.ps1
ScriptB.ps1
.....other stuff现在,如果成功地执行了我的ScriptA,那么ScriptB应该执行,但是如果scriptA抛出任何异常,而我已经在ScriptA中放置了try/catch块,那么ScriptB就不应该执行。但我无法强制执行“测试”中的任何检查,以停止ScriptB.ps1的执行。
查找退出代码,但不确定如何在“测试”中收集,类似于
......
ScriptA.ps1----returns exit code 1
if(exit code!=1)
{
ScriptB.ps1
}
else
{
"Cant execute due to error"
}
.....other stuff发布于 2022-05-20 15:49:41
发布于 2022-05-20 14:48:16
使用循环语句(如foreach($thing in $collection){...})连续调用每个脚本,然后在失败时中断循环--您可以通过检查$?自动变量来评估该循环:
$failed = $false
foreach($scriptFile in Get-ChildItem -Filter Script*.ps1){
& $scriptFile.FullName
if(-not $?){
$failed = $true
break
}
}
if($failed){
# handle failure (exit/return or whatever is appropriate)
} else {
# success, continue here
}https://stackoverflow.com/questions/72320779
复制相似问题