Check counters values remotely using PowerShell

Today you will find out how to get counters details remotely from servers. Function is useful to check quickly if some specific counter values increased or was it just a temporary spike.

Function is based on a Get-Counter command:

$Memory=(Get-Counter -ComputerName $Server -ListSet memory).paths
Get-Counter -ComputerName $Server -Counter $Memory -SampleInterval 1 -MaxSamples $Samples

For more information about Get-Counter you can visit MS site – link.

In this function I added just basic counters:

– Memory
– Processor
– PhysicalDisk
– Network Interface

Number of samples from 1-10 for counters can be also specified:

[Parameter(Position=2, Mandatory = $false, HelpMessage="Provide number for samples (Default = 1, Range = 1-10)", ValueFromPipeline = $true)] 
[ValidateRange(1,10)]
[int]$Samples = "1" 

Usage:

Get-CounterStats
Get-CounterStats -Counter Memory
Get-CounterStats -Counter Memory -Samples 5
Get-CounterStats -Server DC01
Get-CounterStats -Server DC01 -Counter Processor
Get-CounterStats -Server DC01 -Counter Processor -Samples 3

Final script:


Function Get-CounterStats{
    [CmdletBinding()]

    Param 
    (
        [Parameter(Position=0, Mandatory = $false, HelpMessage="Provide server name (Default = Localhost)", ValueFromPipeline = $true)] 
        $Server = $env:computername,

        [Parameter(Position=1, Mandatory = $false, HelpMessage="Select counter (Memory, Processor, PhysicalDisk, Network Interface)", ValueFromPipeline = $true)][ValidateSet("Memory", "Processor", "PhysicalDisk", "Network Interface")][string]
        $Counter = "All",

        [Parameter(Position=2, Mandatory = $false, HelpMessage="Provide number for samples (Default = 1, Range = 1-10)", ValueFromPipeline = $true)] 
        [ValidateRange(1,10)]
        [int]$Samples = "1"
    )
 
    If($Counter -eq "All")
    {
        Try
        {
            Write-Host "Checking memory"  -ForegroundColor Yellow
            $Memory=(Get-Counter -ComputerName $Server -ListSet memory).paths
            $MEMCounters = Get-Counter -ComputerName $Server -Counter $Memory -SampleInterval 1 -MaxSamples $Samples
            $MEMCounters | % { $_.counterSamples } | Sort-Object CookedValue -Descending| ft timestamp, path, @{l="Value (MB)";e={[math]::round($_.cookedvalue/1MB,1)}} -Wrap –AutoSize

            Write-Host "Checking cpu"  -ForegroundColor Yellow
            $CPU=(Get-Counter -ComputerName $Server -ListSet Processor).paths
            $CPUCounters = Get-Counter -ComputerName $Server -Counter $CPU -SampleInterval 1 -MaxSamples $Samples
            $CPUCounters | % { $_.counterSamples } | Sort-Object CookedValue -Descending | ft timestamp, path, @{l="Value (MB)";e={[math]::round($_.cookedvalue/1MB,1)}} -Wrap –AutoSize

            Write-Host "Checking PhysicalDisk"  -ForegroundColor Yellow
            $Disk=(Get-Counter -ComputerName $Server -ListSet PhysicalDisk).paths
            $DiskCounters = Get-Counter -ComputerName $Server -Counter $Memory -SampleInterval 1 -MaxSamples $Samples
            $DiskCounters | % { $_.counterSamples } | Sort-Object CookedValue -Descending| ft timestamp, path, @{l="Value (MB)";e={[math]::round($_.cookedvalue/1MB,1)}} -Wrap –AutoSize

            Write-Host "Checking Network Interface"  -ForegroundColor Yellow
            $Net=(Get-Counter -ComputerName $Server -ListSet "Network Interface").paths
            $NetCounters = Get-Counter -ComputerName $Server -Counter $CPU -SampleInterval 1 -MaxSamples $Samples
            $NetCounters | % { $_.counterSamples } | Sort-Object CookedValue -Descending | ft timestamp, path, @{l="Value (MB)";e={[math]::round($_.cookedvalue/1MB,1)}} -Wrap –AutoSize
        }
        Catch
        {
            $_.Exception.Message
            Continue
        }
    }
    Else
    {
       Try
        {
            Write-Host "Checking $Counter" -ForegroundColor Yellow
            $CounterSelection=(Get-Counter -ComputerName $Server -ListSet $Counter).paths
            $CounterResult = Get-Counter -ComputerName $Server -Counter $CounterSelection -SampleInterval 1 -MaxSamples $Samples
            $CounterResult | % { $_.counterSamples } | Sort-Object CookedValue -Descending| ft timestamp, path, @{l="Value (MB)";e={[math]::round($_.cookedvalue/1MB,1)}} -Wrap –AutoSize
        }
        Catch
        {
            $_.Exception.Message
            Continue
        }
    }
     
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.