PowerShell自动化多条件判断与错误校验逻辑修复请求
自动化任务运行条件逻辑修复问题
问题背景
多次尝试解决自动化任务的条件判断逻辑问题,Copilot未提供有效帮助。需要根据特定变量的状态决定是否触发自动化,变量分为三类:允许触发的错误变量、带分支条件的错误变量、阻止触发的错误变量。
原有代码
Write-Host "Check if automation should run" $IgnoredSqlErrors = 'false' if ( $eightpointsixfivefive -ne 'true' -and $eightpointsixsix -ne 'true' ) { $IgnoredSqlErrors = 'true' } if ( (![string]::IsNullOrEmpty($TfsIdsAgg) -or ![string]::IsNullOrEmpty($changesets) -or ![string]::IsNullOrEmpty($eqnumbers) -or ![string]::IsNullOrEmpty($MissingRollBack) -or ![string]::IsNullOrEmpty($VersionsRangeWithCU) -or ![string]::IsNullOrEmpty($SqlError)) -and $IgnoredSqlErrors -eq 'true' -and [string]::IsNullOrEmpty($errorAggregation_new) -and [string]::IsNullOrEmpty($InconsistentPatch) -and [string]::IsNullOrEmpty($VersionsRange) ) { $patchVerifiedWithWarning = 'true' } else { Write-Host "The automation should not run" }
预期运行逻辑
- 当以下变量存在值时,自动化应运行:
$TfsIdsAgg $changesets $eqnumbers $MissingRollBack $VersionsRangeWithCU - 若
$SqlError存在值,仅当$eightpointsixfivefive和$eightpointsixsix均为false时,自动化才应运行 - 当以下变量存在值时,自动化不应运行:
$errorAggregation_new $InconsistentPatch $VersionsRange
当前问题
原有逻辑仅在$IgnoredSqlErrors = 'true'时正常工作,$IgnoredSqlErrors = 'false'时失效,具体场景:
- 当
$TfsIdsAgg有值且$eightpointsixfivefive为true时,自动化应运行但未触发 - 当
$TfsIdsAgg、$SqlError都有值,且$eightpointsixfivefive或$eightpointsixsix为true时,自动化不应运行但逻辑出错 - 当
$TfsIdsAgg、$SqlError都有值,且$eightpointsixfivefive和$eightpointsixsix均为false时,自动化应运行,这部分原有逻辑正常
核心问题在于$SqlError与其他允许触发的变量被放在同一个OR条件中,导致分支逻辑混乱。
修改后的代码
if (#L1 (![string]::IsNullOrEmpty($TfsIdsAgg) -or ![string]::IsNullOrEmpty($changesets) -or ![string]::IsNullOrEmpty($eqnumbers) -or ![string]::IsNullOrEmpty($MissingRollBack) -or ![string]::IsNullOrEmpty($VersionsRangeWithCU) -or ![string]::IsNullOrEmpty($SqlError)) -and [string]::IsNullOrEmpty($errorAggregation_new) -and [string]::IsNullOrEmpty($InconsistentPatch) -and [string]::IsNullOrEmpty($VersionsRange) ) { if (#L2 ![string]::IsNullOrEmpty($SqlError) -and $IgnoredSqlErrors -eq 'true' ) { $patchVerifiedWithWarning = 'true' Write-Host "the automation was triggered" } elseif (#L2 [string]::IsNullOrEmpty($SqlError) -and ( ![string]::IsNullOrEmpty($TfsIdsAgg) -or ![string]::IsNullOrEmpty($changesets) -or ![string]::IsNullOrEmpty($eqnumbers) -or ![string]::IsNullOrEmpty($MissingRollBack) -or ![string]::IsNullOrEmpty($VersionsRangeWithCU) ) ) { $patchVerifiedWithWarning = 'true' Write-Host "the automation was triggered" } else {#L2 Write-Host "The automation should not run due to error in:" if ($SqlError) {#L3 Write-Log -logfile $loglocation -Message "Errors in SqlError:" foreach ($s in $sqlerror) {#L4 $temp = "" $temp = ($s -split '<br>') Write-host $temp }#end foreach L4 }#endif #l3 }#end if-else L2 } else {#l1 Write-Host "The automation should not run due to error in:" if ($SqlError) {#l2 Write-Log -logfile $loglocation -Message "Errors in SqlError:" foreach ($s in $sqlerror) { $temp = "" $temp = ($s -split '<br>') Write-host $temp } }#endif L2 if ($errorAggregation_new) { Write-host $errorAggregation_new } if ($InconsistentPatch) { Write-host $InconsistentPatch } if ($VersionsRange) { Write-Host $VersionsRange } }#endif L1
内容的提问来源于stack exchange,提问作者Bandit
相关产品推荐
相关产品推荐

