Skip to content

Get-Counters

VMware: Retrieves available performance counters

#Requires -Version 5.1
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$VIServer,
    [Parameter(Mandatory = $true)]
    [pscredential]$VICredential,
    [string]$CounterName = '*',
    [ValidateSet('*', 'Name', 'UId', 'Fields')]
    [string[]]$Properties = @('Name', 'UId', 'Fields'),
    [switch]$ExpandFields
)
Process {
    try {
        if ($Properties -contains '*') { $Properties = @('*') }
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        if (-not $ExpandFields) {
            $result = Get-EsxTop -Server $vmServer -Counter -CounterName $CounterName -ErrorAction Stop | Select-Object $Properties
        }
        else {
            $result = Get-EsxTop -Server $vmServer -Counter -CounterName $CounterName -ErrorAction Stop | Select-Object -ExpandProperty Fields
        }
        if ($null -ne $result) { foreach ($item in $result) { $item | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force } }
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Name of the counter to retrieve; retrieves all if empty

List of properties to expand; use * for all

Off

Expand the Fields property

An interactive directory of PowerShell scripts.