-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathVMNetworkAdapter.psm1
More file actions
181 lines (142 loc) · 6.79 KB
/
VMNetworkAdapter.psm1
File metadata and controls
181 lines (142 loc) · 6.79 KB
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
enum Ensure {
Absent
Present
}
[DscResource()]
Class VMNetworkAdapterTeamMapping {
[DscProperty(Mandatory)]
[Ensure] $Ensure
[DscProperty(Key)]
[String] $PhysicalNetAdapterName
[DscProperty(Key)]
[String] $VMNetworkAdapterName
[VMNetworkAdapterTeamMapping] Get() {
$VMNetworkAdapterTeamMapping = Get-VMNetworkAdapterTeamMapping -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName
$this.PhysicalNetAdapterName = $VMNetworkAdapterTeamMapping.NetAdapterName
$this.VMNetworkAdapterName = $VMNetworkAdapterTeamMapping.ParentAdapter.Name
return $this
}
[bool] Test() {
$VMNetworkAdapterTeamMapping = Get-VMNetworkAdapterTeamMapping -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName
$testState = $false
if ($this.Ensure -eq [Ensure]::Present) {
If ($VMNetworkAdapterTeamMapping.NetAdapterName -eq $this.PhysicalNetAdapterName -and
$VMNetworkAdapterTeamMapping.ParentAdapter.Name -eq $this.VMNetworkAdapterName) {
$testState = $true
}
Else { $testState = $false }
}
elseif ($this.Ensure -eq [Ensure]::Absent) {
If ($VMNetworkAdapterTeamMapping) { $testState = $false }
Else { $testState = $true }
}
Return $testState
}
[Void] Set() {
if ($this.Ensure -eq [Ensure]::Present) {
Write-Verbose "Mapping $($this.VMNetworkAdapterName) to $($this.PhysicalNetAdapterName)"
Set-VMNetworkAdapterTeamMapping -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName -PhysicalNetAdapterName $this.PhysicalNetAdapterName
Write-Verbose "$($this.VMNetworkAdapterName) is now mapped to $($this.PhysicalNetAdapterName)"
}
elseif ($this.Ensure -eq [Ensure]::Absent) {
Write-Verbose "Removing team mapping for $($this.VMNetworkAdapterName)"
Remove-VMNetworkAdapterTeamMapping -ManagementOS -Name $this.VMNetworkAdapterName
Write-Verbose "Team mapping for $($this.VMNetworkAdapterName) is now removed"
}
}
}
[DscResource()]
Class VMNetworkAdapterSettings {
[DscProperty(Key)]
[String] $VMNetworkAdapterName
[DscProperty(Mandatory)]
[String] $VMName = 'ManagementOS'
[DscProperty()]
[ValidateSet('On','Off')]
[String] $IeeePriorityTag = 'Off'
[VMNetworkAdapterSettings] Get() {
$params = @{}
if ($this.VMName -eq 'ManagementOS') {
$params.Add('ManagementOS', $true)
}
else { $params.Add('VMName', $this.VMName)}
$VMNetworkAdapter = Get-VMNetworkAdapter -VMNetworkAdapterName $this.VMNetworkAdapterName @params -ErrorAction SilentlyContinue
$this.VMNetworkAdapterName = $VMNetworkAdapter.Name
$this.IeeePriorityTag = $VMNetworkAdapter.IeeePriorityTag
return $this
}
[bool] Test() {
$params = @{}
if ($this.VMName -eq 'ManagementOS') {$params.Add('ManagementOS', $true)}
else {$params.Add('VMName', $this.VMName)}
$VMNetworkAdapter = Get-VMNetworkAdapter -VMNetworkAdapterName $this.VMNetworkAdapterName @params -ErrorAction SilentlyContinue
$testState = $false
If ($this.IeeePriorityTag -eq $VMNetworkAdapter.IeeePriorityTag) { $testState = $true }
Else { $testState = $false }
Return $testState
}
[Void] Set() {
$params = @{}
if ($this.VMName -eq 'ManagementOS') {$params.Add('ManagementOS', $true)}
else {$params.Add('VMName', $this.VMName)}
$VMNetworkAdapter = Get-VMNetworkAdapter -VMNetworkAdapterName $this.VMNetworkAdapterName @params -ErrorAction SilentlyContinue
if ($this.IeeePriorityTag -ne $VMNetworkAdapter.IeeePriorityTag) {
Write-Verbose "Configuring IEEEPriorityTag on vNIC $($VMNetworkAdapter.Name) to $($this.IeeePriorityTag)"
Set-VMNetworkAdapter -VMNetworkAdapterName $this.VMNetworkAdapterName @params -IeeePriorityTag $this.IeeePriorityTag
Write-Verbose "IEEEPriorityTag on vNIC $($VMNetworkAdapter.Name) is now $($this.IeeePriorityTag)"
}
}
}
[DscResource()]
Class VMNetworkAdapterIsolation {
[DscProperty(Mandatory)]
[Ensure] $Ensure
[DscProperty(Key)]
[String] $VMNetworkAdapterName
[DscProperty(Mandatory)]
[ValidateRange(0,4096)]
[uint16] $DefaultIsolationID
[DscProperty()]
[Boolean] $AllowUntaggedTraffic
[DscProperty()]
[ValidateSet('Vlan','None')]
[String] $IsolationMode
[VMNetworkAdapterIsolation] Get() {
$VMNetworkAdapterIsolation = Get-VMNetworkAdapterIsolation -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName
$this.IsolationMode = $VMNetworkAdapterIsolation.IsolationMode
$this.DefaultIsolationID = $VMNetworkAdapterIsolation.DefaultIsolationID
$this.AllowUntaggedTraffic = $VMNetworkAdapterIsolation.AllowUntaggedTraffic
return $this
}
[bool] Test() {
$VMNetworkAdapterIsolation = Get-VMNetworkAdapterIsolation -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName
$testState = $false
if ($this.Ensure -eq [Ensure]::Present) {
if ( $VMNetworkAdapterIsolation.IsolationMode -eq $this.IsolationMode `
-and $VMNetworkAdapterIsolation.AllowUntaggedTraffic -eq $this.AllowUntaggedTraffic `
-and $VMNetworkAdapterIsolation.DefaultIsolationID -eq $this.DefaultIsolationID )
{ $testState = $true }
else { $testState = $false }
}
elseif ($this.Ensure -eq [Ensure]::Absent) {
If ($VMNetworkAdapterIsolation) { $testState = $false }
Else { $testState = $true }
}
Return $testState
}
[Void] Set() {
if ($this.Ensure -eq [Ensure]::Present) {
Write-Verbose "Removing VMNetworkAdapterVlan if configured on $($this.VMNetworkAdapterName)"
Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName -Untagged
Write-Verbose "Configuring Isolation for $($this.VMNetworkAdapterName)"
Set-VMNetworkAdapterIsolation -ManagementOS -IsolationMode $this.IsolationMode -DefaultIsolationID $this.DefaultIsolationID`
-AllowUntaggedTraffic $this.AllowUntaggedTraffic -VMNetworkAdapterName $this.VMNetworkAdapterName
Write-Verbose "VLAN Isolation for $($this.VMNetworkAdapterName) is now configured"
}
elseif ($this.Ensure -eq [Ensure]::Absent) {
Write-Verbose "Resetting Isolation for $($this.VMNetworkAdapterName)"
Set-VMNetworkAdapterIsolation -ManagementOS -VMNetworkAdapterName $this.VMNetworkAdapterName -IsolationMode 'None'
Write-Verbose "Isolation for $($this.VMNetworkAdapterName) is now reset"
}
}
}