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

使用PowerShell从AD OU采集硬件信息时重复本地数据的问题

问题解决:PowerShell AD指定OU计算机清单导出本地信息的修复

问题根源

你的脚本反复导出本地计算机信息,核心原因是所有硬件查询命令都没有指定远程计算机目标,默认会读取本地机器的数据。此外还有几处变量错误和属性提取问题,导致输出异常。

修正后的完整脚本

# 获取指定OU内的所有计算机
$computers = Get-ADComputer -Filter * -SearchBase "OU=myou,DC=mydc" | Select-Object -ExpandProperty Name

# 遍历每台计算机收集信息
$computers | ForEach-Object {
    $computer = $_
    # 测试计算机连通性
    if (Test-Connection -ComputerName $computer -Quiet -Count 1) {
        Write-Progress -Activity "记录硬件信息" -Status "正在检查 $computer"
        
        try {
            # 远程查询计算机基础信息(指定目标机)
            $computerInfo = Get-CimInstance Win32_ComputerSystem -ComputerName $computer -Property UserName, Name, Model -ErrorAction Stop
            $OSInfo = Get-CimInstance Win32_OperatingSystem -ComputerName $computer -Property Caption, Version -ErrorAction Stop
            $CPUInfo = Get-CimInstance Win32_Processor -ComputerName $computer -Property Name, Architecture -ErrorAction Stop
            $DriveInfo = Get-CimInstance Win32_Volume -ComputerName $computer -Filter 'DriveType = 3' | Where-Object DriveLetter -eq 'C:'
            $RAMInfo = Get-CimInstance CIM_PhysicalMemory -ComputerName $computer | Measure-Object -Property Capacity -Sum | ForEach-Object { [math]::Round($_.Sum / 1GB, 2) }
            $SerialInfo = Get-CimInstance Win32_BIOS -ComputerName $computer -Property SerialNumber -ErrorAction Stop

            # 构建输出对象
            [PSCustomObject]@{
                "计算机名"       = $computerInfo.Name
                "当前/最后用户名" = $computerInfo.UserName
                "型号"           = $computerInfo.Model
                "操作系统名称"   = $OSInfo.Caption
                "操作系统版本"   = $OSInfo.Version
                "CPU名称"        = $CPUInfo.Name
                "系统架构"       = switch ($CPUInfo.Architecture) {
                    0 { "x86" }
                    1 { "MIPS" }
                    2 { "Alpha" }
                    3 { "PowerPC" }
                    6 { "Itanium" }
                    9 { "x64" }
                    default { "未知" }
                }
                "C盘总容量(GB)"   = [math]::Round($DriveInfo.Capacity / 1GB, 2)
                "C盘剩余空间(GB)" = [math]::Round($DriveInfo.FreeSpace / 1GB, 2)
                "内存容量(GB)"   = $RAMInfo
                "序列号"         = $SerialInfo.SerialNumber
            }
        }
        catch {
            Write-Warning "无法获取 $computer 的信息:$($_.Exception.Message)"
        }
    }
    else {
        Write-Warning "$computer 离线,跳过"
    }
} | Export-Csv -Path "C:\temp\inventory2.csv" -Encoding UTF8 -NoTypeInformation

关键修改说明

  1. 指定远程目标:所有Get-CimInstance命令添加-ComputerName $computer,确保查询的是目标AD计算机而非本地
  2. 替换命令:用Get-CimInstance替代老旧的gwmi(WMI命令),兼容性更强且支持现代认证方式
  3. 变量修正:修复进度条中的$PC为实际循环变量$computer,补全未定义的架构变量并映射为可读名称
  4. 属性提取:序列号直接取SerialNumber属性值,避免导出对象类型数据
  5. 格式化优化:存储和内存容量保留两位小数,提升CSV可读性
  6. 错误处理:添加try/catch捕获远程访问错误,避免脚本中断,同时输出警告提示问题机器
  7. 效率提升:直接通过管道传递对象,去掉冗余的数组拼接操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 11:33:15