Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VMNetworkAdapter: Device Naming and MAC Address Spoofing #208

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion source/DSCResources/DSC_VMHyperV/DSC_VMHyperV.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function Set-TargetResource
if ($nic.SwitchName -ne $switch)
{
Write-Verbose -Message ($script:localizedData.VMPropertyShouldBe -f 'NIC', $switch, $nic.SwitchName)
$nic | Connect-VMNetworkAdapter -SwitchName $switch
Connect-VMNetworkAdapter -VMNetworkAdapter $nic -SwitchName $switch
Write-Verbose -Message ($script:localizedData.VMPropertySet -f 'NIC', $switch)
}
}
Expand Down
78 changes: 70 additions & 8 deletions source/DSCResources/DSC_VMNetworkAdapter/DSC_VMNetworkAdapter.psm1
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 @@ -205,34 +221,42 @@ function Set-TargetResource
if ($netAdapterExists.DynamicMacAddressEnabled)
{
Write-Verbose -Message $script:localizedData.EnableStaticMacAddress
$updateMacAddress = $true
$updateAdapter = $true
}
elseif ($MacAddress -ne $netAdapterExists.StaicMacAddress)
elseif ($MacAddress -ne $netAdapterExists.MacAddress)
{
Write-Verbose -Message $script:localizedData.EnableStaticMacAddress
$updateMacAddress = $true
$updateAdapter = $true
}
}
else
{
if (-not $netAdapterExists.DynamicMacAddressEnabled)
{
Write-Verbose -Message $script:localizedData.EnableDynamicMacAddress
$updateMacAddress = $true
$updateAdapter = $true
}
}

if ($netAdapterExists.DeviceNaming -ne $DeviceNaming)
{
$updateAdapter = $true
}

if ($netAdapterExists.SwitchName -ne $SwitchName)
{
Write-Verbose -Message $script:localizedData.PerformSwitchConnect
Connect-VMNetworkAdapter -VMNetworkAdapter $netAdapterExists -SwitchName $SwitchName -ErrorAction Stop -Verbose
}

if (($updateMacAddress))
if (($updateAdapter))
{
Write-Verbose -Message $script:localizedData.PerformVMNetModify

$setArguments = @{ }
$setArguments = @{
DeviceNaming = $DeviceNaming
MacAddressSpoofing = $MacAddressSpoofing
}
$setArguments.Add('VMNetworkAdapter', $netAdapterExists)
if ($MacAddress)
{
Expand All @@ -259,9 +283,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 +393,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 +436,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 @@ -554,10 +596,30 @@ function Test-TargetResource
}
else
{
Write-Verbose -Message $script:localizedData.VMNetAdapterExistsNoActionNeeded
return $true
Write-Verbose -Message $script:localizedData.SwitchIsCorrect
}

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

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 @@ -26,5 +26,10 @@ ConvertFrom-StringData @'
VMNetAdapterExistsShouldRemove=VM Network Adapter Exists. It will be removed.
VMNetAdapterDoesNotExistNoActionNeeded=VM Network adapter does not exist. No action needed.
SwitchIsDifferent=Net Adapter is not connected to the requested switch.
SwitchIsCorrect=Net Adapter is 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.
'@
6 changes: 3 additions & 3 deletions tests/Unit/DSC_VMHyperV.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ try
$StubVMConfig = New-Item -Path 'TestDrive:\TestVM.xml' -ItemType File

# Mock the class VMNetworkAdapter to support piping to cmdlet Connect-VMNetworkAdapter
$stubNIC1 = [Microsoft.HyperV.PowerShell.VMNetworkAdapter]::CreateTypeInstance()
$stubNIC1 = [Microsoft.HyperV.PowerShell.VMNetworkAdapterBase]::CreateTypeInstance()
$stubNIC1.SwitchName = 'Test Switch 1'
$stubNIC1.MacAddress = 'AA-BB-CC-DD-EE-FF'
$stubNIC1.IpAddresses = @('192.168.0.1', '10.0.0.1')

# Mock the class VMNetworkAdapter to support piping to cmdlet Connect-VMNetworkAdapter
$stubNIC2 = [Microsoft.HyperV.PowerShell.VMNetworkAdapter]::CreateTypeInstance()
$stubNIC2 = [Microsoft.HyperV.PowerShell.VMNetworkAdapterBase]::CreateTypeInstance()
$stubNIC2.SwitchName = 'Test Switch 2'
$stubNIC2.MacAddress = 'AA-BB-CC-DD-EE-FE'
$stubNIC2.IpAddresses = @('192.168.1.1')
Expand Down Expand Up @@ -602,7 +602,7 @@ try
Mock -CommandName Stop-VM -MockWith { return $true } # requires output to be able to pipe something into Remove-VM
Mock -CommandName Remove-VM -MockWith { return $true }
Mock -CommandName Set-VMNetworkAdapter -MockWith { return $true }
Mock -CommandName Get-VMNetworkAdapter -MockWith { return $stubVM.NetworkAdapters.IpAddresses }
Mock -CommandName Get-VMNetworkAdapter -MockWith { return $stubVM.NetworkAdapters }
Mock -CommandName Set-VMState -MockWith { return $true }
Mock -CommandName Set-VMMemory

Expand Down
Loading