We help IT Professionals succeed at work.

Script to retrieve installed RAM on remote machines from text file

John Davies
John Davies asked
on
Script to audit memory on a remote machine.

Does anyone know of or have a Powershell script that will loop through a text file (where i add computer names) and check the memory on each of these and write back to a CSV file with the headers - COMPUTERNAME | MEMORY

I have found scripts but I am unable to use Get-WmiObject for security reasons will not allow on any machine (Get-WmiObject : The RPC server is unavailable)

Any help would be appreciated

Thank you
Comment
Watch Question

Commented:
If you have security reasons now, it will have security reasons no matter what scripting or app you'll use. Solve the security issue first, then try again (usually, you need to be domain admin to run the script)
AlexA lack of information provides a lack of a decent solution.

Commented:
Can you use PSexec?

If not, use a machine startup script that outputs the details to a share somewhere. Can you use a GPO or not?

Author

Commented:
Hi Alex, cant use PSexec either. Also have an restricted admin account but cant change or edit GPO
AlexA lack of information provides a lack of a decent solution.

Commented:
So, you can't do anything with PSexec, you can't use powershell, you can't use WMIC, you can't use GPO.


What can you use?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Is Remote PowerShell available?
If not, then what you're trying to do is pretty much impossible, unless you start looking for vulnerabilities to hack the clients.
If you don't have permissions to access the clients, you can't be expected to generate reports about their configuration.
AlexA lack of information provides a lack of a decent solution.

Commented:
oBdA

If he can't call WMI objects, I'm 100% sure WinRM is disabled too, he may be able to pull the PSSession at a real push but I think it's highly unlikely.

try doing

Get-PSSession -computername "Computername of machine here"
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
WMI uses DCOM, not WinRM.
Try these, obviously with an account with administrative permissions on the target machine:
Get-CimInstance -ClassName Win32_PhysicalMemory -ComputerName RemoteMachine

Open in new window

Invoke-Command -ComputerName RemoteMachine -ScriptBlock {Get-CimInstance -ClassName Win32_PhysicalMemory}

Open in new window

AlexA lack of information provides a lack of a decent solution.

Commented:
Really!

I stand corrected, by you, yet again...... :(

I did google it too, you're right. I'll get back in my box :'(

Author

Commented:
This seems to be working, bizarely. Is there a way I get it to read a text file rather than me changing the COMPUTER NAME all the time?

[Cmdletbinding()] 
Param( 
    [string]$Computername = "COMPUTER NAME" 
) 
cls 
$PysicalMemory = Get-WmiObject -class "win32_physicalmemory" -namespace "root\CIMV2" -ComputerName $Computername 
 
Write-Host "Memore Modules:" -ForegroundColor Green 
$PysicalMemory | Format-Table Tag,BankLabel,@{n="Capacity(GB)";e={$_.Capacity/1GB}},Manufacturer,PartNumber,Speed -AutoSize 
 
Write-Host "Total Memory:" -ForegroundColor Green 
Write-Host "$((($PysicalMemory).Capacity | Measure-Object -Sum).Sum/1GB)GB" 
 
$TotalSlots = ((Get-WmiObject -Class "win32_PhysicalMemoryArray" -namespace "root\CIMV2" -ComputerName $Computername).MemoryDevices | Measure-Object -Sum).Sum 
Write-Host "`nTotal Memory Slots:" -ForegroundColor Green 
Write-Host $TotalSlots 
 
$UsedSlots = (($PysicalMemory) | Measure-Object).Count  
Write-Host "`nUsed Memory Slots:" -ForegroundColor Green 
Write-Host $UsedSlots 
 
If($UsedSlots -eq $TotalSlots) 
{ 
    Write-Host "All memory slots are filled up, none is empty!" -ForegroundColor Yellow 
} 

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try this; errors will end up in a separate column in the csv:
Function Get-PhysicalMemory {
[Cmdletbinding()] 
Param(
	[Parameter(ValueFromPipeline=$true)]
	[string]$Computername
)
	Process {
		Write-Verbose "Processing $($computer)"
		$out = [PSCustomObject]([ordered]@{
			ComputerName = $ComputerName
			'Memory (GB)' = $null
			'Total Memory Slots' = $null
			'Used Memory Slots' = $null
			Error = $null
		})
		Try {
			$physicalMemory = Get-WmiObject -Class "Win32_PhysicalMemory" -ComputerName $Computername -ErrorAction Stop
			$totalSlots = ((Get-WmiObject -Class "Win32_PhysicalMemoryArray" -ComputerName $Computername).MemoryDevices | Measure-Object -Sum).Sum 
			$out.'Memory (GB)' = ($physicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB
			$out.'Total Memory Slots' = $totalSlots
			$out.'Used Memory Slots' = @($physicalMemory).Count
		} Catch {
			$out.Error = $_.Exception.Message
		}
		$out
	}
}

Get-Content C:\Temp\ComputerList.txt | Get-PhysicalMemory -Verbose | Export-Csv -NoTypeInformation -Path C:\Temp\ComputerMemory.csv

Open in new window

Author

Commented:
oBdA, Wish I could script like that, thank you very much, works like a charm.