# encoding: ascii
# api: powershell
# title: Get-PerformanceCounter
# description: Get one or more performance counter objects.
# version: 0.1
# type: function
# author: tojo2000
# license: CC0
# function: Get-PerformanceCounter
# x-poshcode-id: 2475
# x-archived: 2017-05-22T05:15:12
# x-published: 2011-01-23T12:39:00
#
#
function Get-PerformanceCounter {
<#
.Synopsis
Gets one or more Performance Counter objects.
.Description
This function will use .NET to retrieve a single Counter or all counters
for a particular Instance and Category.
.Parameter category
The Performance Counter Category.
.Parameter instance
(optional) If the Category is Multi-Instance, the Instance chosen. For
example, if you are querying the LogicalDisk category, the Instance would
be the drive letter. When querying a Multi-Instance Category, this
option is mandatory.
.Parameter counter
(optional) The name of the Counter. If you specify this option, only one
counter will be returned.
.Parameter computer
(optional) The target computer.
.Example
# Get all Counters for Category LogicalDisk and Instance C:
Get-PerformanceCounter LogicalDisk C:
.Example
# Get only the Current Disk Queue Length counter
Get-PerformanceCounter LogicalDisk C: 'Current Disk Queue Length'
.Notes
NAME: Get-PerformanceCounter
AUTHOR: Tim Johnson <tojo2000@tojo2000.com>
.Links
http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountercategory.aspx
#>
param([Parameter(Mandatory = $true, Position = 0)]
[string]$category,
[Parameter(Mandatory = $false, Position = 1)]
[string]$instance,
[Parameter(Mandatory = $false, Position = 2)]
[string]$counter,
[Parameter(Mandatory = $false, Position = 3)]
[string]$computer)
try{
if ($computer) {
$perf = New-Object System.Diagnostics.PerformanceCounterCategory `
$category $computer
} else {
$perf = New-Object System.Diagnostics.PerformanceCounterCategory `
$category
}
} catch {
Write-Host 'ERROR: Failed to create new Category object'
Write-Host $Error[0].ToString()
return
}
if (-not ($instance -or $counter)) {
try {
return $perf.GetCounters()
} catch {
Write-Host 'Unable to list counters for that Category.'
Write-Host $Error[0].ToString()
return
}
}
if (-not $counter) {
try {
return $perf.GetCounters($instance)
} catch {
Write-Host 'Unable to list counters for that Category and Instance.'
Write-Host $Error[0].ToString()
return
}
}
if (-not $computer) {
$computer = '.'
}
if ($instance) {
try {
return New-Object System.Diagnostics.PerformanceCounter($category,
$counter,
$instance,
$computer)
} catch {
Write-Host 'Unable to retrieve that Counter.'
Write-Host $Error[0].ToString()
return
}
} else {
try {
return New-Object System.Diagnostics.PerformanceCounter($category,
$counter,
'',
$computer)
} catch {
Write-Host 'Unable to retrieve that Counter.'
Write-Host $Error[0].ToString()
return
}
}
}