Skip to content

Commit d1cba4d

Browse files
committed
VMNetworkAdapter Device Naming
VMNetworkAdapter MAC Spoofing
1 parent 6c0e583 commit d1cba4d

File tree

5 files changed

+85
-12
lines changed

5 files changed

+85
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
2121
- BREAKING CHANGE: Rename embedded instance class #203
2222
- Fix multiple DNS IP adresses does not work #190
2323
- NetworkSetting parameter is now optional and no default actions are taken if not specified
24+
- DeviceNaming parameter added #206
25+
- MacAddressSpoofing parameter added #206
2426

2527
## [3.18.0] - 2022-06-04
2628

source/DSCResources/DSC_VMNetworkAdapter/DSC_VMNetworkAdapter.psm1

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ function Get-TargetResource
134134
.PARAMETER VlanId
135135
Specifies the Vlan Id for the network adapter.
136136
137+
.PARAMETER DeviceNaming
138+
Use this to enable or disable Device Naming. Default: Off
139+
140+
.PARAMETER MacAddressSpoofing
141+
Use this to enable or disable MAC address spoofing. Default: Off
142+
137143
.PARAMETER Ensure
138144
Specifies if the network adapter should be Present or Absent.
139145
#>
@@ -170,6 +176,16 @@ function Set-TargetResource
170176
[System.String]
171177
$VlanId,
172178

179+
[Parameter()]
180+
[ValidateSet('On', 'Off')]
181+
[System.String]
182+
$DeviceNaming = 'Off',
183+
184+
[Parameter()]
185+
[ValidateSet('On', 'Off')]
186+
[System.String]
187+
$MacAddressSpoofing = 'Off',
188+
173189
[Parameter()]
174190
[ValidateSet('Present', 'Absent')]
175191
[System.String]
@@ -232,7 +248,10 @@ function Set-TargetResource
232248
{
233249
Write-Verbose -Message $script:localizedData.PerformVMNetModify
234250

235-
$setArguments = @{ }
251+
$setArguments = @{
252+
DeviceNaming = $DeviceNaming
253+
MacAddressSpoofing = $MacAddressSpoofing
254+
}
236255
$setArguments.Add('VMNetworkAdapter', $netAdapterExists)
237256
if ($MacAddress)
238257
{
@@ -259,9 +278,11 @@ function Set-TargetResource
259278
$arguments.Add('StaticMacAddress', $MacAddress)
260279
}
261280
$arguments.Add('SwitchName', $SwitchName)
281+
$arguments.Add('DeviceNaming', $DeviceNaming)
262282
}
263283
Write-Verbose -Message $script:localizedData.AddVMNetAdapter
264284
$netAdapterExists = Add-VMNetworkAdapter @arguments -Passthru -ErrorAction Stop
285+
Set-VMNetworkAdapter -VMNetworkAdapter $netAdapterExists -MacAddressSpoofing $MacAddressSpoofing
265286
}
266287

267288
if ($VmName -ne 'ManagementOS')
@@ -367,6 +388,12 @@ function Set-TargetResource
367388
.PARAMETER VlanId
368389
Specifies the Vlan Id for the network adapter.
369390
391+
.PARAMETER DeviceNaming
392+
Use this to enable or disable Device Naming. Default: Off
393+
394+
.PARAMETER MacAddressSpoofing
395+
Use this to enable or disable MAC address spoofing. Default: Off
396+
370397
.PARAMETER Ensure
371398
Specifies if the network adapter should be Present or Absent.
372399
#>
@@ -404,6 +431,16 @@ function Test-TargetResource
404431
[System.String]
405432
$VlanId,
406433

434+
[Parameter()]
435+
[ValidateSet('On', 'Off')]
436+
[System.String]
437+
$DeviceNaming = 'Off',
438+
439+
[Parameter()]
440+
[ValidateSet('On', 'Off')]
441+
[System.String]
442+
$MacAddressSpoofing = 'Off',
443+
407444
[Parameter()]
408445
[ValidateSet('Present', 'Absent')]
409446
[System.String]
@@ -558,6 +595,28 @@ function Test-TargetResource
558595
return $true
559596
}
560597

598+
if ($netAdapterExists.MacAddressSpoofing -ne $MacAddressSpoofing)
599+
{
600+
Write-Verbose -Message $script:localizedData.SpoofingDifferent
601+
return $false
602+
}
603+
else
604+
{
605+
Write-Verbose -Message $script:localizedData.SpoofingConfiguredNoActionNeeded
606+
return $true
607+
608+
}
609+
610+
if ($netAdapterExists.DeviceNaming -ne $DeviceNaming)
611+
{
612+
Write-Verbose -Message $script:localizedData.DeviceNamingDifferent
613+
return $false
614+
}
615+
else
616+
{
617+
Write-Verbose -Message $script:localizedData.DeviceNamingConfiguredNoActionNeeded
618+
return $true
619+
}
561620
}
562621
else
563622
{

source/DSCResources/DSC_VMNetworkAdapter/DSC_VMNetworkAdapter.schema.mof

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ class DSC_VMNetworkAdapter : OMI_BaseResource
1919
[Write, Description("Network Settings of the network adapter. If this parameter is not supplied, DHCP will be used."), EmbeddedInstance("VMNetworkAdapterNetworkSettings")] String NetworkSetting;
2020
[Write, Description("Use this to specify a Vlan id on the Network Adapter.")] String VlanId;
2121
[Write, Description("Ensures that the VM Network Adapter is Present or Absent. The default value is `Present`."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
22+
[Write, Description("Use this to enable or disable Device Naming. Default: Off"), ValueMap{"On","Off"}, Values{"On","Off"}] String DeviceNaming;
23+
[Write, Description("Use this to enable or disable MAC address spoofing. Default: Off"), ValueMap{"On","Off"}, Values{"On","Off"}] String MacAddressSpoofing;
2224
[Read, Description("Returns `$true` if the network adapter uses a dynamic MAC address.")] Boolean DynamicMacAddress;
2325
};

source/DSCResources/DSC_VMNetworkAdapter/en-US/DSC_VMNetworkAdapter.strings.psd1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ ConvertFrom-StringData @'
2727
VMNetAdapterDoesNotExistNoActionNeeded=VM Network adapter does not exist. No action needed.
2828
SwitchIsDifferent=Net Adapter is not connected to the requested switch.
2929
PerformSwitchConnect=Connecting VM Net adapter to the right switch.
30+
SpoofingDifferent=MAC address spoofing configuration does not match.
31+
SpoofingConfiguredNoActionNeeded=MAC address spoofing configured.
32+
DeviceNamingDifferent=Device naming configuration does not match.
33+
DeviceNamingConfiguredNoActionNeeded=Device naming configured.
3034
'@

tests/Unit/DSC_VMNetworkAdapter.Tests.ps1

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ try
155155

156156
Describe 'DSC_VMNetworkAdapter\Set-TargetResource' {
157157
$newAdapter = [PSObject]@{
158-
Id = 'UniqueString'
159-
Name = $TestAdapter.Name
160-
SwitchName = $TestAdapter.SwitchName
161-
VMName = 'VMName'
162-
NetworkSetting = $networkSettingsStatic
163-
Ensure = 'Present'
158+
Id = 'UniqueString'
159+
Name = $TestAdapter.Name
160+
SwitchName = $TestAdapter.SwitchName
161+
VMName = 'VMName'
162+
NetworkSetting = $networkSettingsStatic
163+
Ensure = 'Present'
164+
DeviceNaming = 'On'
165+
MacAddressSpoofing = 'On'
164166
}
165167

166168
Context 'Adapter does not exist but should' {
@@ -172,6 +174,7 @@ try
172174
Mock -CommandName Remove-VMNetworkAdapter
173175
Mock -CommandName Set-VMNetworkAdapterVlan
174176
Mock -CommandName Set-NetworkInformation
177+
Mock -CommandName Set-VMNetworkAdapter
175178

176179
It 'should not throw error' {
177180
{
@@ -185,6 +188,7 @@ try
185188
Assert-MockCalled -commandName Add-VMNetworkAdapter -Exactly 1
186189
Assert-MockCalled -commandName Remove-VMNetworkAdapter -Exactly 0
187190
Assert-MockCalled -CommandName Set-NetworkInformation -Exactly 1
191+
Assert-MockCalled -CommandName Set-VMNetworkAdapter -Exactly 1
188192
}
189193
}
190194

@@ -212,11 +216,13 @@ try
212216

213217
Describe 'DSC_VMNetworkAdapter\Test-TargetResource' {
214218
$newAdapter = [PSObject]@{
215-
Id = 'UniqueString'
216-
Name = $TestAdapter.Name
217-
SwitchName = $TestAdapter.SwitchName
218-
VMName = 'ManagementOS'
219-
Ensure = 'Present'
219+
Id = 'UniqueString'
220+
Name = $TestAdapter.Name
221+
SwitchName = $TestAdapter.SwitchName
222+
VMName = 'ManagementOS'
223+
Ensure = 'Present'
224+
DeviceNaming = 'On'
225+
MacAddressSpoofing = 'On'
220226
}
221227

222228
Context 'Adapter does not exist but should' {

0 commit comments

Comments
 (0)