问题描述
我需要盘点文本文件中列出的计算机的RAM.我有这个脚本:
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
哪个返回此结果:
名称:Intel(R)Core(TM)2 Duo CPU E8500 @ 3.16GHz
Name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
产品:DG31PR
已安装内存:J6H2内存大小:1 GB
Memory Installed: J6H2 Memory Size: 1 GB
我希望结果是这样并导出到CSV:
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
推荐答案
这是脚本的修改版本,可以获取您请求的结果.
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
说明:
$memtype
是一个哈希表,可将数字MemoryType
数字从win32_PhysicalMemory
WMI类转换为友好名称.您可能需要根据此环境中的RAM的种类向此哈希表添加更多引用(我提供了指向数字代码引用的链接).
$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
被定义为一个空数组,在脚本执行期间用于将结果整理到一个对象中.
$result
is defined as an empty array, which is used during the script to collate the results in to an object.
该脚本使用$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. This is an ordered hashtable so that we respect the column order that you requested in the final output.
最后,我们使用Export-CSV
将$result
导出为CSV.
Finally we export $result
to CSV using Export-CSV
.
这篇关于如何使用PowerShell获取多台计算机的RAM/内存详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!