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

# INSTANCE ID
function PnpDevice_InstanceIdArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCimSession = @{}
    $CimSession = $fakeBoundParameter["CimSession"]
    if($CimSession)
    {
        $optionalCimSession.CimSession = $CimSession
    }

    PnpDevice\Get-PnpDevice -FriendlyName "$wordToComplete*" @optionalCimSession |
        Sort-Object -Property FriendlyName |
        ForEach-Object {
            $ToolTip = "Name: {0} - Status: {1} - Class: {2} `nID: {3}" -f $_.FriendlyName,$_.Status,$_.Class,$_.InstanceId 
            New-CompletionResult -CompletionText $_.InstanceId -ToolTip $ToolTip -ListItemText $_.FriendlyName
        }
}
# FRIENDLY NAME
function PnpDevice_FriendlyNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCimSession = @{}
    $CimSession = $fakeBoundParameter["CimSession"]
    if($CimSession)
    {
        $optionalCimSession.CimSession = $CimSession
    }

    PnpDevice\Get-PnpDevice -FriendlyName "$wordToComplete*" @optionalCimSession |
        Sort-Object -Property FriendlyName |
        ForEach-Object {
            $ToolTip = "Name: {0} - Status: {1} - Class: {2} `nID: {3}" -f $_.FriendlyName,$_.Status,$_.Class,$_.InstanceId 
            New-CompletionResult -CompletionText $_.FriendlyName -ToolTip $ToolTip
        }
}
# CLASS
function PnpDevice_ClassArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCimSession = @{}
    $CimSession = $fakeBoundParameter["CimSession"]
    if($CimSession)
    {
        $optionalCimSession.CimSession = $CimSession
    }

    PnpDevice\Get-PnpDevice @optionalCimSession |
        Select-Object -Property Class -Unique |
        Sort-Object -Property Class |
        Where-Object {$_.Class -ne $null} |
        Where-Object {$_.Class -like "$wordToComplete*"} |
        ForEach-Object {
            $ToolTip = "Class: {0} - GUID: {1}" -f $_.Class,$_.ClassGuid
            New-CompletionResult -CompletionText $_.Class -ToolTip $ToolTip
        }
}
# PNPDEVICE PROPERTY KEYNAME
function PnpDevice_KeyNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalParameters = @{}
    $CimSession = $fakeBoundParameter["CimSession"]
    if($CimSession) { $optionalParameters.CimSession = $CimSession }
    $InstanceId = $fakeBoundParameter["InstanceId"]
    if($InstanceId) { $optionalParameters.InstanceId = $InstanceId }

    PnpDevice\Get-PnpDeviceProperty @optionalParameters |
        Where-Object {$_.KeyName -like "$wordToComplete*"} |
        Sort-Object -Property KeyName |
        ForEach-Object {
            $ToolTip = "KeyName: {0} - Data: {1} - Type: {2} `nID: {3}" -f $_.KeyName,$_.Data,$_.Type,$_.InstanceId 
            New-CompletionResult -CompletionText $_.KeyName -ToolTip $ToolTip
        }
}

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

# INSTANCEID
Register-ArgumentCompleter `
    -Command ('Disable-PnpDevice','Enable-PnpDevice','Get-PnpDevice','Get-PnpDeviceProperty') `
    -Parameter 'InstanceId' `
    -Description 'Complete Pnp Device names, for example: Get-PnpDevice -InstanceId <TAB>' `
    -ScriptBlock $function:PnpDevice_InstanceIdArgumentCompletion

# FRIENDLY NAME
Register-ArgumentCompleter `
    -Command ('Get-PnpDevice') `
    -Parameter 'FriendlyName' `
    -Description 'Complete Pnp Device names, for example: Get-PnpDevice -FriendlyName <TAB>' `
    -ScriptBlock $function:PnpDevice_FriendlyNameArgumentCompletion

# CLASS
Register-ArgumentCompleter `
    -Command ('Get-PnpDevice') `
    -Parameter 'Class' `
    -Description 'Complete Pnp Device names, for example: Get-PnpDevice -Class <TAB>' `
    -ScriptBlock $function:PnpDevice_ClassArgumentCompletion

# PNPDEVICE PROPERTY KEYNAME
Register-ArgumentCompleter `
    -Command ('Get-PnpDeviceProperty') `
    -Parameter 'KeyName' `
    -Description 'Complete Pnp Device keynames, for example: Get-PnpDeviceProperty -InstanceId <id> -KeyName <TAB>' `
    -ScriptBlock $function:PnpDevice_KeyNameArgumentCompletion