0

I am new to Stack Over flow and did not know how to start. I have around 1500 PC's in my organization What I want is to write a simple script in PowerShell that will get a list of computers from a text file as input, get the info of my function and save the info to a CSV file as output.

What I am trying to do finding RAM Module capacities For example if you type

WMIC /NODE:MachineName Memorychip get capacity

It will give you each module capacity size of remote PC; for example I have a system which has 4 RAM modules installed each of 8 GB so output will be as

Capacity
8589934592
8589934592
8589934592
8589934592

it means 4 slots and each is of 8 GB

What I want is to use this simple command to write a script either in command line or in PowerShell (PowerShell is preferred).

To be more specific, here is what I am trying right now

$Result = @()
$servers = Get-Content "L:\Data.txt"
Foreach ($s in $servers) {
    $Disk = (WMIC /NODE:$s MemoryChip Get Capacity) 
    $Object = New-Object -TypeName PSCustomObject -Property $Disk
    }
    $Result += $Object
}
$Result | Export-CSV "L:\RAMReport.csv" -NoTypeInformation -Encoding UTF8

I hope my question will be to the point

Thank you so much for the help

JosefZ
  • 28,460
  • 5
  • 44
  • 83
Ehsan
  • 3
  • 1
  • What's wrong on `Get-WmiObject -Class Win32_PhysicalMemory -ComputerName $s -Property Capacity` instead of `WMIC.exe`? Or (starting in PowerShell 3.0,) this cmdlet has been superseded by [`Get-CimInstance`](https://learn.microsoft.com/en-gb/powershell/module/cimcmdlets/get-ciminstance?view=powershell-5.1)? – JosefZ Oct 27 '21 at 19:37
  • Thank you for the comments, But the thing is it gives total capacity what i need is individual capacity of each RAM Slot I have System Center as well but it also gives total capacity which in my case not required – Ehsan Oct 28 '21 at 04:28
  • If there is a better way to get the desired results then it would be great – Ehsan Oct 28 '21 at 04:38

1 Answers1

0

Maybe the following (partially commented) script could lead to desired solution:

$servers = Get-Content "L:\Data.txt"
$Result = Foreach ($s in $servers) {
    $Disk = (WMIC /NODE:"""$s""" MemoryChip Get Capacity) |
        #                ↑↑  ↑↑ remove if $s is already a quoted string
        Where-Object {$_ -ne ''}
    $Prop = @{
        'server' = $s
        "$($Disk[0].Trim())MB" = ($Disk | 
            Select-Object -Skip 1 |
                ForEach-Object { [UInt64]$_ / 1Mb }) -join ' '
    }
    New-Object -TypeName PSCustomObject -Property $Prop
}
$Result # | Export-Csv "L:\RAMReport.csv" -NoTypeInformation -Encoding UTF8
#       ↑ uncomment to export data to a CSV file

Explanation:

  • $Result += $Object is very expensive array operation,
  • wmic /node:"srv-12" MemoryChip Get /Value requires double-quoted server name if contains some characters (e.g. Hyphen-Minus).

Output (without exporting data to a CSV file) could look as follows:

server   CapacityMB
------   ----------
server1  8192 8192
server_2 8192 8192 8192 8192
server-3 8192 
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Thank you so much Josefz for pointing me to the right direction. Yesterday night i applied this script from my AD and scans all the vlans, got the required results in the morning. – Ehsan Oct 29 '21 at 04:31
  • By the way, yesterday night i asked from Microsoft as well regarding System Center, and found out that i can see physical memory slot wise with capacity from Physical Memory Sub tree under Machine name Thanks again – Ehsan Oct 29 '21 at 04:39