DnsServerDsc icon indicating copy to clipboard operation
DnsServerDsc copied to clipboard

DnsServerDsc: Resources errors on test if DNS is not installed

Open GoateePFE opened this issue 8 years ago • 10 comments

xDNSServerForwarder (as well as other resource in the module) need error handling logic to gracefully return FALSE on the Test function when DNS is not installed. This is particularly needed when DSC is used to audit a server config without applying.

GoateePFE avatar Feb 09 '17 20:02 GoateePFE

@GoateePFE I'd be willing to add this functionality. What do you believe the best way to do this would be?

I'm not sure how to best handle the error. I'm thinking to update the get function to return an empty IPAddresses array, test should then not throw an error.

Persistent13 avatar Apr 26 '17 21:04 Persistent13

@Persistent13 That should do it for now.

Long term we should probably add a function to the root of the resource module to check for the presence of the DNS server role and warn if it is not installed. Then all resources could reference that.

Function Test-DNSServerRole { [CmdletBinding()] Param() If ((Get-WindowsFeature DNS).Installed) { Write-Verbose "DNS server role installed." Return $true } Else { Write-Verbose "DNS server role NOT installed." Return $false } }

Easy enough.

GoateePFE avatar Apr 27 '17 13:04 GoateePFE

I'm sure there's an Assert-Module function in the xActiveDirectory module that you can copy/duplicate!

iainbrighton avatar Apr 27 '17 14:04 iainbrighton

Label this as help wanted to that someone in the community can run with this.

johlju avatar May 07 '18 09:05 johlju

https://github.com/PowerShell/xDnsServer/blob/dev/DSCResources/Helper.psm1

This Helper Module includes Internal function to assert if the role specific module is installed or not function Assert-Module

So just need to ensure that the others include and use it

1800Zeta avatar Jun 17 '19 12:06 1800Zeta

Assert-Module will still error out on Get if the module isn't installed. I think Assert-Module still needs to be added to all of the resources in xDnsServer.

For not erroring out on Get we could do a try catch:

try
    {
        Assert-Module -ModuleName 'DNS'
    }
catch
{
    Write-Warning -Message 'DNS module is not installed and this resource will not work properly.'
    return @{
         Ensure = 'Absent'
         Prop    = $null
    }
}

rchristman89 avatar Apr 12 '20 07:04 rchristman89

Actually since Get nor Set use any cmdlets from the DNS module we can just remove Assert-Module?

https://github.com/dsccommunity/xDnsServer/blob/2a8ea7f52c1a29637a5fc895105b8defeddbe4d8/source/DSCResources/MSFT_xDnsServerSetting/MSFT_xDnsServerSetting.psm1#L22

and

https://github.com/dsccommunity/xDnsServer/blob/2a8ea7f52c1a29637a5fc895105b8defeddbe4d8/source/DSCResources/MSFT_xDnsServerSetting/MSFT_xDnsServerSetting.psm1#L225

johlju avatar Apr 12 '20 12:04 johlju

Most of the resources in XDnsServer do require it. So I was thinking it needed to be added to those.

This was specifically opened to not error out in Get, is that something we can support?

rchristman89 avatar Apr 12 '20 17:04 rchristman89

Yes to both. For all the resource; Get-TargetResource should be able to return the result without throwing an error. It should always return the correct hashtable (all the properties in the schema), either with the actual current values or with null values for those properties it cannot fetch. Key properties should always be set to the configuration values and if there is an Ensure property it should be set to Absent if the Key values does not exist in the current state.

johlju avatar Apr 12 '20 17:04 johlju

Awesome. I will look at doing that.

rchristman89 avatar Apr 12 '20 18:04 rchristman89