Skip to content

Commit

Permalink
VMNetworkAdapter Device Naming
Browse files Browse the repository at this point in the history
VMNetworkAdapter MAC Spoofing
  • Loading branch information
nyanhp committed Apr 21, 2023
1 parent 0e5c3d9 commit 103a087
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
- Fix multiple DNS IP adresses does not work #190
- NetworkSetting parameter is now optional and no default actions are taken if not specified
- Switch to use VM image `windows-latest` to build phase.
- DeviceNaming parameter added #206
- MacAddressSpoofing parameter added #206

## [3.18.0] - 2022-06-04

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ function Get-TargetResource
.PARAMETER VlanId
Specifies the Vlan Id for the network adapter.
.PARAMETER DeviceNaming
Use this to enable or disable Device Naming. Default: Off
.PARAMETER MacAddressSpoofing
Use this to enable or disable MAC address spoofing. Default: Off
.PARAMETER Ensure
Specifies if the network adapter should be Present or Absent.
#>
Expand Down Expand Up @@ -170,6 +176,16 @@ function Set-TargetResource
[System.String]
$VlanId,

[Parameter()]
[ValidateSet('On', 'Off')]
[System.String]
$DeviceNaming = 'Off',

[Parameter()]
[ValidateSet('On', 'Off')]
[System.String]
$MacAddressSpoofing = 'Off',

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
Expand Down Expand Up @@ -232,7 +248,10 @@ function Set-TargetResource
{
Write-Verbose -Message $script:localizedData.PerformVMNetModify

$setArguments = @{ }
$setArguments = @{
DeviceNaming = $DeviceNaming
MacAddressSpoofing = $MacAddressSpoofing
}
$setArguments.Add('VMNetworkAdapter', $netAdapterExists)
if ($MacAddress)
{
Expand All @@ -259,9 +278,11 @@ function Set-TargetResource
$arguments.Add('StaticMacAddress', $MacAddress)
}
$arguments.Add('SwitchName', $SwitchName)
$arguments.Add('DeviceNaming', $DeviceNaming)
}
Write-Verbose -Message $script:localizedData.AddVMNetAdapter
$netAdapterExists = Add-VMNetworkAdapter @arguments -Passthru -ErrorAction Stop
Set-VMNetworkAdapter -VMNetworkAdapter $netAdapterExists -MacAddressSpoofing $MacAddressSpoofing
}

if ($VmName -ne 'ManagementOS')
Expand Down Expand Up @@ -367,6 +388,12 @@ function Set-TargetResource
.PARAMETER VlanId
Specifies the Vlan Id for the network adapter.
.PARAMETER DeviceNaming
Use this to enable or disable Device Naming. Default: Off
.PARAMETER MacAddressSpoofing
Use this to enable or disable MAC address spoofing. Default: Off
.PARAMETER Ensure
Specifies if the network adapter should be Present or Absent.
#>
Expand Down Expand Up @@ -404,6 +431,16 @@ function Test-TargetResource
[System.String]
$VlanId,

[Parameter()]
[ValidateSet('On', 'Off')]
[System.String]
$DeviceNaming = 'Off',

[Parameter()]
[ValidateSet('On', 'Off')]
[System.String]
$MacAddressSpoofing = 'Off',

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
Expand Down Expand Up @@ -558,6 +595,28 @@ function Test-TargetResource
return $true
}

if ($netAdapterExists.MacAddressSpoofing -ne $MacAddressSpoofing)
{
Write-Verbose -Message $script:localizedData.SpoofingDifferent
return $false
}
else
{
Write-Verbose -Message $script:localizedData.SpoofingConfiguredNoActionNeeded
return $true

}

if ($netAdapterExists.DeviceNaming -ne $DeviceNaming)
{
Write-Verbose -Message $script:localizedData.DeviceNamingDifferent
return $false
}
else
{
Write-Verbose -Message $script:localizedData.DeviceNamingConfiguredNoActionNeeded
return $true
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ class DSC_VMNetworkAdapter : OMI_BaseResource
[Write, Description("Network Settings of the network adapter. If this parameter is not supplied, DHCP will be used."), EmbeddedInstance("VMNetworkAdapterNetworkSettings")] String NetworkSetting;
[Write, Description("Use this to specify a Vlan id on the Network Adapter.")] String VlanId;
[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;
[Write, Description("Use this to enable or disable Device Naming. Default: Off"), ValueMap{"On","Off"}, Values{"On","Off"}] String DeviceNaming;
[Write, Description("Use this to enable or disable MAC address spoofing. Default: Off"), ValueMap{"On","Off"}, Values{"On","Off"}] String MacAddressSpoofing;
[Read, Description("Returns `$true` if the network adapter uses a dynamic MAC address.")] Boolean DynamicMacAddress;
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ ConvertFrom-StringData @'
VMNetAdapterDoesNotExistNoActionNeeded=VM Network adapter does not exist. No action needed.
SwitchIsDifferent=Net Adapter is not connected to the requested switch.
PerformSwitchConnect=Connecting VM Net adapter to the right switch.
SpoofingDifferent=MAC address spoofing configuration does not match.
SpoofingConfiguredNoActionNeeded=MAC address spoofing configured.
DeviceNamingDifferent=Device naming configuration does not match.
DeviceNamingConfiguredNoActionNeeded=Device naming configured.
'@
28 changes: 17 additions & 11 deletions tests/Unit/DSC_VMNetworkAdapter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ try

Describe 'DSC_VMNetworkAdapter\Set-TargetResource' {
$newAdapter = [PSObject]@{
Id = 'UniqueString'
Name = $TestAdapter.Name
SwitchName = $TestAdapter.SwitchName
VMName = 'VMName'
NetworkSetting = $networkSettingsStatic
Ensure = 'Present'
Id = 'UniqueString'
Name = $TestAdapter.Name
SwitchName = $TestAdapter.SwitchName
VMName = 'VMName'
NetworkSetting = $networkSettingsStatic
Ensure = 'Present'
DeviceNaming = 'On'
MacAddressSpoofing = 'On'
}

Context 'Adapter does not exist but should' {
Expand All @@ -172,6 +174,7 @@ try
Mock -CommandName Remove-VMNetworkAdapter
Mock -CommandName Set-VMNetworkAdapterVlan
Mock -CommandName Set-NetworkInformation
Mock -CommandName Set-VMNetworkAdapter

It 'should not throw error' {
{
Expand All @@ -185,6 +188,7 @@ try
Assert-MockCalled -commandName Add-VMNetworkAdapter -Exactly 1
Assert-MockCalled -commandName Remove-VMNetworkAdapter -Exactly 0
Assert-MockCalled -CommandName Set-NetworkInformation -Exactly 1
Assert-MockCalled -CommandName Set-VMNetworkAdapter -Exactly 1
}
}

Expand Down Expand Up @@ -212,11 +216,13 @@ try

Describe 'DSC_VMNetworkAdapter\Test-TargetResource' {
$newAdapter = [PSObject]@{
Id = 'UniqueString'
Name = $TestAdapter.Name
SwitchName = $TestAdapter.SwitchName
VMName = 'ManagementOS'
Ensure = 'Present'
Id = 'UniqueString'
Name = $TestAdapter.Name
SwitchName = $TestAdapter.SwitchName
VMName = 'ManagementOS'
Ensure = 'Present'
DeviceNaming = 'On'
MacAddressSpoofing = 'On'
}

Context 'Adapter does not exist but should' {
Expand Down

0 comments on commit 103a087

Please sign in to comment.