问题
I need to inventory the RAM of the computers that are listed in a text file. I have this script:
$($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
Which returns this result:
computer1
Name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
product : DG31PR
Memory Installed: J6H2 Memory Size: 1 GB
I would like the result to be like this and exported to CSV:
Name      TotalRam      Type      Motherboard
comp1     2gb           ddr3      h81m-k
comp2     2gb           ddr3      h81m-k
          2gb
comp3     1gb           ddr2      DG31PR
          0,5gb
    回答1:
This is a modified version of your script to get the result you requested.
#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
Explanation:
$memtype is a hashtable that converts the numeric MemoryType number from the win32_PhysicalMemory WMI class to the friendly name. You may need to add more references to this hashtable depending on the variety of RAM in your environment (I have provided a link to the numeric code references).
$result is defined as an empty array, which is used during the script to collate the results in to an object.
The script creates an object as $object with a hashtable of the properties you wished to collate and then adds each object to the $result collection. This is an ordered hashtable so that we respect the column order that you requested in the final output.
Finally we export $result to CSV using Export-CSV.
来源:https://stackoverflow.com/questions/43091085/how-can-i-use-powershell-to-get-ram-memory-details-of-multiple-computers