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

在Azure经典发布管道中用PowerShell删除Windows服务器旧证书的问题排查

问题分析:Azure经典发布管道中删除Windows服务器旧证书的故障排查

背景

在Azure经典发布管道中,尝试通过PowerShell删除Windows Server 2019上的过期证书,过程中遇到RemoteDeployer_NonZeroExitCode错误,切换到PowerShell on Target Machines任务后问题仍未解决。

初始代码与错误信息

错误日志

2024-05-07T02:43:20.4279860Z ##[error]Atleast one remote job failed. Consult logs
for more details. ErrorCodes(s):
'RemoteDeployer_NonZeroExitCodeRemoteDeployer_NonZeroExitCodeRemoteDeployer_NonZeroEx
itCode***RemoteDeployer_NonZeroExitCode'

初始执行代码

param(
    $servers = "$(deploy-Hostesses)"
)

foreach ($server in $servers) {
    Write-Host "Processing server: $server"
    
    Invoke-Command -ComputerName $server -ScriptBlock {
        # Retrieve all certificates from the certificate store
        $certs = Get-ChildItem -Path Cert:\LocalMachine\My
        
        # Define the date threshold (current date minus expiration days)
        $thresholdDate = (Get-Date).AddDays(-120)  
        
        foreach ($cert in $certs) {
            # Check if the certificate is expired
            if ($cert.NotAfter -lt $thresholdDate) {
                Write-Host "Certificate $($cert.Thumbprint) is expired. Deleting..."
                # Delete the expired certificate
                Remove-Item -Path "Cert:\LocalMachine\My\$($cert.Thumbprint)" -Force
                Write-Host "Certificate $($cert.Thumbprint) deleted."
            }
        }
    }
}

环境说明

  • 服务器系统:Windows Server 2019
  • 已启用PowerShell远程(PSRemoting)

更新后的代码(切换至PowerShell on Target Machines任务)

# Get the current date
$currentDate = Get-Date

# Define the date threshold (120 days ago)
$thresholdDate = $currentDate.AddDays(-30)

# Retrieve all certificates from the certificate store
$certs = Get-ChildItem -Path $CertStore

foreach ($cert in $certs) {
    # Check if the certificate is expired (NotAfter date is older than the threshold date)
    if ($cert.NotAfter -lt $thresholdDate) {
        Write-Output "Certificate $($cert.Thumbprint) is expired. Deleting..."
        # Delete the expired certificate
        Remove-Item -Path $cert.PSPath -Force
        Write-Output "Certificate $($cert.Thumbprint) deleted."
    }
}

存在的遗漏与问题分析

初始代码的问题

  1. 服务器列表解析错误:$servers = "$(deploy-Hostesses)" 将变量值解析为单一字符串而非数组,多服务器场景下foreach只会遍历一次整个字符串,导致Invoke-Command无法正确连接目标服务器。
  2. 权限不足:Azure发布管道服务账户通常没有目标服务器的本地管理员权限,删除LocalMachine存储下的证书需要管理员权限,权限缺失会直接导致删除失败并返回非零退出码。
  3. 错误捕获缺失:代码无任何错误处理逻辑,单台服务器执行失败(如证书被占用、权限不足)会直接终止脚本,无法定位具体故障点。
  4. 日志输出无效:远程会话中使用Write-Host,输出不会同步到Azure管道日志,无法查看远程执行的详细过程,排查难度大。

更新后代码的问题

  1. 未定义关键变量:代码中使用$CertStore但未赋值,Get-ChildItem会因路径无效报错,直接导致任务失败。
  2. 逻辑注释与代码不一致:注释标注阈值为“120天前”,但代码实际是AddDays(-30),逻辑混乱可能导致误删或漏删证书。
  3. 证书占用未处理:若证书被进程(如IIS、系统服务)占用,Remove-Item -Force也无法删除,且无重试或跳过逻辑,会直接抛出错误。
  4. 权限验证缺失:切换任务后未确认执行账户是否拥有目标服务器本地管理员权限及证书存储修改权限,权限问题仍可能存在。
  5. 日志信息不全:缺少服务器名称、证书Subject等关键信息,不利于后续故障定位。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:46:16