Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the CPU, memory usage and free disk space using powershell

Tags:

powershell

Could you help me combine these three scripts to a format that's like this

ComputerName     CPUUsageAverage    MemoryUsage    C PercentFree
xxx                           12            50%              30%

The way I execute this is: Get-content '.\servers.txt' | Get-CPUusageAverage

Here are the scripts:

CPU

Function Get-CPUusageAverage
{
$input|Foreach-Object{Get-WmiObject -computername $_ win32_processor | Measure-Object -property LoadPercentage -Average | Select Average}
}

Memory

    Function get-MemoryUsage
{
$input|Foreach-Object{
gwmi -Class win32_operatingsystem -computername $_ |
Select-Object @{Name = "MemoryUsage"; Expression = { “{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }
}
}
}

C PercentFree

Function get-CPercentFree
{
$input|ForEach-Object{
  Get-WmiObject -Class win32_Volume -ComputerName $_ -Filter "DriveLetter = 'C:'" |
  Select-object @{Name = "C PercentFree"; Expression = { “{0:N2}” -f (($_.FreeSpace / $_.Capacity)*100) } }
 } 
 }
like image 679
Dumont Avatar asked Aug 06 '13 21:08

Dumont


People also ask

How do I find the CPU and RAM usage using PowerShell?

In Windows PowerShell there is no exclusive cmdlet to find out the CPU and memory utilization rates. You can use the get-wmi object cmdlet along with required parameters to fetch the results.

Which command will calculate harddisk free space in PowerShell?

The below PowerShell function, Get-DiskSize, will create an object that carries the disk free space information.

How do I get CPU info in PowerShell?

Press on the keyboard the keys “Win” + “R”. In the Run window, type: dxdiag (without quotation marks). In the “DirectX Diagnostic Tool” window, in the “System” tab, general information about the computer processor will be displayed.


2 Answers

First I would avoid using $input. You can just combine the queries into single function that then outputs a pscustomobject with the data for each computer e.g.:

function Get-ComputerStats {
  param(
    [Parameter(Mandatory=$true, Position=0, 
               ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [string[]]$ComputerName
  )

  process {
    foreach ($c in $ComputerName) {
        $avg = Get-WmiObject win32_processor -computername $c | 
                   Measure-Object -property LoadPercentage -Average | 
                   Foreach {$_.Average}
        $mem = Get-WmiObject win32_operatingsystem -ComputerName $c |
                   Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)}
        $free = Get-WmiObject Win32_Volume -ComputerName $c -Filter "DriveLetter = 'C:'" |
                    Foreach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100)}
        new-object psobject -prop @{ # Work on PowerShell V2 and below
        # [pscustomobject] [ordered] @{ # Only if on PowerShell V3
            ComputerName = $c
            AverageCpu = $avg
            MemoryUsage = $mem
            PercentFree = $free
        }
    }
  }

 cat '.\servers.txt' | Get-ComputerStats | Format-Table
like image 87
Keith Hill Avatar answered Sep 21 '22 20:09

Keith Hill


How about:

cat '.\servers.txt' | % {select -property @{'n'='CPUUsageAverage';'e'={$_ | Get-CPUUsageAverage}},@{'n'='Memory Usage';'e'={$_ | Get-MemoryUsage}},@{'n'=C PercentFree';'e'={$_ | Get-CPercentFree}}}
like image 27
DavidN Avatar answered Sep 22 '22 20:09

DavidN