Getting output as infinite or Nan - powershell

I have tried the below command to get the memory usage of the computer but getting error as infinite or NaN.
$Totalsizemem=gwmi Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"
{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}
gwmi -computername localhost Win32_Process | Sort WorkingSetSize -Descending | Select
Name,#{n="Memoryusage(%)";Expression ={[math]::round(($_.WorkingSetSize / 1GB),1) /
$Totalsizemem*100}} | Select -First 10 | Format-Table -AutoSize | Out-Default
The expected output is -
Name Memoryusage(%)
---- --------------
powershell_ise.exe 0.655737704918033
explorer.exe 0.655737704918033
explorer.exe 0.655737704918033
explorer.exe 0.655737704918033
but the output i'm getting -
Name Memoryusage(%)
---- --------------
powershell_ise.exe ∞
svchost.exe ∞
explorer.exe NaN
svchost.exe NaN
explorer.exe NaN
Does anyone know how to rectify this?

You keep rounding and formatting the intermediate results, sacrificing accuracy to the point where your data is meaningless.
Keep it simple:
$Totalsizemem = Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum |Select -Expand Sum
Get-WmiObject Win32_Process |Sort WorkingSetSize -Descending |Select Name,#{Name='MemoryUsage(%)';Expression={$_.WorkingSetSize * 100 / $Totalsizemem}} -First 10

Related

Powershell script to list running processes and CPU utilization using Get-Counter is not returning all processes

I found the following PS command to list running processes sorted by CPU utilization:
Get-Counter -ErrorAction SilentlyContinue '\Process(*)\% Processor Time' |
Select-Object -ExpandProperty countersamples |
Select-Object -Property instancename, cookedvalue |
? {$_.instanceName -notmatch "^(idle|_total|system)$"} |
Sort-Object -Property cookedvalue -Descending |
Select-Object -First 10 |
ft InstanceName,#{L='CPU';E={($_.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize
Below is the output, which appears it could be accurate, however while this ran my Antimalware Service Executable was using about 8% CPU. Why is this not showing up in the PowerShell output?
InstanceName CPU
------------ ---
msmpeng 0.39%
slack 0.19%
taskmgr 0.19%
pwsh 0.10%
todo 0.10%
dwm 0.10%
system 0.00%
creative cloud helper 0.00%
adobe desktop service 0.00%
adobeipcbroker 0.00%
Edit: The answer here gives similar results. Here is my adaptation of the answer (so it outputs it to a table instead of CSV)
$Cores = (Get-WmiObject -class win32_processor -Property numberOfCores).numberOfCores;
$LogicalProcessors = (Get-WmiObject –class Win32_processor -Property NumberOfLogicalProcessors).NumberOfLogicalProcessors;
$TotalMemory = (get-ciminstance -class "cim_physicalmemory" | % {$_.Capacity})
get-process -IncludeUserName | select #{Name="Time"; Expression={(get-date(get-date).ToUniversalTime() -uformat "%s")}},`
ID, StartTime, Handles,WorkingSet, PeakPagedMemorySize, PrivateMemorySize, VirtualMemorySize,`
#{Name="Total_RAM"; Expression={ ($TotalMemory )}},`
CPU,
#{Name='CPU_Usage'; Expression = { $TotalSec = (New-TimeSpan -Start $_.StartTime).TotalSeconds
[Math]::Round( ($_.CPU * 100 / $TotalSec) /$LogicalProcessors, 2) }},`
#{Name="Cores"; Expression={ ($Cores )}},`
#{Name="Logical_Cores"; Expression={ ($LogicalProcessors )}},`
UserName, ProcessName, Path | Sort-Object -Property CPU -Descending | Format-Table -Property ProcessName, CPU | Select -First 10
And here are the results:
ProcessName CPU
----------- ---
SearchIndexer 2898.21875
MsMpEng 1395.9375
System 1045
MsSense 612.359375
explorer 507.296875
OneDrive 467.296875
OUTLOOK 362.765625
SenseNdr 355.8125
After some tests, these results are actually worse. I ran a CPU intensive App and it shows up with the first (original) method, but it shows 1.5% even though Task Manager shows 25%. The second method, from the other SO answer, does not show the process at all.

How to get Total CPU usage percentage without instance name in powershell

Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Average I will get output like Average -----------30
how can i get that value 30 only .
To get just the value, ccess the Average propert of Measure-Object's output. No need to pipe the results to Select-Object at all. Like so,
(Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average).Average
8

How to get output in GB instead of B

I found a script to check the available space on a remote computer. However I can't find how I can get the output in GB instead of B.
I don't know a lot about Powershell. Can somebody help me adjusting this code so I can see the available free disk space on a remote computer
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName STR3598C -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$disk.Size
$disk.FreeSpace
A possible way to achieve this could be to use the following:
gwmi win32_logicaldisk -ComputerName STR3598C -Filter "DeviceID='C:'" | Format-Table DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1GB,2)}},#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
Expected output:
DeviceId MediaType Size FreeSpace
-------- --------- ---- ---------
C: 12 215.52 111.1
Another way is to store the results in a variable (like with your code example) and format that for output to screen and/or save it in a CSV file:
$computer = 'STR3598C'
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $computer -Filter "DeviceID='C:'" |
Select-Object #{Name = 'Computer'; Expression = {$_.PSComputerName}},
DeviceID, VolumeName,
#{Name = 'Size'; Expression = {'{0:N2}' -f ($_.Size / 1GB)}},
#{Name = 'FreeSpace'; Expression = {'{0:N2} GB' -f ($_.FreeSpace / 1GB)}}
# output on screen
$disk | Format-Table -AutoSize
# or save the output as CVS
$disk | Export-Csv -Path 'D:\disksize.csv' -NoTypeInformation
Output on screen
Computer DeviceID VolumeName Size FreeSpace
-------- -------- ---------- ---- ---------
STR3598C C: Systeem 232,79 GB 89,33 GB

Getting RAM info - Powershell

I have a list of PC's that i need to get the Physical Memory and the amount of memory slots available. I have a list of PC's in a text file and i am using the "get-content" cmdlet. This is what I have:
Get-WmiObject -Class Win32_PhysicalMemory -ComputerName (Get-Content "C:\TextFile.txt") | ForEach-Object {[math]::truncate($_.capacity / 1GB)}
When I run this, only the Memory total is displayed. My 2 questions are:
1) How can get the PC name to show up next to the corresponding number?
and
2) How can i add the total memory slots available to this as well?
Thank You!
DeviceLocator tells you the slot.
Example
$memoryBySlot = Get-CimInstance -Class Win32_PhysicalMemory | Foreach-Object {
[PSCustomObject] #{
CapacityGB = $_.Capacity /1GB
DeviceLocator = $_.DeviceLocator
ComputerName = $env:COMPUTERNAME
}
}
$memoryBySlot | Out-String
$memoryBySlot | Measure-Object -Property CapacityGB -Sum | select count, sum, property
Results
CapacityGB DeviceLocator ComputerName
---------- ------------- ------------
16 DIMM1 MYMACHINE
16 DIMM2 MYMACHINE
16 DIMM3 MYMACHINE
16 DIMM4 MYMACHINE
Count Sum Property
----- --- --------
4 64 CapacityGB
You can try something like that :
Get-Content "C:\TextFile.txt" | % {$a = (Get-CimInstance -ClassName Win32_PhysicalMemory -ComputerName $_ | Measure-Object -Sum -Property Capacity).Sum / (1024*1024*1024); [PSCUStOMOBJECT]#{"Computer"=$_;"MEM"=$a}} |export-csv 'file.csv'

Can not get where-object & -gt to work together

I'm simply trying to filter out the variables of the "PeakWorkingSetSize" process so that the output will ONLY be greater than 5000. No matter what combination I try, the numbers won't filter. Please help.
get-wmiobject -class Win32_Process | where-object {$_.PeakWorkingSetSize -gt "5000"} | format-table -auto
FIXED thanks to #Matt!
I actually still don't know what the problem was. XD
Here is the code that works:
get-wmiobject -class Win32_Process | where-object {$_.PeakWorkingSetSize -gt "5000" | format-table -property Name,PeakWorkingSetSize -auto
There is nothing wrong with this query and it should run as you have it written. Format-Table would be making for some odd output size some of those properties do not translate to string very well. Your query and selecting some specific properties yields expected results for me. 5000 is a really low number though.
PS C:\Users\Matt> get-wmiobject -class Win32_Process | where-object {$_.PeakWorkingSetSize -gt "5000"} | select name,processid,peakworkingsetsize
name processid peakworkingsetsize
---- --------- ------------------
System 4 15588
csrss.exe 580 35412
csrss.exe 732 47356
services.exe 780 11820
winlogon.exe 812 8832
.... output truncated.....
If something is still wrong I think we need to see your expected output to understand what is wrong.