Hello powershell.org,
I’ve used this site a few times in the past but just now in a position to need to make a post. I’ll start off and say I’m not a developer and haven’t done much in the way of scripting in the last 15 years, so please bear that in mind. I’m a network engineer and machinist/mechanic so I’m much more familiar with the hands-on parts of IT.
With that out of the way, my boss has assigned me the task of generating a powershell script that will gather:
Current CPU, RAM, and disk usage
List of current processes and CPU/RAM used by each
Export this information to a CSV file
Below is what I have so far. It… sort of works. I’ll admit, 0% of this is my own work. It’s all stuff I’ve gathered from various sources on Google and tinkered with to try to make the CSV export work. The exported file has a lot of random junk in it that I don’t know how to get rid of. The last section is commented out at the moment, that’s another attempt at the process CPU/RAM usage that I was messing with but I couldn’t make it work either. At this point, I’ve spent 4.5 hours on this. I feel like someone that knows what they’re doing could definitely have finished this 5 times over in that time.
If anyone could please help make this work I would be very grateful.
[pre]$outfile = ‘C:\dump.csv’
if (Get-ChildItem $outfile) {
(Get-ChildItem $outfile).Delete()
}
#Get CPU usage
$CpuLoad = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average )| ConvertTo-Csv | Out-File $outfile -Append
#Get RAM usage
Get-Counter ‘\Memory\Available MBytes’ | ConvertTo-Csv | Out-File $outfile -Append
#Get HDD usage
Get-PSDrive | ConvertTo-Csv | Out-File $outfile -Append
#Processes with RAM and CPU usage
Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | Where-Object { $_.name -inotmatch '_total|idle' }
| ForEach-Object {
“Process={0,-25} CPU_Usage={1,-12} Memory_Usage_(MB)={2,-16}” -f `
$.Name,$.PercentProcessorTime,([math]::Round($_.WorkingSetPrivate/1Mb,2))
} | ConvertTo-Csv | Out-File $outfile -Append
#Get-Counter ‘\Process(*)% Processor Time’ `