如何使用PowerShell获取多台计算机的RAM/内存详细信息?

如何使用PowerShell获取多台计算机的RAM/内存详细信息?,powershell,csv,ram,Powershell,Csv,Ram,我需要清点文本文件中列出的计算机的RAM。我有这个剧本: $($servers = Get-Content D:\123.txt Foreach ($s in $servers) { $s get-wmiobject Win32_Processor -ComputerName $s -ErrorAction SilentlyContinue| select Name | Format-List Get-WmiObject win32_baseboard -Compute

我需要清点文本文件中列出的计算机的RAM。我有这个剧本:

$($servers = Get-Content D:\123.txt

Foreach ($s in $servers) {
    $s
    get-wmiobject Win32_Processor -ComputerName $s -ErrorAction SilentlyContinue| select Name | Format-List
    Get-WmiObject win32_baseboard -ComputerName $s -ErrorAction SilentlyContinue| Select product | Format-List

    $colRAM = Get-WmiObject -Class "win32_PhysicalMemory" -namespace "root\CIMV2" -computerName $s

    $colRAM | ForEach {
           “Memory Installed: ” + $_.DeviceLocator
           “Memory Size: ” + ($_.Capacity / 1GB) + ” GB”       
           $SlotsFilled = $SlotsFilled + 1
           $TotMemPopulated = $TotMemPopulated + ($_.Capacity / 1GB)
    }

    Write-Host "_____________________________________ "
}) *>&1 > output.txt
返回以下结果:

计算机1

名称:英特尔(R)核心(TM)2双CPU E8500@3.16GHz

邮品:DG31PR

已安装内存:J6H2内存大小:1 GB

我希望结果如下并导出到CSV:

Name      TotalRam      Type      Motherboard
comp1     2gb           ddr3      h81m-k
comp2     2gb           ddr3      h81m-k
          2gb
comp3     1gb           ddr2      DG31PR
          0,5gb

这是脚本的修改版本,用于获得您请求的结果

#For more types https://msdn.microsoft.com/en-us/library/aa394347(v=vs.85).aspx
$memtype = @{
    0 = 'Unknown'
    1 = 'Other'
    2 = 'DRAM'
    20 = 'DDR'
    21 = 'DDR-2'
    22= 'DDR2 FB-DIMM'
    24 = 'DDR3'
    25 = 'FBD2'
}

$Result = @()

$servers = Get-Content D:\123.txt


Foreach ($s in $servers) {

    $Motherboard = (Get-WmiObject win32_baseboard -ComputerName $s -ErrorAction SilentlyContinue).product 
    $colRAM = Get-WmiObject -Class "win32_PhysicalMemory" -namespace "root\CIMV2" -computerName $s

    $TotMemPopulated = 0
    $SlotsFilled = 0

    $colRAM | ForEach-Object {
        $SlotsFilled = $SlotsFilled + 1
        $TotMemPopulated = $TotMemPopulated + ($_.Capacity / 1GB)  

        $Props =[ordered]@{        
            Name = $s
            TotalRam = "$TotMemPopulated`gb"
            Type = $memtype[[int]$_.MemoryType]
            MotherBoard = $Motherboard
        }
        $Object = New-Object -TypeName PSCustomObject -Property $Props

    }

    $Result += $Object
}

$Result | Export-CSV RamReport.csv
说明:

$memtype
是一个哈希表,用于将数值
MemoryType
数字从
win32\u PhysicalMemory
WMI类转换为友好名称。根据您环境中RAM的不同,您可能需要向该哈希表添加更多引用(我提供了一个指向数字代码引用的链接)

$result
被定义为空数组,在脚本期间用于将结果整理到对象中

脚本将创建一个对象,名为
$object
,其中包含要整理的属性的哈希表,然后将每个对象添加到$result集合中。这是一个有序哈希表,因此我们尊重您在最终输出中请求的列顺序


最后,我们将
$result
导出到CSV,使用
导出CSV

效果非常好!但是我们可以定义内存的数量,例如2x1gb吗?您可以将其添加到$props:
numberofDIMM=$colRAM.Count
中,这样可以计算出有多少DIMM,但它不会告诉您DIMM的确切大小。为了确保这一点(因为它们可能不匹配),您需要单独记录每个dimm的大小。