CimCmdlets.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
## CimCmdlets module Custom Completers ##

#
# .SYNOPSIS
#
# Complete the -QueryDialect argument to CimCmdlets cmdlets
#
function CimInstance_QueryDialectCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Write-Output CQL WQL | Where-Object {$_ -like "$wordToComplete*"} | ForEach-Object {
        New-CompletionResult $_ $_
    }
}


#
# .SYNOPSIS
#
# Complete the -Property argument to Get-CimInstance
#
function CimInstance_PropertyParameterCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $param = @{}
    $ns = $fakeBoundParameter['Namespace']
    $cn = $fakeBoundParameter['ComputerName']
    $cs = $fakeBoundParameter['CimSession']
    $cl = $fakeBoundParameter['ClassName']

    if($ns) {$param.Namespace = $ns}
    if($cn) {$param.ComputerName = $cn}
    if($cs) {$param.CimSession = $cs}
    if($cl) {$param.ClassName = $cl}

    (CimCmdlets\Get-CimClass @param).CimClassProperties.Name | Where-Object {$_ -like "$wordToComplete*"} | Sort-Object | Foreach-Object {
        New-CompletionResult $_ $_
    }
}


#
# .SYNOPSIS
#
# Complete the -ResultClassName argument to Get-CimAssociatedInstance
#
function CimInstance_CimAssociatedInstanceResultClassNameParameterCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $param = @{}
    $ns = $fakeBoundParameter['Namespace']
    $cn = $fakeBoundParameter['ComputerName']
    $ci = $fakeBoundParameter['CimInstance']
    $io = $fakeBoundParameter['InputObject']

    if($ns) {$param.Namespace = $ns}
    if($cn) {$param.ComputerName = $cn}
    if($ci) {$param.CimInstance = $ci}
    if($io) {$param.InputObject = $io}

    (CimCmdlets\Get-CimAssociatedInstance @param).CimClass.CimClassName | Where-Object {$_ -like "$wordToComplete*"} | Sort-Object | Foreach-Object {
        New-CompletionResult $_ $_
    }
}


Register-ArgumentCompleter `
    -Command ('Get-CimInstance','Invoke-CimMethod','Register-CimIndicationEvent','Remove-CimInstance,Set-CimInstance') `
    -Parameter 'QueryDialect' `
    -Description 'Complete the -QueryDialect argument to CimInstance cmdlets: Get-CimInstance -QueryDialect -QueryDialect <TAB>' `
    -ScriptBlock $function:CimInstance_QueryDialectCompleter


Register-ArgumentCompleter `
    -Command 'Get-CimInstance' `
    -Parameter 'Property' `
    -Description 'Complete the -Property argument to CimInstance cmdlets: Get-CimInstance -Class Win32_Process -Property <TAB>' `
    -ScriptBlock $function:CimInstance_PropertyParameterCompleter


Register-ArgumentCompleter `
    -Command 'Get-CimAssociatedInstance' `
    -Parameter 'ResultClassName' `
    -Description 'Complete the -ResultClassName argument to Get-CimAssociatedInstance: $disk = Get-CimInstance -Class Win32_LogicalDisk -Filter ''DriveType=3''; Get-CimAssociatedInstance -CimInstance $disk -ResultClassName <TAB>' `
    -ScriptBlock $function:CimInstance_CimAssociatedInstanceResultClassNameParameterCompleter