You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell补丁验证脚本用户切换逻辑调整求助

问题:PowerShell脚本补丁与用户绑定逻辑调整

核心需求

为每个补丁运行验证脚本,补丁与用户的绑定规则如下:

  • 运行补丁后检查其TAG,若包含#EE_PatchVerificationScriptPassed,下一个补丁使用下一个用户
  • 若未找到该TAG,下一个补丁继续使用当前用户

场景示例

示例1:第一个补丁验证通过,切换下一个用户

2092541
AutomationUser1
2109086
AutomationUser2

示例2:第一个补丁验证未通过,保持当前用户

2092541
AutomationUser1
2109086
AutomationUser1

示例3:前两个补丁中第二个验证通过,第三个补丁切换用户

2092541
AutomationUser1
2109086
AutomationUser1
2109897
AutomationUser2

已实现的TAG获取代码(可正常运行)

$Tag = (Get-VSTeamWorkItem -Id $patches[$i] -WarningAction SilentlyContinue).Fields.'System.Tags'
if ($Tag -contains "#EE_PatchVerificationScriptPassed") {...}

当前问题

自行修改的脚本未达到预期逻辑,例如实际运行结果:

2092541 AutomationUser1
2109086 AutomationUser1
2092542 AutomationUser2

而预期结果应为:

2092541 AutomationUser1
2109086 AutomationUser1
2092542 AutomationUser1

尝试过的代码版本

版本1

if ($patches) {
    $tagfound = 'true'
    for ($i = 0; $i -lt $patches.Count; $i++) {      
        for ($j = $i ; $j -lt $users.Count; $j++) {
            if ($tagfound -eq 'false') {
                $j = $j-1
            }
            
            . C:\AutomaticScript\ScriptForPipelineheader.ps1 -path "$($path)\$($patches[$i])" -User $users[$j]
            Write-Host "$($patches[$i]) $($users[$j])" 
            
            $Tag = (Get-VSTeamWorkItem -Id $patches[$i] -WarningAction SilentlyContinue).Fields.'System.Tags'
            if ($Tag -contains "#EE_PatchVerificationScriptPassed") {
                $tagfound = 'true'
            }
            break           
        }
        if ($Tag -notcontains "#EE_PatchVerificationScriptPassed") {
            $tagfound = 'false'       
        }
    }
}

版本2

if ($patches) {
        $tagfound = 'true'
        $counter = 0
        for ($i = 0; $i -lt $patches.Count; $i++) {      
            for ($j = $i ; $j -lt $users.Count; $j++) {
                if ($tagfound -eq 'false') {
                    $j = $j-$counter
                }
                
                . C:\AutomaticScript\ScriptForPipelineheader.ps1 -path "$($path)\$($patches[$i])" -User $users[$j]
                Write-Host "$($patches[$i]) $($users[$j])" 
                
                $Tag = (Get-VSTeamWorkItem -Id $patches[$i] -WarningAction SilentlyContinue).Fields.'System.Tags'
                if ($Tag -contains "#EE_PatchVerificationScriptPassed") {
                    $tagfound = 'true'
                }
                break           
            }
            if ($Tag -notcontains "#EE_PatchVerificationScriptPassed") {
                $tagfound = 'false'
                $counter += 1        
            }
        }
    }

版本3

if ($patches) {
            $counter = 0
            for ($i = 0; $i -lt $patches.Count; $i++) {      
                for ($j = $i ; $j -lt $users.Count; $j++) {
                    
                        $j = $j-$counter
  
                    . C:\AutomaticScript\ScriptForPipelineheader.ps1 -path "$($path)\$($patches[$i])" -User $users[$j]
                    Write-Host "$($patches[$i]) $($users[$j])" 
                    
                    $Tag = (Get-VSTeamWorkItem -Id $patches[$i] -WarningAction SilentlyContinue).Fields.'System.Tags'
                    break           
                }
                if ($Tag -notmatch "#EE_PatchVerificationScriptPassed") {
                    $counter += 1        
                }
            }
        }

正确的代码调整方案

核心逻辑是用一个变量跟踪当前使用的用户索引,仅当补丁验证通过时才推进索引:

if ($patches -and $users) {
    # 初始化当前用户索引,从第一个用户开始
    $currentUserIndex = 0

    foreach ($patch in $patches) {
        # 使用当前索引对应的用户运行验证脚本
        . C:\AutomaticScript\ScriptForPipelineheader.ps1 -path "$path\$patch" -User $users[$currentUserIndex]
        Write-Host "$patch $($users[$currentUserIndex])"

        # 获取当前补丁的TAG
        $Tag = (Get-VSTeamWorkItem -Id $patch -WarningAction SilentlyContinue).Fields.'System.Tags'

        # 如果TAG包含指定标签,且当前不是最后一个用户,则切换到下一个用户
        if ($Tag -contains "#EE_PatchVerificationScriptPassed" -and $currentUserIndex -lt ($users.Count - 1)) {
            $currentUserIndex++
        }
    }
}

逻辑说明

  1. 初始化$currentUserIndex = 0,默认从第一个用户开始
  2. 遍历每个补丁,使用当前索引对应的用户执行脚本
  3. 检查补丁TAG:
    • 若验证通过且还有下一个用户,索引+1,下一个补丁使用新用户
    • 若验证未通过或已到最后一个用户,保持当前索引,下一个补丁继续用当前用户

内容的提问来源于stack exchange,提问作者Bandit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 03:17:05