Hyper-V.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
## Hyper-V module Custom Completers ##

#
# .SYNOPSIS
#
# Complete the -VMName argument to Get-VM
#
function HyperV_VMNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCn = @{}
    $cn = $fakeBoundParameter["ComputerName"]
    if($cn)
    {
        $optionalCn.ComputerName = $cn
    }

    Hyper-V\Get-VM -Name "$wordToComplete*" @optionalCn |
        Sort-Object |
        ForEach-Object {
            $toolTip = "State: $($_.State) Status: $($_.Status)"
            New-CompletionResult $_.Name $toolTip
        }
}


#
# .SYNOPSIS
#
# Complete the -Name or -VMSwitch argument to various Hyper-V cmdlets.
#
function HyperV_VMSwitchArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $optionalCn = @{}
    $cn = $fakeBoundParameter["ComputerName"]
    if($cn)
    {
        $optionalCn.ComputerName = $cn
    }

    Hyper-V\Get-VMSwitch -Name "$wordToComplete*" @optionalCn |
        Sort-Object |
        ForEach-Object {
            $tooltip = "Description: $($_.NetAdapterInterfaceDescription)"
            New-CompletionResult $_.Name $tooltip
        }
}


#
# .SYNOPSIS
#
# Complete the -Name argument to *-VMIntegrationService cmdlets
#
function HyperV_VMIntegrationServiceNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $vm = $fakeBoundParameter["VMName"]
    if (!$vm)
    {
        return
    }

    $optionalCn = @{}
    $cn = $fakeBoundParameter["ComputerName"]
    if($cn)
    {
        $optionalCn.ComputerName = $cn
    }

    Hyper-V\Get-VMIntegrationService -Name "$wordToComplete*" -VMName $vm @optionalCn |
        Sort-Object |
        ForEach-Object {
            # TODO - need a tooltip
            New-CompletionResult $_.Name
        }
}


#
# .SYNOPSIS
#
# Complete vhd/vhdx files for -Path and -ParentPath parameters to *-VHD commands.
#
function HyperV_VHDPathArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-CompletionWithExtension $lastWord ('.vhd', '.vhdx')
}


#
# .SYNOPSIS
#
# Tab-complete for -VMNetworAdapter -Name when -VMName is provided.
#
function HyperV_VMNetworkAdapterNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $vm = $fakeBoundParameter['VMName']
    if ($vm)
    {
        Hyper-V\Get-VMNetworkAdapter -VMName $vm -Name "$wordToComplete*" |
            Sort-Object -Property Name |
            ForEach-Object {
                $toolTip = "MAC: {0} Connected to: {1}" -f $_.MacAddress, $_.SwitchName
                New-CompletionResult $_.Name $toolTip
            }
    }
}


Register-ArgumentCompleter `
    -Command ('Checkpoint-VM','Compare-VM','Debug-VM','Export-VM','Get-VM','Measure-VM','Move-VM','New-VM','Remove-VM','Rename-VM','Restart-VM','Resume-VM','Save-VM','Set-VM','Start-VM','Stop-VM','Suspend-VM') `
    -Parameter 'Name' `
    -Description 'Complete VM names, for example: Get-VM -Name <TAB>' `
    -ScriptBlock $function:HyperV_VMNameArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Add-VMDvdDrive','Add-VMFibreChannelHba','Add-VMHardDiskDrive','Add-VMNetworkAdapter','Add-VMNetworkAdapterAcl','Add-VMNetworkAdapterExtendedAcl','Add-VmNetworkAdapterRoutingDomainMapping','Add-VMRemoteFx3dVideoAdapter','Add-VMScsiController','Add-VMSwitchExtensionPortFeature','Complete-VMFailover','Connect-VMNetworkAdapter','Disable-VMIntegrationService','Disable-VMResourceMetering','Disconnect-VMNetworkAdapter','Enable-VMIntegrationService','Enable-VMReplication','Enable-VMResourceMetering','Export-VMSnapshot','Get-VMBios','Get-VMComPort','Get-VMConnectAccess','Get-VMDvdDrive','Get-VMFibreChannelHba','Get-VMFirmware','Get-VMFloppyDiskDrive','Get-VMHardDiskDrive','Get-VMIdeController','Get-VMIntegrationService','Get-VMMemory','Get-VMNetworkAdapter','Get-VMNetworkAdapterAcl','Get-VMNetworkAdapterExtendedAcl','Get-VMNetworkAdapterFailoverConfiguration','Get-VmNetworkAdapterIsolation','Get-VMNetworkAdapterRoutingDomainMapping','Get-VMNetworkAdapterVlan','Get-VMProcessor','Get-VMRemoteFx3dVideoAdapter','Get-VMReplication','Get-VMScsiController','Get-VMSnapshot','Get-VMSwitchExtensionPortData','Get-VMSwitchExtensionPortFeature','Grant-VMConnectAccess','Import-VMInitialReplication','Measure-VMReplication','Move-VMStorage','Remove-VMDvdDrive','Remove-VMFibreChannelHba','Remove-VMHardDiskDrive','Remove-VMNetworkAdapter','Remove-VMNetworkAdapterAcl','Remove-VMNetworkAdapterExtendedAcl','Remove-VMNetworkAdapterRoutingDomainMapping','Remove-VMRemoteFx3dVideoAdapter','Remove-VMReplication','Remove-VMSavedState','Remove-VMScsiController','Remove-VMSnapshot','Remove-VMSwitchExtensionPortFeature','Rename-VMNetworkAdapter','Rename-VMSnapshot','Reset-VMReplicationStatistics','Reset-VMResourceMetering','Restore-VMSnapshot','Resume-VMReplication','Revoke-VMConnectAccess','Set-VMBios','Set-VMComPort','Set-VMDvdDrive','Set-VMFibreChannelHba','Set-VMFirmware','Set-VMFloppyDiskDrive','Set-VMHardDiskDrive','Set-VMMemory','Set-VMNetworkAdapter','Set-VMNetworkAdapterFailoverConfiguration','Set-VmNetworkAdapterIsolation','Set-VmNetworkAdapterRoutingDomainMapping','Set-VMNetworkAdapterVlan','Set-VMProcessor','Set-VMRemoteFx3dVideoAdapter','Set-VMReplication','Set-VMSwitchExtensionPortFeature','Start-VMFailover','Start-VMInitialReplication','Stop-VMFailover','Stop-VMInitialReplication','Stop-VMReplication','Suspend-VMReplication','Test-VMNetworkAdapter') `
    -Parameter 'VMName' `
    -Description 'Complete VM names, for example: Set-VMMemory -VMName <TAB>' `
    -ScriptBlock $function:HyperV_VMNameArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Get-VMSwitch', 'Remove-VMSwitch', 'Rename-VMSwitch', 'Set-VMSwitch') `
    -Parameter 'Name' `
    -Description 'Complete switch names, for example: Get-VMSwitch -Name <TAB>' `
    -ScriptBlock $function:HyperV_VMSwitchArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Add-VMNetworkAdapter','Add-VMSwitchExtensionPortFeature','Add-VMSwitchExtensionSwitchFeature','Connect-VMNetworkAdapter','Get-VMNetworkAdapter','Get-VMSwitchExtensionPortData','Get-VMSwitchExtensionPortFeature','Get-VMSwitchExtensionSwitchData','Get-VMSwitchExtensionSwitchFeature','New-VM','Remove-VMNetworkAdapter','Remove-VMSwitchExtensionPortFeature','Remove-VMSwitchExtensionSwitchFeature','Set-VMSwitchExtensionPortFeature','Set-VMSwitchExtensionSwitchFeature') `
    -Parameter 'SwitchName' `
    -Description 'Complete switch names, for example: New-VM -SwitchName <TAB>' `
    -ScriptBlock $function:HyperV_VMSwitchArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Disable-VMIntegrationService','Enable-VMIntegrationService','Get-VMIntegrationService') `
    -Parameter 'Name' `
    -Description 'Complete integration service names, e.g. Get-VMIntegrationService -VMName myvm -Name <TAB>' `
    -ScriptBlock $function:HyperV_VMIntegrationServiceNameArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Convert-VHD','Dismount-VHD','Get-VHD','Merge-VHD','Mount-VHD','Optimize-VHD','Resize-VHD','Set-VHD','Test-VHD') `
    -Parameter 'Path' `
    -Description 'Completion VHD[X] files for various commands' `
    -ScriptBlock $function:HyperV_VHDPathArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Convert-VHD','New-VHD','Set-VHD') `
    -Parameter 'ParentPath' `
    -Description 'Completion VHD[X] files for various commands' `
    -ScriptBlock $function:HyperV_VHDPathArgumentCompletion


Register-ArgumentCompleter `
    -Command ('Connect-VMNetworkAdapter','Disconnect-VMNetworkAdapter','Get-VMNetworkAdapter','Remove-VMNetworkAdapter','Rename-VMNetworkAdapter','Set-VMNetworkAdapter','Test-VMNetworkAdapter') `
    -Parameter 'Name' `
    -Description 'Tab completes names of VM network adapaters, for example: Get-VMNetworkAdapter -VMName Foo -Name <TAB>' `
    -ScriptBlock $function:HyperV_VMNetworkAdapterNameArgumentCompletion