![](/img/trans.png)
[英]In Powershell, how can I correctly use Get-CimInstance to retrieve the IPV6 addresses of respective computers?
[英]How can I use PowerShell to get RAM / Memory details of multiple computers?
我需要清點在文本文件中列出的計算機的 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
名稱 : Intel(R) Core(TM)2 Duo 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_PhysicalMemory
WMI 類轉換為友好名稱。 您可能需要根據環境中 RAM 的種類添加更多對此哈希表的引用(我提供了指向數字代碼引用的鏈接)。
$result
被定義為一個空數組,它在腳本期間用於將結果整理到一個對象中。
該腳本使用您希望整理的屬性的哈希表創建一個對象作為$object
,然后將每個對象添加到 $result 集合中。 這是一個有序的哈希表,因此我們尊重您在最終輸出中請求的列順序。
最后,我們使用Export-CSV
將$result
Export-CSV
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.