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

如何在PowerShell中循环多组Header调用Invoke-RestMethod更新PDU密码?

问题

我是PowerShell/编程新手,目前尝试更新Chatsworth CPI PDU的管理员密码。这些PDU采用菊花链连接,可通过主/备PDU IP访问。每个PDU有唯一标识符,需作为Header传递;验证后API会返回Sessionid,该Sessionid需与标识符一同传递至其他端点,且有效期仅10分钟。

我希望找到一种方法,在Invoke-RestMethod中同时循环标识符和Sessionid,以批量更新所有密码,请问最佳实现方式是什么?

当前测试获取Sessionid的代码:

#Unique Identifier for each PDU
$Identifier = @("80224", "80245", "80181", "76998", "80226", "80949", "80479", "80556", "86619", "86621", "80521", "80925", "80293", "80924", "76893", "80514", "80363", "81280", "80476")

#Loop each identifier to endpoint
$Sessionid = foreach ($identity in $Identifier) {
    <# $$header is the current item #>

    $Newheader = @{
        "PDUSelector" = $identity

    }

    

    Invoke-RestMethod -Uri $Login -Method Post -SkipCertificateCheck -Body $body -ContentType application/json -Headers $Newheader  


    
}
#Sessionids returned
$Sessionid.SessionID

返回的Sessionid示例:

+KZWXna9xd2yvw==
yL6FALD8n/5B2g==
AZyCuSvqScOXTA==
oPvKWulUCqpjzA==
8utMMVZ5o0aIag==
VVY7QQcsCPPHCw==
BRhvFvCLQGEkiQ==
FmFMLZc/3ITQrQ==
g+XrlAaImDcGAw==
bXYRvNw0yRIDUw==
mYTW3HtjT1Mopg==
3Bk9PX4bQXvp+g==
sqZJGKO/SnrDdQ==
COg4+s84jurHgA==
mKL6a35Y3m5gnw==
qqTjRHxtLV5sXw==
skyV2hB9/dqJ8A==
MolN0nt1rqBg/A==
VhbAj/43I/yGyw==
解决方案

核心思路是把每个PDU的标识符和对应的SessionID绑定在一起,确保循环时能同时获取两者。推荐用自定义对象数组存储这组关联数据,之后遍历数组执行密码更新操作。

步骤1:重构SessionID获取逻辑,绑定标识符与SessionID

修改原有代码,返回包含Identifier和SessionID的自定义对象,保证两者一一对应:

# 定义PDU标识符列表
$pduIdentifiers = @("80224", "80245", "80181", "76998", "80226", "80949", "80479", "80556", "86619", "86621", "80521", "80925", "80293", "80924", "76893", "80514", "80363", "81280", "80476")

# 登录URL和请求体(请替换为实际值)
$Login = "https://your-pdu-ip/api/login"
$body = @{
    Username = "admin"
    Password = "old-admin-password"
} | ConvertTo-Json

# 循环获取SessionID并存储关联数据
$pduSessions = foreach ($id in $pduIdentifiers) {
    $headers = @{ "PDUSelector" = $id }
    $response = Invoke-RestMethod -Uri $Login -Method Post -SkipCertificateCheck -Body $body -ContentType "application/json" -Headers $headers
    
    [PSCustomObject]@{
        Identifier = $id
        SessionID  = $response.SessionID
    }
}

# 查看绑定结果
$pduSessions

步骤2:遍历关联数据,批量更新密码

利用绑定好的标识符和SessionID数组,循环构造请求参数,调用密码更新接口:

# 密码更新接口URL(请替换为实际地址)
$passwordUpdateUrl = "https://your-pdu-ip/api/update-admin-password"

# 新密码(请替换为符合要求的强密码)
$newPassword = "your-new-strong-password"

foreach ($session in $pduSessions) {
    # 构造请求头:包含PDU标识符和SessionID
    $updateHeaders = @{
        "PDUSelector" = $session.Identifier
        "SessionID"   = $session.SessionID
    }

    # 构造密码更新请求体(请根据API文档调整字段)
    $updateBody = @{
        NewPassword = $newPassword
        # 若API需要旧密码验证,添加以下字段
        # OldPassword = "old-admin-password"
    } | ConvertTo-Json

    try {
        Invoke-RestMethod -Uri $passwordUpdateUrl -Method Post -SkipCertificateCheck -Body $updateBody -ContentType "application/json" -Headers $updateHeaders
        Write-Host "PDU $($session.Identifier) 密码更新成功" -ForegroundColor Green
    }
    catch {
        Write-Host "PDU $($session.Identifier) 密码更新失败:$($_.Exception.Message)" -ForegroundColor Red
    }
}

关键注意事项

  • SessionID有效期:由于SessionID仅10分钟有效,建议将获取SessionID和更新密码的逻辑放在同一脚本块中,避免间隔过久导致Session失效。
  • 错误处理:try/catch块可捕获单个PDU更新失败的情况,不会中断其他PDU的操作。
  • API参数验证:请对照Chatsworth CPI PDU官方API文档,确认密码更新接口的请求方法、请求体字段和头参数是否正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 10:06:22