如何使用 PowerShell 获取多台计算机的 RAM/内存详细信息?
How can I use PowerShell to get RAM / Memory details of multiple computers?
我需要清点文本文件中列出的计算机的 RAM。我有这个脚本:
$($servers = Get-Content D:3.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
哪个returns这个结果:
computer1
Name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
product : DG31PR
Memory Installed: J6H2 Memory Size: 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:3.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_PhysicalMemory
WMI class 转换为友好名称。根据您环境中 RAM 的种类,您可能需要添加更多对此哈希表的引用(我已经为数字代码引用提供了 link)。
$result
被定义为一个空数组,在脚本期间使用它来将结果整理到一个对象中。
该脚本创建一个对象 $object
,其中包含您希望整理的属性的哈希表,然后将每个对象添加到 $result 集合中。这是一个有序哈希表,因此我们尊重您在最终输出中请求的列顺序。
最后我们使用 Export-CSV
.
将 $result
导出到 CSV
我需要清点文本文件中列出的计算机的 RAM。我有这个脚本:
$($servers = Get-Content D:3.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
哪个returns这个结果:
computer1
Name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
product : DG31PR
Memory Installed: J6H2 Memory Size: 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:3.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_PhysicalMemory
WMI class 转换为友好名称。根据您环境中 RAM 的种类,您可能需要添加更多对此哈希表的引用(我已经为数字代码引用提供了 link)。
$result
被定义为一个空数组,在脚本期间使用它来将结果整理到一个对象中。
该脚本创建一个对象 $object
,其中包含您希望整理的属性的哈希表,然后将每个对象添加到 $result 集合中。这是一个有序哈希表,因此我们尊重您在最终输出中请求的列顺序。
最后我们使用 Export-CSV
.
$result
导出到 CSV