简体   繁体   English

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

[英]How can I use PowerShell to get RAM / Memory details of multiple computers?

I need to inventory the RAM of the computers that are listed in a text file.我需要清点在文本文件中列出的计算机的 RAM。 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电脑1

Name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz名称 : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz

product : DG31PR产品 : DG31PR

Memory Installed: J6H2 Memory Size: 1 GB已安装内存:J6H2 内存大小:1 GB

I would like the result to be like this and exported to CSV:我希望结果是这样并导出到 CSV:

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

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. $memtype是一个哈希表,它将数字MemoryType编号从win32_PhysicalMemory WMI 类转换为友好名称。 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).您可能需要根据环境中 RAM 的种类添加更多对此哈希表的引用(我提供了指向数字代码引用的链接)。

$result is defined as an empty array, which is used during the script to collate the results in to an object. $result被定义为一个空数组,它在脚本期间用于将结果整理到一个对象中。

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.该脚本使用您希望整理的属性的哈希表创建一个对象作为$object ,然后将每个对象添加到 $result 集合中。 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 .最后,我们使用Export-CSV$result Export-CSV

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Powershell 中,如何正确使用 Get-CimInstance 来检索各个计算机的 IPV6 地址? - In Powershell, how can I correctly use Get-CimInstance to retrieve the IPV6 addresses of respective computers? Windows 7-使用Powershell脚本获取RAM详细信息 - Windows 7 - Get RAM details using powershell script 如何获取PowerShell脚本以删除其他计算机上的用户? - How do I get PowerShell script to delete users on other computers? 如何在PowerShell中将所有Active Directory计算机读入阵列? - How can I read all Active Directory computers into an array in PowerShell? 如何使用PowerShell或cmd“目录”获取多个但相似路径的内容? - How can I use PowerShell or a cmd “dir” to get the contents of multiple, but similar paths? 如何使用Powershell在Windows Server中以csv格式获取磁盘详细信息 - How can i get the disk details in csv format in windows server using powershell 如何获取当前PowerShell DSC正在执行的所有操作的详细信息? - How can I get details of all operations being performed by current PowerShell DSC? PowerShell - 如何从 PowerShell 获得我的 exe 程序的内存消耗峰值 - PowerShell - how can I get memory consumption peak for my exe program from PowerShell 如何在所有域计算机上使用I Exchange命令? - How can use I Exchange Commands on all domain computers? 如何使用 powershell 对进程名称进行分组并显示使用的内存总和 - How can I use powershell to group process names and show the sum of memory used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM