GroupPolicy.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
# ARGUMENT COMPLETER FUNCTIONS #################################################

# GPO
function GroupPolicy_GPONameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalDomain = @{}
    $Domain = $fakeBoundParameter["Domain"]
    if($Domain)
    {
        $optionalDomain.Domain = $Domain
    }

    GroupPolicy\Get-GPO -All @optionalDomain |
        Where-Object {$_.DisplayName -like "$wordToComplete*"} |
        Sort-Object -Property DisplayName |
        ForEach-Object {
            $ToolTip = "Name: {0} - GUID: {1}" -f $_.DisplayName,$_.Id 
            New-CompletionResult -CompletionText $_.DisplayName -ToolTip $ToolTip
        }
}
function GroupPolicy_GPOGUIDArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalDomain = @{}
    $Domain = $fakeBoundParameter["Domain"]
    if($Domain)
    {
        $optionalDomain.Domain = $Domain
    }

    GroupPolicy\Get-GPO -All @optionalDomain |
        Where-Object {$_.DisplayName -like "$wordToComplete*"} |
        Sort-Object -Property DisplayName |
        ForEach-Object {
            $ToolTip = "Name: {0} - GUID: {1}" -f $_.DisplayName,$_.Id 
            New-CompletionResult -CompletionText $_.Id -ToolTip $ToolTip -ListItemText $_.DisplayName
        }
}

# StarterGPO
function GroupPolicy_StarterGPONameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalDomain = @{}
    $Domain = $fakeBoundParameter["Domain"]
    if($Domain)
    {
        $optionalDomain.Domain = $Domain
    }

    GroupPolicy\Get-GPStarterGPO -All @optionalDomain | 
        Where-Object {$_.DisplayName -like "$wordToComplete*"} |
        Sort-Object -Property DisplayName |
        ForEach-Object {
            $ToolTip = "Name: {0} - GUID: {1}" -f $_.DisplayName,$_.Id 
            New-CompletionResult -CompletionText $_.DisplayName -ToolTip $ToolTip
        }
}
function GroupPolicy_StarterGPOGUIDArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalDomain = @{}
    $Domain = $fakeBoundParameter["Domain"]
    if($Domain)
    {
        $optionalDomain.Domain = $Domain
    }

    GroupPolicy\Get-GPStarterGPO -All @optionalDomain | 
        Where-Object {$_.DisplayName -like "$wordToComplete*"} |
        Sort-Object -Property DisplayName |
        ForEach-Object {
            $ToolTip = "Name: {0} - GUID: {1}" -f $_.DisplayName,$_.Id 
            New-CompletionResult -CompletionText $_.Id -ToolTip $ToolTip -ListItemText $_.DisplayName
        }
}

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

# GPO
Register-ArgumentCompleter `
    -Command ('Get-GPO','Get-GPOReport','Get-GPPermission','Get-GPPrefRegistryValue','Get-GPRegistryValue','Backup-GPO','New-GPLink','Remove-GPLink','Remove-GPO','Remove-GPPrefRegistryValue','Remove-GPRegistryValue','Rename-GPO','Set-GPLink','Get-GPPermission','Set-GPPrefRegistryValue','Set-GPRegistryValue') `
    -Parameter 'Name' `
    -Description 'Complete GPO names, for example: Get-GPO -Name <TAB>' `
    -ScriptBlock $function:GroupPolicy_GPONameArgumentCompletion

Register-ArgumentCompleter `
    -Command ('Get-GPO','Get-GPOReport','Get-GPPermission','Get-GPPrefRegistryValue','Get-GPRegistryValue','Backup-GPO','New-GPLink','Remove-GPLink','Remove-GPO','Remove-GPPrefRegistryValue','Remove-GPRegistryValue','Rename-GPO','Set-GPLink',,'Get-GPPermission','Set-GPPrefRegistryValue','Set-GPRegistryValue') `
    -Parameter 'GUID' `
    -Description 'Complete GPO names, for example: Get-GPO -GUID <TAB>' `
    -ScriptBlock $function:GroupPolicy_GPOGUIDArgumentCompletion

# Starter GPO
Register-ArgumentCompleter `
    -Command ('Get-GPStarterGPO') `
    -Parameter 'Name' `
    -Description 'Complete Starter GPO names, for example: Get-GPStarterGPO -Name <TAB>' `
    -ScriptBlock $function:GroupPolicy_StarterGPONameArgumentCompletion

Register-ArgumentCompleter `
    -Command ('Get-GPStarterGPO') `
    -Parameter 'GUID' `
    -Description 'Complete Starter GPO names, for example: Get-GPStarterGPO -GUID <TAB>' `
    -ScriptBlock $function:GroupPolicy_StarterGPOGUIDArgumentCompletion