Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Capture CPU & Memory - every x number of minutes, write to file - almost there?

$
0
0

Hello,

I'm trying to capture the cpu utilization and memory from a few servers and write the info to a file.

The function below I've worked on and I can't get to the next point of starting by specifying a list of servers, then append the data to a csv file, and lastly I'd schedule it as a task.

Can anyone assist?

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)
        [pscustomobject] [ordered] @{ #
            ComputerName = $c
            AverageCpu = $avg
            MemoryUsage = $mem
            PercentFree = $free
        }
    }
  }

 cat '.\servers.txt' | Get-ComputerStats | Format-Table

Viewing all articles
Browse latest Browse all 6937

Trending Articles