% free ram of remote machines
Solution 1
I came up with this fairly generalizable script, which gives a percentage of free ram. For percentage of used ram simply add $a=$b-$a
.
$a=0
$b=0
$strComputer = "localhost"
$a=Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *freePhysical* | Out-String
$b=Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *totalvisiblememory* | Out-String
$a = $a -replace '\D+(\d+)','$1'
$b = $b -replace '\D+(\d+)','$1'
[math]::Round($a/$b*10000)/100
Solution 2
Using the method of Connecting to WMI Remotely with PowerShell from https://msdn.microsoft.com/en-us/library/ee309377(v=vs.85).aspx
I have used .NET to Format Numbers example from here. https://technet.microsoft.com/en-us/library/ee692795.aspx
$Servers = @("localhost")
foreach ($Server in $Servers){
$OS = get-wmiobject -Namespace "root\cimv2" -Class Win32_OperatingSystem -Impersonation 3 -computername $Server
foreach ($Item in $OS){
$RAM = "{0:N6}" -f ($Item.TotalVisibleMemorySize)/1kB
$FREE = "{0:N6}" -f ($Item.FreePhysicalMemory)/1kB
$Server + "`t" + "{0:P0}" -f ($FREE/$RAM)
}
}
EDIT
To resolve issues with number formats across PS and .net versions you can use either the Decimal or Fixed point format rather than number to correct. https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-decimal-d-format-specifier
$Servers = @("localhost")
foreach ($Server in $Servers){
$OS = get-wmiobject -Namespace "root\cimv2" -Class Win32_OperatingSystem -Impersonation 3 -computername $Server
foreach ($Item in $OS){
$RAM = "{0:D6}" -f ($Item.TotalVisibleMemorySize)/1kB
$FREE = "{0:D6}" -f ($Item.FreePhysicalMemory)/1kB
$Server + "`t" + "{0:P0}" -f ($FREE/$RAM)
}
}
OR
$Servers = @("localhost")
foreach ($Server in $Servers){
$OS = get-wmiobject -Namespace "root\cimv2" -Class Win32_OperatingSystem -Impersonation 3 -computername $Server
foreach ($Item in $OS){
$RAM = "{0:F6}" -f ($Item.TotalVisibleMemorySize)/1kB
$FREE = "{0:F6}" -f ($Item.FreePhysicalMemory)/1kB
$Server + "`t" + "{0:P0}" -f ($FREE/$RAM)
}
}
Related videos on Youtube
Comments
-
user2305193 over 1 year
Reading on superuser/stackoverflow I couldn't come up with a script that acutally outputs the %-free ram (as seen in Windows task manager) of several remote machines (e.g. server1-server4). Here's what I have in terms of code, the platform should be windows, either CMD or powershell (or similar):
1) CMD, couldn't get the % of free ram (i.e. couldn't access the 'busy' ram to calculcate 'busy/total * 100'. source):
wmic /NODE:"servername" /USER:"yourdomain\administrator" OS GET FreePhysicalMemory
2) powershell (source), couldn't get the memory of remote machines (i.e. cannot get
Get-WmiObject
of the remote machine):$system = Get-WmiObject win32_OperatingSystem $totalPhysicalMem = $system.TotalVisibleMemorySize $freePhysicalMem = $system.FreePhysicalMemory $usedPhysicalMem = $totalPhysicalMem - $freePhysicalMem $usedPhysicalMemPct = [math]::Round(($usedPhysicalMem / $totalPhysicalMem) * 100,1)
any help appreciated
-
root over 6 yearsPossible duplicate, stackoverflow.com/questions/12250783/…. Also, your example Powershell code is attempting to gather disk space information, which isn't included in your question. If this is also a requirement, you should mention it. It would be helpful if you added specifics of how the commands you're trying are failing rather than saying "couldn't get the memory".
-
user2305193 over 6 yearsI looked at that post, however as far as I have researched you cannot use
Get-Counter -Counter
for subsequent % calculation. I basically copy-pasted the code from the sources (I was hoping an expert could help me out quickly, reasoning that this would be a common question) - but I will further comment the question.
-
-
user2305193 over 6 yearsdid not work out of the box for me:
Cannot convert value "20'823'476.000000" to type "System.Int32". Error: "Input string was not in a correct format." At line:8 char:9 + $RAM = "{0:N6}" -f ($Item.TotalVisibleMemorySize)/1kB + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger
-
Jonathan Scion over 6 years@user2305193 I have updated answer to hopefully overcome your issue.