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: add parameter IgnoreNetworkSetting #189

Closed
wants to merge 2 commits into from
Closed
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 @@ -23,6 +23,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
all unit tests.
- xVhdFileDirectory
- Added initial set of unit tests
- xVMNetworkAdapter
- add parameter IgnoreNetworkSetting

### Deprecated

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ Manages VMNetadapters attached to a Hyper-V virtual machine or the management OS
* **`[String]` VMName** _(Required)_: Name of the VM to attach to.
If you want to attach new VM Network adapter to the management OS,
set this property to 'Management OS'.
* **`[Boolean]` IgnoreNetworkSetting** _(Write)_: If specified with `True` the network settings of the network adapter are not changed.
This should be set if the guest operating system configures the network adapter.
Default value is `False`.
* **`[NetworkSettings]` NetworkSetting** _(Write)_: Network Settings of the network adapter.
If this parameter is not supplied, DHCP will be used.
* **`[String]` MacAddress** _(Write)_: Use this to specify a Static MAC Address.
Expand Down
93 changes: 56 additions & 37 deletions source/DSCResources/DSC_VMNetworkAdapter/DSC_VMNetworkAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ function Get-TargetResource
Specifies the MAC address for the network adapter. This is not applicable if VMName
is set to ManagementOS. Use this parameter to specify a static MAC address.

.PARAMETER IpAddress
.Parameter IgnoreNetworkSetting
Specifies whether the IpAddress information for the network adapter is set or ignored

.PARAMETER NetworkSetting
Specifies the IpAddress information for the network adapter.
If not specified DHCP will be enabled on the network adapter.

.PARAMETER VlanId
Specifies the Vlan Id for the network adapter.
Expand Down Expand Up @@ -165,6 +169,10 @@ function Set-TargetResource
[System.String]
$MacAddress,

[Parameter()]
[Boolean]
$IgnoreNetworkSetting = $false,

[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$NetworkSetting,
Expand Down Expand Up @@ -267,7 +275,7 @@ function Set-TargetResource
$netAdapterExists = Add-VMNetworkAdapter @arguments -Passthru -ErrorAction Stop
}

if ($VmName -ne 'ManagementOS')
if ($VmName -ne 'ManagementOS' -and $IgnoreNetworkSetting -ne $true )
{
$networkInfo = Get-NetworkInformation -VMName $VMName -Name $Name
if (-not $NetworkSetting)
Expand Down Expand Up @@ -363,8 +371,12 @@ function Set-TargetResource
Specifies the MAC address for the network adapter. This is not applicable if VMName
is set to ManagementOS. Use this parameter to specify a static MAC address.

.PARAMETER IpAddress
.Parameter IgnoreNetworkSetting
Specifies whether the IpAddress information for the network adapter is tested or ignored

.PARAMETER NetworkSetting
Specifies the IpAddress information for the network adapter.
If not specified DHCP will be enabled on the network adapter.

.PARAMETER VlanId
Specifies the Vlan Id for the network adapter.
Expand Down Expand Up @@ -398,6 +410,10 @@ function Test-TargetResource
[System.String]
$MacAddress,

[Parameter()]
[Boolean]
$IgnoreNetworkSetting = $false,

[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$NetworkSetting,
Expand Down Expand Up @@ -457,50 +473,53 @@ function Test-TargetResource
}
}

$networkInfo = Get-NetworkInformation -VMName $VMName -Name $Name
if (-not $NetworkSetting)
if ($IgnoreNetworkSetting -ne $true)
{
if ($networkInfo)
$networkInfo = Get-NetworkInformation -VMName $VMName -Name $Name
if (-not $NetworkSetting)
{
Write-Verbose -Message $script:localizedData.NotDhcp
return $false
}
}
else
{
if (-not $networkInfo)
{
Write-Verbose -Message $script:localizedData.Dhcp
return $false
if ($networkInfo)
{
Write-Verbose -Message $script:localizedData.NotDhcp
return $false
}
}
else
{
$ipAddress = $NetworkSetting.CimInstanceProperties['IpAddress'].Value
$subnet = $NetworkSetting.CimInstanceProperties['Subnet'].Value
$defaultGateway = $NetworkSetting.CimInstanceProperties['DefaultGateway'].Value
$dnsServer = $NetworkSetting.CimInstanceProperties['DnsServer'].Value

if (-not $IpAddress -or -not $subnet)
{
throw $script:localizedData.MissingIPAndSubnet
}

if ($ipAddress -and -not $networkInfo.IPAddress.Split(',').Contains($ipAddress))
if (-not $networkInfo)
{
Write-Verbose -Message $script:localizedData.IPAddressNotConfigured
Write-Verbose -Message $script:localizedData.Dhcp
return $false
}

if ($defaultGateway -and -not $networkInfo.DefaultGateway.Split(',').Contains($defaultGateway))
else
{
Write-Verbose -Message $script:localizedData.GatewayNotConfigured
return $false
}
$ipAddress = $NetworkSetting.CimInstanceProperties['IpAddress'].Value
$subnet = $NetworkSetting.CimInstanceProperties['Subnet'].Value
$defaultGateway = $NetworkSetting.CimInstanceProperties['DefaultGateway'].Value
$dnsServer = $NetworkSetting.CimInstanceProperties['DnsServer'].Value

if ($dnsServer -and -not $networkInfo.DNSServer.Split(',').Contains($dnsServer))
{
Write-Verbose -Message $script:localizedData.DNSServerNotConfigured
return $false
if (-not $IpAddress -or -not $subnet)
{
throw $script:localizedData.MissingIPAndSubnet
}

if ($ipAddress -and -not $networkInfo.IPAddress.Split(',').Contains($ipAddress))
{
Write-Verbose -Message $script:localizedData.IPAddressNotConfigured
return $false
}

if ($defaultGateway -and -not $networkInfo.DefaultGateway.Split(',').Contains($defaultGateway))
{
Write-Verbose -Message $script:localizedData.GatewayNotConfigured
return $false
}

if ($dnsServer -and -not $networkInfo.DNSServer.Split(',').Contains($dnsServer))
{
Write-Verbose -Message $script:localizedData.DNSServerNotConfigured
return $false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DSC_VMNetworkAdapter : OMI_BaseResource
[Required] String SwitchName;
[Required] String VMName;
[Write] String MacAddress;
[Write] Boolean IgnoreNetworkSetting;
[Write, EmbeddedInstance("NetworkSettings")] String NetworkSetting;
[Write] String VlanId;
[Write, ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
Expand Down