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

PowerShell服务器信息采集脚本问题排查与优化需求

PowerShell服务器信息采集脚本修复方案

原脚本存在三类问题,以下是修复后的完整脚本及问题说明:

修复后完整脚本

Import-Module ActiveDirectory

# 获取目标服务器列表
$servers = (Get-ADComputer -SearchBase "OU=OU" -Filter *).Name

# 定义远程执行的脚本块
$scriptblock = {
    $serverName = $env:COMPUTERNAME
    
    # 检测服务器状态
    $status = if (Test-Connection -ComputerName $serverName -Count 1 -Quiet) {
        'Up'
    }
    else {
        'Down'
    }

    # 获取格式化后的系统安装日期(MM/dd/yyyy)
    $os = Get-CimInstance -Class Win32_OperatingSystem
    $buildDate = Get-Date $os.InstallDate -Format "MM/dd/yyyy"

    # 获取所有本地计算机证书(LocalMachine\My存储区)
    $allCerts = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { !$_.PSIsContainer }
    $certInfo = $allCerts | ForEach-Object {
        "证书主题: $($_.Subject) | 生效时间: $($_.NotBefore.ToString('MM/dd/yyyy')) | 过期时间: $($_.NotAfter.ToString('MM/dd/yyyy'))"
    } -Join "`n"

    # 获取Trellix引擎最后更新时间
    $trellixPath = "C:\Program Files\Common Files\McAfee\Engine\AMCoreUpdater\amupdate.dat"
    $trellixDate = if (Test-Path $trellixPath) {
        Get-ItemProperty -Path $trellixPath | Select-Object -ExpandProperty LastWriteTime | Get-Date -Format "MM/dd/yyyy HH:mm"
    }
    else {
        "未找到Trellix更新文件"
    }

    # 获取所有本地磁盘信息并格式化百分比
    $diskInfo = Get-CimInstance -Class Win32_LogicalDisk -Filter 'DriveType = 3' | ForEach-Object {
        $freeSpaceGB = [math]::Round($_.FreeSpace / 1GB, 2)
        $diskSizeGB = [math]::Round($_.Size / 1GB, 2)
        $freePercent = [math]::Round(($_.FreeSpace / $_.Size) * 100, 2)
        "盘符: $($_.DeviceID) | 总容量: ${diskSizeGB}GB | 剩余空间: ${freeSpaceGB}GB | 剩余占比: ${freePercent}%"
    } -Join "`n"

    # 生成服务器信息对象
    [PSCustomObject]@{
        ServerName           = $serverName
        Status               = $status
        BuildDate            = $buildDate
        AllCertificates      = $certInfo
        TrellixLastUpdate    = $trellixDate
        AllDiskInfo          = $diskInfo
    }
}

# 远程执行脚本并收集结果
$allServerInfo = Invoke-Command -ComputerName $servers -ScriptBlock $scriptblock -ErrorAction SilentlyContinue

# 将结果导出为合并后的CSV
$allServerInfo | Export-Csv -Path "C:\NEC\CombinedFile.csv" -NoTypeInformation -Encoding UTF8

Write-Host "服务器信息已导出至 C:\NEC\CombinedFile.csv"

具体修复说明

1. 解决无法上报所有磁盘及全部证书的问题

  • 磁盘信息:替换原脚本仅保留最后一个磁盘的逻辑,遍历所有本地磁盘(DriveType=3),将每个磁盘的盘符、容量、剩余空间、百分比信息拼接为字符串,存储在AllDiskInfo字段中
  • 证书信息:遍历Cert:\LocalMachine\My存储区的所有证书,将每个证书的主题、生效/过期时间拼接为字符串,存储在AllCertificates字段中

2. 修复BuildDate格式问题

使用Get-Date命令将系统安装日期格式化为MM/dd/yyyy标准格式,替换原脚本直接输出$buildDate.datetime的逻辑

3. 格式化磁盘空间百分比

通过[math]::Round()函数将百分比保留两位小数,并添加%符号,同时对磁盘容量和剩余空间也做了小数位数优化

4. 优化脚本逻辑

  • 用Get-CimInstance替代Get-WmiObject(CIM是WMI的现代替代方案,性能更优)
  • 简化服务器状态检测逻辑,使用Test-Connection的-Quiet参数直接返回布尔值
  • 添加Trellix文件存在性校验,避免文件不存在时报错
  • 直接通过Invoke-Command收集所有远程执行结果,无需在远程服务器生成单独CSV再合并,提升效率
  • 确保结果按服务器名称自然分组(每个服务器对应一条记录,包含该服务器的所有磁盘和证书信息)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 00:42:37