Skip to content

Commit

Permalink
Merge pull request #216 from PlagueHO/Issue-113
Browse files Browse the repository at this point in the history
Add DHCP support to xDNSServerAddress - Fixes #113
  • Loading branch information
Tyson J. Hayes committed Jun 16, 2017
2 parents 20c090a + 4f94f62 commit f5a552b
Show file tree
Hide file tree
Showing 14 changed files with 999 additions and 441 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
- Fixed bug in Get-TargetResource when Name is the only adapter selector parameter.
- Improved verbose logging.
- More improvements to verbose logging.
- Added Get-DnsClientServerStaticAddress to NetworkingDsc.Common to return statically
assigned DNS server addresses to support fix for [issue 113](https://github.com/PowerShell/xNetworking/issues/113).
- MSFT_xDNSserverAddress:
- Added support for setting DNS Client to DHCP for [issue 113](https://github.com/PowerShell/xNetworking/issues/113).
- Added new examples to show how to enable DHCP on DNS Client.
- Improved integration test coverage to enable testing of multiple addresses and
DHCP.
- Converted exception creation to use common exception functions.
- MSFT_xDhcpClient:
- Updated example to also cover setting DNS Client to DHCP.

## 4.1.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $localizedData = Get-LocalizedData `
IP address family.
.PARAMETER Address
The desired DNS Server address(es).
The desired DNS Server address(es). Exclude to enable DHCP.
#>
function Get-TargetResource
{
Expand All @@ -44,7 +44,6 @@ function Get-TargetResource
[String]
$AddressFamily,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String[]]
$Address
Expand All @@ -54,11 +53,17 @@ function Get-TargetResource
$($LocalizedData.GettingDNSServerAddressesMessage)
) -join '')

# Remove the parameters we don't want to splat
$null = $PSBoundParameters.Remove('Address')

# Get the current DNS Server Addresses based on the parameters given.
[String[]] $currentAddress = Get-DnsClientServerStaticAddress `
@PSBoundParameters `
-ErrorAction Stop

$returnValue = @{
Address = (Get-DnsClientServerAddress `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily).ServerAddresses
AddressFamily = $AddressFamily
Address = $currentAddress
AddressFamily = $AddressFamily
InterfaceAlias = $InterfaceAlias
}

Expand All @@ -76,11 +81,11 @@ function Get-TargetResource
IP address family.
.PARAMETER Address
The desired DNS Server address(es).
The desired DNS Server address(es). Exclude to enable DHCP.
.PARAMETER Validate
Requires that the DNS Server addresses be validated if they are updated.
It will cause the resouce to throw a 'A general error occurred that is not covered by a more
It will cause the resource to throw a 'A general error occurred that is not covered by a more
specific error code.' error if set to True and specified DNS Servers are not accessible.
#>
function Set-TargetResource
Expand All @@ -98,7 +103,6 @@ function Set-TargetResource
[String]
$AddressFamily,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String[]]
$Address,
Expand All @@ -111,36 +115,66 @@ function Set-TargetResource
$($LocalizedData.ApplyingDNSServerAddressesMessage)
) -join '')

#Get the current DNS Server Addresses based on the parameters given.
$PSBoundParameters.Remove('Address')
$PSBoundParameters.Remove('Validate')
$currentAddress = (Get-DnsClientServerAddress @PSBoundParameters `
-ErrorAction Stop).ServerAddresses
# If address not passed, set to an empty array
if (-not $PSBoundParameters.ContainsKey('Address'))
{
[String[]] $Address = @()
}

#Check if the Server addresses are the same as the desired addresses.
# Remove the parameters we don't want to splat
$null = $PSBoundParameters.Remove('Address')
$null = $PSBoundParameters.Remove('Validate')

# Get the current DNS Server Addresses based on the parameters given.
[String[]] $currentAddress = @(Get-DnsClientServerStaticAddress `
@PSBoundParameters `
-ErrorAction Stop)

# Check if the Server addresses are the same as the desired addresses.
[Boolean] $addressDifferent = (@(Compare-Object `
-ReferenceObject $currentAddress `
-DifferenceObject $Address `
-SyncWindow 0).Length -gt 0)

if ($addressDifferent)
{
# Set the DNS settings as well
$Splat = @{
$dnsServerAddressSplat = @{
InterfaceAlias = $InterfaceAlias
Address = $Address
Validate = $Validate
}
Set-DnsClientServerAddress @Splat `
-ErrorAction Stop

Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): "
$($LocalizedData.DNSServersHaveBeenSetCorrectlyMessage)
) -join '' )
if ($Address.Count -eq 0)
{
# Reset the DNS server address to DHCP
$dnsServerAddressSplat += @{
ResetServerAddresses = $true
}

Set-DnsClientServerAddress @dnsServerAddressSplat `
-ErrorAction Stop

Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): "
$($LocalizedData.DNSServersHaveBeenSetToDHCPMessage)
) -join '' )
}
else
{
# Set the DNS server address to static
$dnsServerAddressSplat += @{
Address = $Address
Validate = $Validate
}

Set-DnsClientServerAddress @dnsServerAddressSplat `
-ErrorAction Stop

Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): "
$($LocalizedData.DNSServersHaveBeenSetCorrectlyMessage)
) -join '' )
}
}
else
{
#Test will return true in this case
# Test will return true in this case
Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): "
$($LocalizedData.DNSServersAlreadySetMessage)
) -join '' )
Expand All @@ -158,11 +192,11 @@ function Set-TargetResource
IP address family.
.PARAMETER Address
The desired DNS Server address(es).
The desired DNS Server address(es). Exclude to enable DHCP.
.PARAMETER Validate
Requires that the DNS Server addresses be validated if they are updated.
It will cause the resouce to throw a 'A general error occurred that is not covered by a more
It will cause the resource to throw a 'A general error occurred that is not covered by a more
specific error code.' error if set to True and specified DNS Servers are not accessible.
#>
function Test-TargetResource
Expand All @@ -181,7 +215,6 @@ function Test-TargetResource
[String]
$AddressFamily,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String[]]
$Address,
Expand All @@ -196,21 +229,32 @@ function Test-TargetResource
$($LocalizedData.CheckingDNSServerAddressesMessage)
) -join '' )

#Validate the Settings passed
Foreach ($ServerAddress in $Address) {
Assert-ResourceProperty `
-Address $ServerAddress `
-AddressFamily $AddressFamily `
-InterfaceAlias $InterfaceAlias
# Validate the Address passed or set to empty array if not passed
if ($PSBoundParameters.ContainsKey('Address'))
{
foreach ($ServerAddress in $Address)
{
Assert-ResourceProperty `
-Address $ServerAddress `
-AddressFamily $AddressFamily `
-InterfaceAlias $InterfaceAlias
} # foreach
}
else
{
[String[]] $Address = @()
} # if

# Remove the parameters we don't want to splat
$null = $PSBoundParameters.Remove('Address')
$null = $PSBoundParameters.Remove('Validate')

#Get the current DNS Server Addresses based on the parameters given.
$currentAddress = (Get-DnsClientServerAddress `
-InterfaceAlias $InterfaceAlias `
-AddressFamily $AddressFamily `
-ErrorAction Stop).ServerAddresses
# Get the current DNS Server Addresses based on the parameters given.
[String[]] $currentAddress = @(Get-DnsClientServerStaticAddress `
@PSBoundParameters `
-ErrorAction Stop)

#Check if the Server addresses are the same as the desired addresses.
# Check if the Server addresses are the same as the desired addresses.
[Boolean] $addressDifferent = (@(Compare-Object `
-ReferenceObject $currentAddress `
-DifferenceObject $Address `
Expand All @@ -219,14 +263,15 @@ function Test-TargetResource
if ($addressDifferent)
{
$desiredConfigurationMatch = $false

Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): "
$($LocalizedData.DNSServersNotCorrectMessage) `
-f ($Address -join ','),($currentAddress -join ',')
) -join '' )
}
else
{
#Test will return true in this case
# Test will return true in this case
Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): "
$($LocalizedData.DNSServersSetCorrectlyMessage)
) -join '' )
Expand All @@ -246,9 +291,8 @@ function Test-TargetResource
IP address family.
.PARAMETER Address
The desired DNS Server address.
The desired DNS Server address. Set to empty to enable DHCP.
#>

function Assert-ResourceProperty
{
[CmdletBinding()]
Expand All @@ -272,57 +316,33 @@ function Assert-ResourceProperty

if ( -not (Get-NetAdapter | Where-Object -Property Name -EQ $InterfaceAlias ))
{
$errorId = 'InterfaceNotAvailable'
$errorCategory = [System.Management.Automation.ErrorCategory]::DeviceError
$errorMessage = $($LocalizedData.InterfaceNotAvailableError) -f $InterfaceAlias
$exception = New-Object -TypeName System.InvalidOperationException `
-ArgumentList $errorMessage
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord `
-ArgumentList $exception, $errorId, $errorCategory, $null

$PSCmdlet.ThrowTerminatingError($errorRecord)
New-InvalidArgumentException `
-Message ($LocalizedData.InterfaceNotAvailableError -f $InterfaceAlias) `
-ArgumentName 'InterfaceAlias'
}

if ( -not ([System.Net.IPAddress]::TryParse($Address, [ref]0)))
{
$errorId = 'AddressFormatError'
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument
$errorMessage = $($LocalizedData.AddressFormatError) -f $Address
$exception = New-Object -TypeName System.InvalidOperationException `
-ArgumentList $errorMessage
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord `
-ArgumentList $exception, $errorId, $errorCategory, $null

$PSCmdlet.ThrowTerminatingError($errorRecord)
New-InvalidArgumentException `
-Message ($LocalizedData.AddressFormatError -f $Address) `
-ArgumentName 'Address'
}

$detectedAddressFamily = ([System.Net.IPAddress]$Address).AddressFamily.ToString()
if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
{
$errorId = 'AddressMismatchError'
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument
$errorMessage = $($LocalizedData.AddressIPv4MismatchError) -f $Address,$AddressFamily
$exception = New-Object -TypeName System.InvalidOperationException `
-ArgumentList $errorMessage
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord `
-ArgumentList $exception, $errorId, $errorCategory, $null

$PSCmdlet.ThrowTerminatingError($errorRecord)
New-InvalidArgumentException `
-Message ($LocalizedData.AddressIPv4MismatchError -f $Address,$AddressFamily) `
-ArgumentName 'Address'
}

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString()) `
-and ($AddressFamily -ne 'IPv6'))
{
$errorId = 'AddressMismatchError'
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument
$errorMessage = $($LocalizedData.AddressIPv6MismatchError) -f $Address,$AddressFamily
$exception = New-Object -TypeName System.InvalidOperationException `
-ArgumentList $errorMessage
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord `
-ArgumentList $exception, $errorId, $errorCategory, $null

$PSCmdlet.ThrowTerminatingError($errorRecord)
New-InvalidArgumentException `
-Message ($LocalizedData.AddressIPv6MismatchError -f $Address,$AddressFamily) `
-ArgumentName 'Address'
}
} # Assert-ResourceProperty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class MSFT_xDNSServerAddress : OMI_BaseResource
{
[Key, Description("Alias of the network interface for which the DNS server address is set.")] string InterfaceAlias;
[Key, Description("IP address family."), ValueMap{"IPv4", "IPv6"},Values{"IPv4", "IPv6"}] string AddressFamily;
[Required, Description("The desired DNS Server address(es).")] string Address[];
[Write, Description("Requires that the DNS Server addresses be validated if they are updated. It will cause the resouce to throw a 'A general error occurred that is not covered by a more specific error code.' error if set to True and specified DNS Servers are not accessible.")] boolean Validate;
[Write, Description("The desired DNS Server address(es). Exclude to enable DHCP.")] string Address[];
[Write, Description("Requires that the DNS Server addresses be validated if they are updated. It will cause the resource to throw a 'A general error occurred that is not covered by a more specific error code.' error if set to True and specified DNS Servers are not accessible.")] boolean Validate;
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Localized resources for MSFT_xDNSServerAddress

ConvertFrom-StringData @'
GettingDNSServerAddressesMessage=Getting the DNS Server Addresses.
ApplyingDNSServerAddressesMessage=Applying the DNS Server Addresses.
DNSServersSetCorrectlyMessage=DNS Servers are set correctly.
DNSServersAlreadySetMessage=DNS Servers are already set correctly.
CheckingDNSServerAddressesMessage=Checking the DNS Server Addresses.
DNSServersNotCorrectMessage=DNS Servers are not correct. Expected "{0}", actual "{1}".
DNSServersHaveBeenSetCorrectlyMessage=DNS Servers were set to the desired state.
InterfaceNotAvailableError=Interface "{0}" is not available. Please select a valid interface and try again.
AddressFormatError=Address "{0}" is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError=Address "{0}" is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError=Address "{0}" is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
GettingDNSServerAddressesMessage = Getting the DNS server addresses.
ApplyingDNSServerAddressesMessage = Applying the DNS server addresses.
DNSServersSetCorrectlyMessage = DNS server addresses are set correctly.
DNSServersAlreadySetMessage = DNS server addresses are already set correctly.
CheckingDNSServerAddressesMessage = Checking the DNS server addresses.
DNSServersNotCorrectMessage = DNS server addresses are not correct. Expected "{0}", actual "{1}".
DNSServersHaveBeenSetCorrectlyMessage = DNS server addresses were set to the desired state.
DNSServersHaveBeenSetToDHCPMessage = DNS server addresses were set to the desired state of DHCP.
InterfaceNotAvailableError = Interface "{0}" is not available. Please select a valid interface and try again.
AddressFormatError = Address "{0}" is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError = Address "{0}" is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError = Address "{0}" is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
'@
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<#
.EXAMPLE
Enabling DHCP Client for the Ethernet Alias
Enabling DHCP for the IP Address and DNS on the adapter with alias 'Ethernet'.
#>
Configuration Example
{
Expand All @@ -21,5 +21,11 @@ Configuration Example
InterfaceAlias = 'Ethernet'
AddressFamily = 'IPv4'
}

xDnsServerAddress EnableDhcpDNS
{
InterfaceAlias = 'Ethernet'
AddressFamily = 'IPv4'
}
}
}
Loading

0 comments on commit f5a552b

Please sign in to comment.