Fix -WhatIf, -Verbose, and -ErrorAction support in the DnsServer PowerShell module
The new cmdlets for DNS policies in particular have inconsistent and downright broken support for -WhatIf and -Verbose.
Add-DnsServerClientSubnet -cn MyDC -Name 'Whatever' -IPv4Subnet 10.0.0.0/32
That works if you explicitly add -WhatIf. But if I make the call inside an advanced function that SupportsShouldProcess and call the outer function with -WhatIf, the preference will NOT carry over. In order to do that, I have to use -WhatIf:$WhatIfPreference.
The same is true with -Verbose, having to use -Verbose:$VerbosePreference because it won't inherit it.
This is true on the cmdlets for:
- Client Subnets
- Zone Scopes
- Query Reoslution Policies
Additionally, -ErrorAction is seemingly ignored; they all throw regardless of the setting, forcing the use of try / catch.
The worst gaffe is Get-DnsServerClientSubnet. It has no -WhatIf support, which is normal for a read-only Get- cmdlet, but if $WhatifPreference is $true, it uses it! It won't return the values. And since there is no -WhatIf parameter to override, I'm having a **** of a time getting around this.
So far I've tried explicitly setting $WhatIfPreference to $false before calling it but it seems like that's not working. Still experimenting.
I've confirmed that it's always looking at the highest scope, which at least in the case of $WhatIfPreference is interchangeable with Global, so I can (messily) work around this by saving $Global:WhatifPreference, setting it to $false before this call, and then restoring it. Uggghhh.
This gets weirder in the case of Get-DnsServerClientSubnet and WhatIf. It seem that it's specifically looking for the variable $WhatIfPreference, but only at Scope 2.. so it's ignoring the current value of $WhatIfPreference (in Scope 0), but it using the value in Scope 2 (two levels up). I don't have enough testing in place to determine if that's a hardcoded 2, or there are some other critera. In my current environment $WhatIfPreference is not set at Scope 1 so I don't know how that might change things.