-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo-AutoCompleter.ps1
65 lines (47 loc) · 1.94 KB
/
Demo-AutoCompleter.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
#demo auto completion
return "This is a walk through demo"
Get-Command Register-ArgumentCompleter
help Register-ArgumentCompleter -full
#add completion to a command
# Get-volume -CimSession "<tab>"
Register-ArgumentCompleter -CommandName Get-Volume -ParameterName CimSession -ScriptBlock {
Get-content .\computers.txt
}
# Get-WinEvent -logname
Register-ArgumentCompleter -CommandName Get-WinEvent -ParameterName Logname -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
(Get-Winevent -listlog "$wordtoComplete*").logname | Sort-Object |
foreach-object {
# completion text,listitem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# Get-WinEvent -Logname <tab>
Register-ArgumentCompleter -CommandName Get-Command -ParameterName Verb -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
Get-Verb "$wordToComplete*" |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Verb, $_.Verb, 'ParameterValue', ("Group: $($_.Group)"))
}
}
# Get-Command -verb <tab>
#add to your own code
Function Get-ServiceStatus {
[cmdletbinding()]
Param([string]$Computername = $env:COMPUTERNAME)
$p = @{
Computername = $computername
ClassName = "Win32_service"
Filter = "StartMode ='Auto' AND State<>'Running'"
}
Get-Ciminstance @p
}
# Get-ServiceStatus -computername <tab>
Register-ArgumentCompleter -CommandName Get-ServiceStatus -ParameterName Computername -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
Get-content c:\scripts\company.txt | where {$_ -like "*$wordtocomplete*"} |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_.Trim(), $_.Trim(), 'ParameterValue', $_.trim())
}
}
# Get-ServiceStatus -computername <tab>