Microsoft.PowerShell.Diagnostics.ArgumentCompleters.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

#
# .SYNOPSIS
#
# Complete the -Counter argument to Get-Counter cmdlet
#
function CounterParameterCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCn = @{}
    $cn = $fakeBoundParameter["ComputerName"]
    if($cn)
    {
        $optionalCn.ComputerName = $cn
    }

    (Get-Counter -ListSet * @optionalCn).Counter |
        Where-Object { $_ -like "$wordToComplete*" } |
        Sort-Object |
        ForEach-Object {
            # TODO: need a tooltip
            New-CompletionResult $_
        }
}


#
# .SYNOPSIS
#
# Complete the -ListSet argument to Get-Counter cmdlet
#
function ListSetParameterCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCn = @{}
    $cn = $fakeBoundParameter["ComputerName"]
    if($cn)
    {
        $optionalCn.ComputerName = $cn
    }

    Get-Counter -ListSet "$wordToComplete*" @optionalCn |
        Sort-Object CounterSetName |
        ForEach-Object {
            $tooltip = $_.Description
            New-CompletionResult $_.CounterSetName $tooltip
        }
}


#
# .SYNOPSIS
#
# Completes names of the logs for Get-WinEvent cmdlet.
#
function GetWinEvent_LogNameCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCn = @{}
    $cn = $fakeBoundParameter['ComputerName']
    if ($cn)
    {
        $optionalCn.ComputerName = $cn
    }

    Get-WinEvent -ListLog "$wordToComplete*" -Force @optionalCn |
        where { $_.IsEnabled } |
        Sort-Object -Property LogName |
        ForEach-Object {
            $toolTip = "Log $($_.LogName): $($_.RecordCount) entries"
            New-CompletionResult $_.LogName $toolTip
        }
}


#
# .SYNOPSIS
#
# Completes names of the logs for Get-WinEvent cmdlet.
#
function GetWinEvent_ListLogCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.GetLogNames() | Where-Object {$_ -like "*$wordToComplete*"} | Sort-Object | ForEach-Object {
        New-CompletionResult $_ $_
    }
}


#
# .SYNOPSIS
#
# Completes providers names for Get-WinEvent cmdlet.
#
function GetWinEvent_ListProviderCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.GetProviderNames() | Where-Object {$_ -like "*$wordToComplete*"} | Sort-Object | ForEach-Object {
        New-CompletionResult $_ $_
    }
}


Register-ArgumentCompleter `
    -Command 'Get-Counter' `
    -Parameter 'Counter' `
    -Description @'
Complete counter for the Get-Counter cmdlet, optionally on a remote machine. For example:
    Get-Counter -Counter <TAB>
    Get-Counter -cn 127.0.0.1 -Counter <TAB>
'@
 `
    -ScriptBlock $function:CounterParameterCompletion


Register-ArgumentCompleter `
    -Command 'Get-Counter' `
    -Parameter 'ListSet' `
    -Description @'
Complete counter sets for the Get-Counter cmdlet, optionally on a remote machine. For example:
    Get-Counter -ListSet <TAB>
    Get-Counter -cn 127.0.0.1 -ListSet <TAB>
'@
 `
    -ScriptBlock $function:ListSetParameterCompletion


Register-ArgumentCompleter `
    -Command 'Get-WinEvent' `
    -Parameter 'LogName' `
    -Description 'Completes names for the logs, for example: Get-WinEvent -LogName <TAB>' `
    -ScriptBlock $function:GetWinEvent_LogNameCompleter


Register-ArgumentCompleter `
    -Command 'Get-WinEvent' `
    -Parameter 'ListLog' `
    -Description 'Completes names for the logs, for example: Get-WinEvent -ListLog <TAB>' `
    -ScriptBlock $function:GetWinEvent_ListLogCompleter


Register-ArgumentCompleter `
    -Command 'Get-WinEvent' `
    -Parameter 'ListProvider' `
    -Description 'Completes names of the providers, for example: Get-WinEvent -ListProvider <TAB>' `
    -ScriptBlock $function:GetWinEvent_ListProviderCompleter