DSCResources/cVMNetworkAdapterVlan/cVMNetworkAdapterVlan.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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# Fallback message strings in en-US DATA localizedData { # same as culture = "en-US" ConvertFrom-StringData @' HyperVModuleNotFound=Hyper-V PowerShell Module not found. VMNameAndManagementTogether=VMName cannot be provided when ManagementOS is set to True. MustProvideVMName=Must provide VMName parameter when ManagementOS is set to False. GetVMNetAdapter=Getting VM Network Adapter information. FoundVMNetAdapter=Found VM Network Adapter. NoVMNetAdapterFound=No VM Network Adapter found. VMNetAdapterDoesNotExist=VM Network Adapter does not exist. PerformVMVlanSet=Perfoming VM Network Adapter VLAN setting configuration. IgnoreVlan=Ignoring VLAN configuration when the opeartion mode chosen is Untagged. VlanIdRequiredInAccess=VlanId must be specified when chosen operation mode is Access. MustProvideNativeVlanId=NativeVlanId must be specified when chosen operation mode is Trunk. PrimaryVlanIdRequired=PrimaryVlanId is required when the chosen operation mode is Community or Isolated or Promiscuous. AccessVlanMustChange=VlanId in Access mode is different. It will be changed. AdaptersExistsWithVlan=VM Network adapter exists with required VLAN configuration. NativeVlanMustChange=NativeVlanId in Trunk mode is different and it wil be changed. AllowedVlanListMustChange=AllowedVlanIdList is different in trunk mode. It will be changed. PrimaryVlanMustChange=PrimaryVlanId is different and must be changed. SecondaryVlanMustChange=SecondaryVlanId is different and must be changed. SecondaryVlanListMustChange=SecondaryVlanIdList is different and must be changed. AdapterExistsWithDifferentVlanMode=VM Network adapter exists with different Vlan configuration. It will be fixed. '@ } if (Test-Path "$PSScriptRoot\$PSCulture") { Import-LocalizedData LocalizedData -filename "cVMNetworkAdapterVlan.psd1" -BaseDirectory "$PSScriptRoot\$PSCulture" } Function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] Param ( [Parameter(Mandatory)] [String] $Id, [Parameter(Mandatory)] [String] $Name, [Parameter(Mandatory)] [String] $VMName ) if(!(Get-Module -ListAvailable -Name Hyper-V)) { Throw $localizedData.HyperVModuleNotFound } $Arguments = @{ Name = $Name } if ($VMName -ne 'Management OS') { $Arguments.Add('VMName',$VMName) } else { $Arguments.Add('ManagementOS', $true) } try { Write-Verbose $localizedData.GetVMNetAdapter $AdapterExists = Get-VMNetworkAdapter @Arguments -ErrorAction SilentlyContinue if ($AdapterExists) { Write-Verbose $localizedData.FoundVMNetAdapter $Configuration = @{ 'Name' = $Name 'VMName' = $VMName } #$Configuration.Remove('Name') $Configuration.Add('AdapterMode',$AdapterExists.VlanSetting.OperationMode) $Configuration.Add('VlanId',$AdapterExists.VlanSetting.AccessVlanId) $Configuration.Add('NativeVlanId',$AdapterExists.VlanSetting.NativeVlanId) $Configuration.Add('PrimaryVlanId',$AdapterExists.VlanSetting.PrimaryVlanId) $Configuration.Add('SecondaryVlanId',$AdapterExists.VlanSetting.SecondaryVlanId) $Configuration.Add('SecondaryVlanIdList',$AdapterExists.VlanSetting.SecondaryVlanIdListString) $Configuration.Add('AllowedVlanIdList',$AdapterExists.VlanSetting.AllowedVlanIdListString) } $Configuration } catch { Write-Error $_ } } Function Set-TargetResource { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $Id, [Parameter(Mandatory)] [String] $Name, [Parameter(Mandatory)] [String] $VMName, [Parameter()] [ValidateSet('Untagged','Access','Trunk','Communnity','Isolated','Promiscuous')] [String] $AdapterMode = 'Untagged', [Parameter()] [uint32] $VlanId, [Parameter()] [uint32] $NativeVlanId, [Parameter()] [String] $AllowedVlanIdList, [Parameter()] [uint32] $PrimaryVlanId, [Parameter()] [uint32] $SecondaryVlanId, [Parameter()] [String] $SecondaryVlanIdList ) if(!(Get-Module -ListAvailable -Name Hyper-V)) { Throw $localizedData.HyperVModuleNotFound } $Arguments = @{ Name = $Name } if ($VMName -ne 'Management OS') { $Arguments.Add('VMName',$VMName) } else { $Arguments.Add('ManagementOS', $true) } try { Write-Verbose $localizedData.GetVMNetAdapter $AdapterExists = Get-VMNetworkAdapter @Arguments -ErrorAction SilentlyContinue if ($AdapterExists) { Write-Verbose $localizedData.FoundVMNetAdapter $SetArguments = $Arguments $SetArguments.Remove('Name') $SetArguments.Add('VMNetworkAdapterName',$Name) switch ($AdapterMode) { 'Untagged' { $SetArguments.Add('Untagged',$true) break } 'Access' { $SetArguments.Add('Access',$true) $SetArguments.Add('VlanId',$VlanId) break } 'Trunk' { $SetArguments.Add('Trunk',$true) $SetArguments.Add('NativeVlanId',$NativeVlanId) if ($AllowedVlanIdList) { $SetArguments.Add('AllowedVlanIdList',$AllowedVlanIdList) } break } 'Community' { $SetArguments.Add('Community',$true) $SetArguments.Add('PrimaryVlanId',$PrimaryVlanId) if ($SecondaryVlanId) { $SetArguments.Add('SecondaryVlanId',$SecondaryVlanId) } break } 'Isolated' { $SetArguments.Add('Isolated',$true) $SetArguments.Add('PrimaryVlanId',$PrimaryVlanId) if ($SecondaryVlanId) { $SetArguments.Add('SecondaryVlanId',$SecondaryVlanId) } break } 'Promiscuous' { $SetArguments.Add('Promiscuous',$true) $SetArguments.Add('PrimaryVlanId', $PrimaryVlanId) if ($SecondaryVlanIdList) { $SetArguments.Add('SecondaryVlanIdList', $SecondaryVlanIdList) } break } } Write-Verbose $localizedData.PerformVMVlanSet Set-VMNetworkAdapterVlan @SetArguments -ErrorAction Stop } else { throw $localizedData.NoVMNetAdapterFound } } catch { Write-Error $_ } } Function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] Param ( [Parameter(Mandatory)] [String] $Id, [Parameter(Mandatory)] [String] $Name, [Parameter(Mandatory)] [String] $VMName, [Parameter()] [ValidateSet('Untagged','Access','Trunk','Communnity','Isolated','Promiscuous')] [String] $AdapterMode = 'Untagged', [Parameter()] [uint32] $VlanId, [Parameter()] [uint32] $NativeVlanId, [Parameter()] [String] $AllowedVlanIdList, [Parameter()] [uint32] $PrimaryVlanId, [Parameter()] [uint32] $SecondaryVlanId, [Parameter()] [String] $SecondaryVlanIdList ) if(!(Get-Module -ListAvailable -Name Hyper-V)) { Throw $localizedData.HyperVModuleNotFound } $Arguments = @{ Name = $Name } if ($VMName -ne 'Management OS') { $Arguments.Add('VMName',$VMName) } else { $Arguments.Add('ManagementOS', $true) } switch ($AdapterMode) { 'Untagged' { if ($VlanId -or $NativeVlanId -or $PrimaryVlanId -or $SecondaryVlanId -or $AllowedVlanIdList -or $SecondaryVlanIdList) { Write-Verbose $localizedData.IgnoreVlan } break } 'Access' { if (-not $VlanId) { throw $localizedData.VlanIdRequiredInAccess } break } 'Trunk' { if (-not $NativeVlanId) { throw $localizedData.MustProvideNativeVlanId } break } 'Community' { if (-not $PrimaryVlanId) { throw $localizedData.PrimaryVlanIdRequired } break } 'Isolated' { if (-not $PrimaryVlanId) { throw $localizedData.PrimaryVlanIdRequired } break } 'Promiscuous' { if (-not $PrimaryVlanId) { throw $localizedData.PrimaryVlanIdRequired } break } } try { #There is a remote timing issue that occurs when VLAN is set just after creating a VM Adapter. This needs more investigation. Sleep until then. Start-Sleep -Seconds 10 Write-Verbose $localizedData.GetVMNetAdapter $AdapterExists = Get-VMNetworkAdapter @Arguments -ErrorAction SilentlyContinue if ($AdapterExists) { Write-Verbose $localizedData.FoundVMNetAdapter if ($AdapterExists.VlanSetting.OperationMode -eq $AdapterMode) { switch ($AdapterExists.VlanSetting.OperationMode) { 'Access' { if ($VlanId -ne $AdapterExists.VlanSetting.AccessVlanId) { Write-Verbose $localizedData.AccessVlanMustChange return $false } else { Write-Verbose $localizedData.AdaptersExistsWithVlan return $true } break } 'Trunk' { if ($NativeVlanId -ne $AdapterExists.VlanSetting.NativeVlanId) { Write-Verbose $localizedData.NativeVlanMustChange return $false } elseif ($AllowedVlanIdList -ne $AdapterMode.VlanSetting.AllowedVlanIdListString) { Write-Verbose $localizedData.AllowedVlanListMustChange return $false } else { Write-Verbose $localizedData.AdaptersExistsWithVlan return $true } break } 'Untagged' { if ($AdapterMode -eq 'Untagged') { Write-Verbose $localizedData.AdaptersExistsWithVlan Write-Verbose $localizedData.IgnoreVlan return $true } break } ('Community' -or 'isolated') { if ($PrimaryVlanId -ne $AdapterExists.VlanSetting.PrimaryVlanId) { Write-Verbose $localizedData.PrimaryVlanMustChange return $false } elseif ($SecondaryVlanId -ne $AdapterExists.VlanSetting.SecondaryVlanId) { Write-Verbose $localizedData.SecondaryVlanMustChange return $false } else { Write-Verbose $localizedData.AdaptersExistsWithVlan return $true } break } 'Promiscuous' { if ($PrimaryVlanId -ne $AdapterExists.VlanSetting.PrimaryVlanId) { Write-Verbose $localizedData.PrimaryVlanMustChange return $false } elseif ($SecondaryVlanIdList -ne $AdapterExists.VlanSetting.SecondaryVlanIdListString) { Write-Verbose $localizedData.SecondaryVlanListMustChange return $false } else { Write-Verbose $localizedData.AdaptersExistsWithVlan return $true } } } } else { Write-Verbose $localizedData.AdapterExistsWithDifferentVlanMode return $false } } else { throw $localizedData.VMNetAdapterDoesNotExist } } catch { Write-Error $_ } } |