Perfmon/Public/Get-PerformanceCounterLocalName.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function Get-PerformanceCounterLocalName
{
    param
    (
        [Parameter(Mandatory)]
        [UInt32]$ID,
        
        [string]$ComputerName = $env:COMPUTERNAME
    )
 
    $code = @'
    [DllImport("pdh.dll", SetLastError=true, CharSet=CharSet.Unicode)]
    public static extern UInt32 PdhLookupPerfNameByIndex(string szMachineName, uint dwNameIndex, System.Text.StringBuilder szNameBuffer, ref uint pcchNameBufferSize);
'@

 
    $buffer = New-Object System.Text.StringBuilder(1024)
    [UInt32]$bufferSize = $buffer.Capacity
 
    $t = Add-Type -MemberDefinition $code -PassThru -Name PerfCounter -Namespace Utility
    $rv = $t::PdhLookupPerfNameByIndex($ComputerName, $id, $buffer, [Ref]$bufferSize)
 
    if ($rv -eq 0)
    {
        $buffer.ToString().Substring(0, $bufferSize - 1)
    }
    else
    {
        throw 'Get-PerformanceCounterLocalName : Unable to retrieve localized name. Check computer name and performance counter ID.'
    }
}