functions/Get-DbaPfDataCollectorCounter.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function Get-DbaPfDataCollectorCounter {
    <#
        .SYNOPSIS
            Gets Performance Counters
 
        .DESCRIPTION
            Gets Performance Counters
 
        .PARAMETER ComputerName
            The target computer. Defaults to localhost.
 
        .PARAMETER Credential
            Allows you to login to $ComputerName using alternative credentials.
 
        .PARAMETER CollectorSet
            The Collector Set name
   
        .PARAMETER Collector
            The Collector name
    
        .PARAMETER Counter
            The Counter name - in the form of '\Processor(_Total)\% Processor Time'
     
        .PARAMETER InputObject
            Enables piped results from Get-DbaPfDataCollectorSet
 
        .PARAMETER EnableException
            By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
            This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
            Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
     
        .NOTES
            Tags: PerfMon
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
     
        .LINK
            https://dbatools.io/Get-DbaPfDataCollectorCounter
 
        .EXAMPLE
            Get-DbaPfDataCollectorCounter
     
            Gets all counters for all collector sets on localhost
 
        .EXAMPLE
            Get-DbaPfDataCollectorCounter -ComputerName sql2017
     
             Gets all counters for all collector sets on on sql2017
     
        .EXAMPLE
            Get-DbaPfDataCollectorCounter -ComputerName sql2017 -Counter '\Processor(_Total)\% Processor Time'
 
            Gets the '\Processor(_Total)\% Processor Time' counter on sql2017
     
        .EXAMPLE
            Get-DbaPfDataCollectorCounter -ComputerName sql2017, sql2016 -Credential (Get-Credential) -CollectorSet 'System Correlation'
     
            Gets all counters for 'System Correlation' Collector on sql2017 and sql2016 using alternative credentials
     
        .EXAMPLE
            Get-DbaPfDataCollectorSet -CollectorSet 'System Correlation' | Get-DbaPfDataCollector | Get-DbaPfDataCollectorCounter
     
            Gets all counters for 'System Correlation' Collector
    #>

    [CmdletBinding()]
    param (
        [DbaInstance[]]$ComputerName = $env:COMPUTERNAME,
        [PSCredential]$Credential,
        [Alias("DataCollectorSet")]
        [string[]]$CollectorSet,
        [Alias("DataCollector")]
        [string[]]$Collector,
        [string[]]$Counter,
        [parameter(ValueFromPipeline)]
        [object[]]$InputObject,
        [switch]$EnableException
    )
    begin {
        $columns = 'ComputerName', 'Name', 'DataCollectorSet', 'Counters', 'DataCollectorType', 'DataSourceName', 'FileName', 'FileNameFormat', 'FileNameFormatPattern', 'LatestOutputLocation', 'LogAppend', 'LogCircular', 'LogFileFormat', 'LogOverwrite', 'SampleInterval', 'SegmentMaxRecords'
    }
    process {
        if ($InputObject.Credential -and (Test-Bound -ParameterName Credential -Not)) {
            $Credential = $InputObject.Credential
        }
        
        if (-not $InputObject -or ($InputObject -and (Test-Bound -ParameterName ComputerName))) {
            foreach ($computer in $ComputerName) {
                $InputObject += Get-DbaPfDataCollector -ComputerName $computer -Credential $Credential -CollectorSet $CollectorSet -Collector $Collector
            }
        }
        
        if ($InputObject) {
            if (-not $InputObject.DataCollectorObject) {
                Stop-Function -Message "InputObject is not of the right type. Please use Get-DbaPfDataCollector"
                return
            }
        }
        
        foreach ($counterobject in $InputObject) {
            foreach ($countername in $counterobject.Counters) {
                if ($Counter -and $Counter -notcontains $countername) { continue }
                [pscustomobject]@{
                    ComputerName                          = $counterobject.ComputerName
                    DataCollectorSet                      = $counterobject.DataCollectorSet
                    DataCollector                         = $counterobject.Name
                    DataCollectorSetXml                   = $counterobject.DataCollectorSetXml
                    Name                                  = $countername
                    FileName                              = $counterobject.FileName
                    CounterObject                         = $true
                    Credential                            = $Credential
                } | Select-DefaultView -ExcludeProperty DataCollectorObject, Credential, CounterObject, DataCollectorSetXml
            }
        }
    }
}