Microsoft.PowerShell.LocalAccounts.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
# Argument completer by Kurt Roggen [BE] - kurtroggen.be - @roggenk
# Microsoft.PowerShell.LocalAccounts module (Windows 10/Windows Server 2016)
# Supports cmdlets
# - *-LocalUser cmdlets (Parameters -SID)
# - *-LocalGroup cmdlets (Parameters -Name, -SID, -Group)
# - *-LocalGroupMember cmdlets (Parameters -Member)

# ARGUMENT COMPLETER FUNCTIONS #################################################

# LOCALUSER ###############
# LOCALUSER -Name
function LocalAccounts_LocalUserNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Microsoft.PowerShell.LocalAccounts\Get-LocalUser -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - SID: {1} `nEnabled: {2}" -f $_.Name,$_.Sid ,$_.Enabled
            New-CompletionResult -CompletionText $_.Name -ToolTip $ToolTip
        }
}

# LOCALUSER -SID
function LocalAccounts_LocalUserSIDArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Microsoft.PowerShell.LocalAccounts\Get-LocalUser -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - SID: {1} `nEnabled: {2}" -f $_.Name,$_.Sid ,$_.Enabled
            New-CompletionResult -CompletionText $_.SID -ToolTip $ToolTip -ListItemText $_.Name
        }
}

# LOCALGROUP ###############
# LOCALGROUP -NAME
function LocalAccounts_LocalGroupNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Microsoft.PowerShell.LocalAccounts\Get-LocalGroup -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - SID: {1}" -f $_.Name,$_.Sid 
            New-CompletionResult -CompletionText $_.Name -ToolTip $ToolTip
        }
}

# LOCALGROUP -SID
function LocalAccounts_LocalGroupSIDArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Microsoft.PowerShell.LocalAccounts\Get-LocalGroup -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - SID: {1}" -f $_.Name,$_.Sid 
            New-CompletionResult -CompletionText $_.SID -ToolTip $ToolTip -ListItemText $_.Name
        }
}

# LOCALGROUP -GROUP
function LocalAccounts_LocalGroupGroupArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Microsoft.PowerShell.LocalAccounts\Get-LocalGroup -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - SID: {1}" -f $_.Name,$_.Sid
            $CompletionText = "(Get-LocalGroup -Name '{0}')" -f $_.Name 
            New-CompletionResult -CompletionText $CompletionText -ToolTip $ToolTip -ListItemText $_.Name -NoQuotes
        }
}

# LOCALGROUPMEMBER -MEMBER
function LocalAccounts_LocalGroupMemberMemberArgumentCompletion    
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $GroupParams = @{}
    $LocalGroupName = $fakeBoundParameter["Name"]
    $LocalGroup     = $fakeBoundParameter["Group"]
    if($LocalGroupName) { $GroupParams.Name  = $LocalGroupName }
    if($LocalGroup)     { $GroupParams.Group = $LocalGroup }

    Microsoft.PowerShell.LocalAccounts\Get-LocalGroupMember @GroupParams |
        Where-Object {$_.Name -like "$wordToComplete*" } |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - SID: {1} `nType: {2} - Source: {3}" -f $_.Name,$_.Sid,$_.ObjectClass,$_.PrincipalSource
            New-CompletionResult -CompletionText $_.Name -ToolTip $ToolTip
        }
}

# ARGUMENT COMPLETER REGISTRATION ##############################################

# LOCALUSER ###############
# LOCALUSER -NAME
Register-ArgumentCompleter `
    -Command ('Disable-LocalUser','Enable-LocalUser','Get-LocalUser','Remove-LocalUser','Rename-LocalUser','Set-LocalUser') `
    -Parameter 'Name' `
    -Description 'Complete local user names, for example: Get-LocalUser -Name <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalUserNameArgumentCompletion
# LOCALUSER -SID
Register-ArgumentCompleter `
    -Command ('Disable-LocalUser','Enable-LocalUser','Get-LocalUser','Remove-LocalUser','Rename-LocalUser','Set-LocalUser') `
    -Parameter 'SID' `
    -Description 'Complete local user names, for example: Get-LocalUser -Name <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalUserSIDArgumentCompletion

# LOCALGROUP ###############
# LOCALGROUP -NAME
Register-ArgumentCompleter `
    -Command ('Get-LocalGroup','Remove-LocalGroup','Rename-LocalGroup','Set-LocalGroup') `
    -Parameter 'Name' `
    -Description 'Complete local group names, for example: Get-LocalGroup -Name <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalGroupNameArgumentCompletion
# LOCALGROUP -SID
Register-ArgumentCompleter `
    -Command ('Get-LocalGroup','Remove-LocalGroup','Rename-LocalGroup','Set-LocalGroup') `
    -Parameter 'SID' `
    -Description 'Complete local group names, for example: Get-LocalGroup -SID <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalGroupSIDArgumentCompletion

# LOCALGROUPMEMBER ###############
# LOCALGROUPMEMBER -NAME
Register-ArgumentCompleter `
    -Command ('Add-LocalGroupMember','Get-LocalGroupMember','Remove-LocalGroupMember') `
    -Parameter 'Name' `
    -Description 'Complete local group names, for example: Get-LocalGroupMember -Name <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalGroupNameArgumentCompletion
# LOCALGROUPMEMBER -SID
Register-ArgumentCompleter `
    -Command ('Add-LocalGroupMember','Get-LocalGroupMember','Remove-LocalGroupMember') `
    -Parameter 'SID' `
    -Description 'Complete local group names, for example: Get-LocalGroupMember -SID <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalGroupSIDArgumentCompletion
# LOCALGROUPMEMBER -MEMBER
Register-ArgumentCompleter `
    -Command ('Get-LocalGroupMember','Remove-LocalGroupMember') `
    -Parameter 'Member' `
    -Description 'Complete local group names, for example: Get-LocalGroupMember -Name <TAB>' `
    -ScriptBlock $function:LocalAccounts_LocalGroupMemberMemberArgumentCompletion