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

请求修改PowerShell脚本以批量查询多服务器iDRAC固件版本

批量查询多服务器iDRAC固件版本的PowerShell脚本

没问题,我帮你把单服务器的iDRAC固件查询脚本改成批量版本,用foreach循环和哈希表来管理目标服务器,输出完全符合你要的表格格式,还加了基础的错误处理避免个别服务器故障导致整个脚本中断。

修改后的完整脚本

# 统一的iDRAC账号密码(如果不同服务器有不同账号,可移到服务器列表的哈希表里)
$SM_Module_username = "admin"
$SM_Module_password = "xxxxxx"
$secpasswd = ConvertTo-SecureString $SM_Module_password -AsPlainText -Force
$global:credential = New-Object System.Management.Automation.PSCredential($SM_Module_username, $secpasswd)

# 定义目标服务器列表(哈希表数组,可添加更多服务器,若有不同账号可在这里单独设置)
$targetServers = @(
    @{ ServerIP = "172.128.35.56" },
    @{ ServerIP = "172.128.35.57" },
    @{ ServerIP = "172.128.35.58" }
    # 继续添加更多服务器...
)

# 初始化数组存储查询结果
$results = @()

# 遍历每台服务器执行查询
foreach ($server in $targetServers) {
    $smmip = $server.ServerIP
    $hostName = "无法获取主机名"
    $fwVersion = "查询失败"

    try {
        # 获取主机名
        $uri = "https://$smmip/redfish/v1/Systems/System.Embedded.1"
        $result = Invoke-WebRequest -Uri $uri -Credential $credential -Method Get -UseBasicParsing -ErrorAction Stop -Headers @{"Accept"="application/json"}
        $get_host = $result.Content | ConvertFrom-Json
        $hostName = $get_host.HostName

        # 获取iDRAC固件版本
        $expand_query ='?$expand=*($levels=1)'
        $uri = "https://$smmip/redfish/v1/UpdateService/FirmwareInventory$expand_query"
        $result = Invoke-WebRequest -Uri $uri -Credential $credential -Method Get -UseBasicParsing -ErrorAction Stop -Headers @{"Accept"="application/json"}
        $get_fw_inventory = $result.Content | ConvertFrom-Json
        $fwInfo = $get_fw_inventory.Members | Where-Object {($_.Id -like "Installed*") -and ($_.Name -eq "Integrated Remote Access Controller")} | Select-Object -First 1
        if ($fwInfo) {
            $fwVersion = $fwInfo.Version
        } else {
            $fwVersion = "未找到iDRAC固件信息"
        }
    } catch {
        Write-Warning "服务器 $smmip 查询失败:$($_.Exception.Message)"
    }

    # 将结果添加到数组
    $results += [PSCustomObject]@{
        'Host Name' = $hostName
        'Name' = "Integrated Remote Access Controller"
        'Version' = $fwVersion
    }
}

# 输出格式化表格
$results | Format-Table -AutoSize

关键说明

  • 服务器列表管理:用哈希表数组$targetServers存储每台服务器的IP,你可以轻松添加/删除目标机器;如果不同服务器的iDRAC账号密码不一样,直接把Username和Password字段加到对应的哈希表里,在循环里单独生成凭证即可。
  • 错误处理:加入try/catch块,个别服务器连接失败或查询出错时,脚本会继续执行其他服务器的查询,同时输出警告信息,不会直接中断。
  • 结果收集:用PSCustomObject整理每台服务器的查询结果,最后通过Format-Table输出你要的标准表格格式,和示例完全匹配。
  • 兼容性:完全适配PowerShell 5.1.14409.1018版本,没有用到高版本特性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 10:02:45