provider/vcomputer/Get-IcingaHypervVirtualSwitches.psm1

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
<#
.SYNOPSIS
    This function fetches all data for virtual switches, allowing to check for the state of virtual switches.
.DESCRIPTION
    This function fetches all data for virtual switches, allowing to check for the state of virtual switches.
.OUTPUTS
    System.Hashtable
.ROLE
    ### WMI Permissions

    * Root\Virtualization\v2
    * Root\Cimv2
.LINK
    https://github.com/Icinga/icinga-powershell-hyperv
#>

function Get-IcingaHypervVirtualSwitches()
{
    param(
        [array]$IncludeSwitches   = @(),
        [array]$ExcludeSwitches   = @(),
        [switch]$InternalSwitches = $FALSE,
        [switch]$ExternalSwitches = $FALSE
    );

    # Get all necessary information about virtual switches
    $VirtualSwithes           = Get-IcingaWindowsInformation -ClassName Msvm_VirtualEthernetSwitch -Namespace 'Root\Virtualization\v2';
    $InternalVirtualSwitches  = Get-IcingaWindowsInformation -ClassName Msvm_InternalEthernetPort -Namespace 'Root\Virtualization\v2';
    $ExternalVirtualSwitches  = Get-IcingaWindowsInformation -ClassName Msvm_ExternalEthernetPort -Namespace 'Root\Virtualization\v2';
    $VirtualSwitchesData      = @{ };

    # We iterate through all available internal and external virtual switches
    foreach ($switch in $VirtualSwithes) {
        $details = @{ };
        # Check, if there are still switches, which have to be filtered in
        if ($IncludeSwitches.Count -ne 0) {
            if (($IncludeSwitches -contains $switch.ElementName) -eq $FALSE) {
                continue;
            }
        }

        # Check, if there are still switches, which have to be filtered out
        if ($ExcludeSwitches.Count -ne 0) {
            if (($ExcludeSwitches -contains $switch.ElementName) -eq $TRUE) {
                continue;
            }
        }

        # We iterate through all available internal virtual switches
        foreach ($internal in $InternalVirtualSwitches) {
            # We continue if the internal switch name is not present in the outer loop
            if ($switch.ElementName -ne $internal.ElementName) {
                continue;
            }

            # Add some additional information about the virtual switches
            $details += @{
                'SupportedMaximumTransmissionUnit' = $internal.SupportedMaximumTransmissionUnit;
                'AutoSense'                        = $internal.AutoSense;
                'ActiveMaximumTransmissionUnit'    = $internal.ActiveMaximumTransmissionUnit;
                'FullDuplex'                       = $internal.FullDuplex;
                'LinkTechnology'                   = $internal.LinkTechnology;
                'NetworkAddresses'                 = $internal.NetworkAddresses;
                'OtherLinkTechnology'              = $internal.OtherLinkTechnology;
                'OtherNetworkPortType'             = $internal.OtherNetworkPortType;
                'PermanentAddress'                 = $internal.PermanentAddress;
                'PortNumber'                       = $internal.PortNumber;
                'MaxDataSize'                      = $internal.MaxDataSize;
                'MaxSpeed'                         = $internal.MaxSpeed;
                'OtherPortType'                    = $internal.OtherPortType;
                'PortType'                         = $internal.PortType;
                'RequestedSpeed'                   = $internal.RequestedSpeed;
                'Speed'                            = $internal.Speed;
                'UsageRestriction'                 = $internal.UsageRestriction;
            };

            # We iterate through all available external virtual switches
            foreach ($external in $ExternalVirtualSwitches) {
                # We continue if the external PermanentAddress does not match the internal Switch PermanentAddress
                if ($external.PermanentAddress -ne $internal.PermanentAddress) {
                    continue;
                }

                # We add some external switch specific information
                $details += @{
                    'Interface'            = $external.Name;
                    'InterfaceDescription' = $external.Description;
                    'InterfaceID'          = $external.DeviceID;
                    'IsBound'              = $external.IsBound;
                };

                if ($details.ContainsKey('SwitchType') -eq $FALSE) {
                    $details.Add('SwitchType', 'External');
                }

                # If the loop has not yet been skipped or interrupted, it can now be cancelled,
                # because we have all the information we need
                break;
            }

            # If the loop has not yet been skipped or interrupted, it can now be cancelled,
            # because we have all the information we need
            break;
        }

        # Check if the hashtable key already exists
        if ($details.ContainsKey('SwitchType') -eq $FALSE) {
            $details.Add('SwitchType', 'Internal');
        }

        # we filter out all external virtual switches, if the user only wants the internal one
        if ($InternalSwitches -and ($details.SwitchType -ne 'Internal')) {
            continue;
        }

        # we filter out all external virtual switches, if the user only wants the internal one
        if ($ExternalSwitches -and ($details.SwitchType -ne 'External')) {
            continue;
        }

        $details += @{
            'AvailableRequestedStates'    = $switch.AvailableRequestedStates;
            'Caption'                     = $switch.Caption;
            'CommunicationStatus'         = $switch.CommunicationStatus;
            'CreationClassName'           = $switch.CreationClassName;
            'Dedicated'                   = $switch.Dedicated;
            'Description'                 = $switch.Description;
            'DetailedStatus'              = $switch.DetailedStatus;
            'ElementName'                 = $switch.ElementName;
            'EnabledDefault'              = $switch.EnabledDefault;
            'EnabledState'                = $switch.EnabledState;
            'HealthState'                 = $switch.HealthState;
            'IdentifyingDescriptions'     = $switch.IdentifyingDescriptions;
            'InstallDate'                 = $switch.InstallDate;
            'InstanceID'                  = $switch.InstanceID;
            'MaxIOVOffloads'              = $switch.MaxIOVOffloads;
            'MaxVMQOffloads'              = $switch.MaxVMQOffloads;
            'Name'                        = $switch.Name;
            'NameFormat'                  = $switch.NameFormat;
            'OperatingStatus'             = $switch.OperatingStatus;
            'OperationalStatus'           = $switch.OperationalStatus;
            'OtherDedicatedDescriptions'  = $switch.OtherDedicatedDescriptions;
            'OtherEnabledState'           = $switch.OtherEnabledState;
            'OtherIdentifyingInfo'        = $switch.OtherIdentifyingInfo;
            'PowerManagementCapabilities' = $switch.PowerManagementCapabilities;
            'PrimaryOwnerContact'         = $switch.PrimaryOwnerContact;
            'PrimaryOwnerName'            = $switch.PrimaryOwnerName;
            'PrimaryStatus'               = $switch.PrimaryStatus;
            'RequestedState'              = $switch.RequestedState;
            'ResetCapability'             = $switch.ResetCapability;
            'Roles'                       = $switch.Roles;
            'Status'                      = $switch.Status;
            'StatusDescriptions'          = $switch.StatusDescriptions;
            'TimeOfLastStateChange'       = $switch.TimeOfLastStateChange;
            'TransitioningToState'        = $switch.TransitioningToState;
            'PSComputerName'              = $switch.PSComputerName;
        };

        $VirtualSwitchesData.Add($switch.Name, $details);
    }

    return $VirtualSwitchesData;
}