DSCResources/cVMSwitch/cVMSwitch.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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [parameter(Mandatory)] [String]$Name, [parameter(Mandatory)] [ValidateSet("External","Internal","Private")] [String]$Type ) # Check if Hyper-V module is present for Hyper-V cmdlets if(!(Get-Module -ListAvailable -Name Hyper-V)) { Throw "Please ensure that Hyper-V role is installed with its PowerShell module" } $switch = Get-VMSwitch -Name $Name -SwitchType $Type -ErrorAction SilentlyContinue @{ Name = $switch.Name Type = $switch.SwitchType NetAdapterName = $( if($switch.NetAdapterInterfaceDescription){ (Get-NetAdapter -InterfaceDescription $switch.NetAdapterInterfaceDescription).Name}) AllowManagementOS = $switch.AllowManagementOS Ensure = if($switch){'Present'}else{'Absent'} Id = $switch.Id NetAdapterInterfaceDescription = $switch.NetAdapterInterfaceDescription EnableIov = $switch.IovEnabled MinimumBandwidthMode = $switch.BandwidthReservationMode } } function Set-TargetResource { [CmdletBinding()] param ( [parameter(Mandatory)] [String]$Name, [parameter(Mandatory)] [ValidateSet('External','Internal','Private')] [String]$Type, [Parameter()] [ValidateNotNullOrEmpty()] [String]$NetAdapterName, [Parameter()] [Boolean]$AllowManagementOS, [Parameter()] [Boolean]$EnableIov, [Parameter()] [ValidateSet('None', 'Default', 'Weight', 'Absolute')] [String] $MinimumBandwidthMode, [ValidateSet("Present","Absent")] [String]$Ensure = "Present" ) # Check if Hyper-V module is present for Hyper-V cmdlets if(!(Get-Module -ListAvailable -Name Hyper-V)) { Throw "Please ensure that Hyper-V role is installed with its PowerShell module" } if($Ensure -eq 'Present') { $switch = (Get-VMSwitch -Name $Name -SwitchType $Type -ErrorAction SilentlyContinue) # If switch is present and it is external type, that means it doesn't have right properties (TEST code ensures that) if($switch -and ($switch.SwitchType -eq 'External')) { Write-Verbose -Message "Checking switch $Name NetAdapterInterface ..." if((Get-NetAdapter -Name $NetAdapterName).InterfaceDescription -ne $switch.NetAdapterInterfaceDescription) { Write-Verbose -Message "Removing switch $Name and creating with right netadapter ..." $switch | Remove-VMSwitch -Force $parameters = @{} $parameters["Name"] = $Name $parameters["NetAdapterName"] = $NetAdapterName $parameters["MinimumBandwidthMode"] = $MinimumBandwidthMode if($PSBoundParameters.ContainsKey("AllowManagementOS")){$parameters["AllowManagementOS"]=$AllowManagementOS} $null = New-VMSwitch @parameters Write-Verbose -Message "Switch $Name has right netadapter $NetAdapterName" } else { Write-Verbose -Message "Switch $Name has right netadapter $NetAdapterName" } Write-Verbose -Message "Checking switch $Name AllowManagementOS ..." if($PSBoundParameters.ContainsKey("AllowManagementOS") -and ($switch.AllowManagementOS -ne $AllowManagementOS)) { Write-Verbose -Message "Switch $Name AllowManagementOS property is not correct" $switch | Set-VMSwitch -AllowManagementOS $AllowManagementOS Write-Verbose -Message "Switch $Name AllowManagementOS property is set to $AllowManagementOS" } else { Write-Verbose -Message "Switch $Name AllowManagementOS is correctly set" } } # If the switch is not present, create one else { Write-Verbose -Message "Switch $Name is not $Ensure." Write-Verbose -Message "Creating Switch ..." $parameters = @{} $parameters["Name"] = $Name if($NetAdapterName) { $parameters["NetAdapterName"] = $NetAdapterName $parameters["MinimumBandwidthMode"] = $MinimumBandwidthMode if($PSBoundParameters.ContainsKey("AllowManagementOS")) { $parameters["AllowManagementOS"] = $AllowManagementOS } } else { $parameters["SwitchType"] = $Type } $null = New-VMSwitch @parameters Write-Verbose -Message "Switch $Name is now $Ensure." } } # Ensure is set to "Absent", remove the switch else { Get-VMSwitch $Name -ErrorAction SilentlyContinue | Remove-VMSwitch -Force } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [parameter(Mandatory)] [String]$Name, [parameter(Mandatory)] [ValidateSet('External','Internal','Private')] [String]$Type, [Parameter()] [ValidateNotNullOrEmpty()] [String]$NetAdapterName, [Parameter()] [Boolean]$AllowManagementOS, [Parameter()] [Boolean]$EnableIov, [Parameter()] [ValidateSet('None', 'Default', 'Weight', 'Absolute')] [String] $MinimumBandwidthMode, [ValidateSet("Present","Absent")] [String]$Ensure = "Present" ) #region input validation # Check if Hyper-V module is present for Hyper-V cmdlets if(!(Get-Module -ListAvailable -Name Hyper-V)) { Throw "Please ensure that Hyper-V role is installed with its PowerShell module" } If($Type -eq 'External' -and !($NetAdapterName)) { Throw "For external switch type, NetAdapterName must be specified" } If($Type -ne 'External' -and $NetAdapterName) { Throw "For Internal or Private switch type, NetAdapterName should not be specified" } #endregion try { # Check if switch exists Write-Verbose -Message "Checking if Switch $Name is $Ensure ..." $switch = Get-VMSwitch -Name $Name -SwitchType $Type -ErrorAction Stop # If switch exists if($switch) { Write-Verbose -Message "Switch $Name is Present" # If switch should be present, check the switch type if($Ensure -eq 'Present') { # If switch is the external type, check additional propeties if($switch.SwitchType -eq 'External') { Write-Verbose -Message "Checking if Switch $Name has correct NetAdapterInterface ..." if((Get-NetAdapter -Name $NetAdapterName -ErrorAction SilentlyContinue).InterfaceDescription -ne $switch.NetAdapterInterfaceDescription) { return $false } else { Write-Verbose -Message "Switch $Name has correct NetAdapterInterface" } if($PSBoundParameters.ContainsKey("AllowManagementOS")) { Write-Verbose -Message "Checking if Switch $Name has AllowManagementOS set correctly..." if(($switch.AllowManagementOS -ne $AllowManagementOS)) { return $false } else { Write-Verbose -Message "Switch $Name has AllowManagementOS set correctly" } } if ($EnableIov) { if ($switch.IovEnabled -ne $EnableIov) { return $false } } if ($MinimumBandwidthMode -ne $switch.BandwidthReservationMode) { return $false } return $true } else { return $true } } # If switch should be absent, but is there, return $false else { return $false } } } # If no switch was present catch [System.Management.Automation.ActionPreferenceStopException] { Write-Verbose -Message "Switch $Name is not Present" return ($Ensure -eq 'Absent') } } Export-ModuleMember -Function *-TargetResource |