Private/Resolve-QueryOptions.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Resolve-QueryOptions {
    [CmdletBinding()]
    param(
        [switch]$UseCache,
        [switch]$Recursion,
        [TimeSpan]$Timeout,
        [int]$Retries,
        [switch]$ThrowDnsErrors,
        [switch]$UseRandomNameServer,
        [switch]$ContinueOnDnsError,
        [switch]$ContinueOnEmptyResponse,
        [switch]$UseTcpFallback,
        [switch]$UseTcpOnly,
        [int]$ExtendedDnsBufferSize,
        [switch]$EnableAuditTrail,
        [switch]$RequestDnsSecRecords,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # We want to allow per-call query options from Resolve-Dns. But we don't
    # want to bother creating a new instance of DnsQueryOptions if the caller
    # didn't specify any options or the options they did specify don't
    # conflict with the existing options on the current LookupClient.
    #
    # Within Resolve-Dns, we should be able to splat its $PSBoundParameters
    # against this function and return either $null to indicate there are no
    # query specific options to apply or a DnsQueryOptions instance that
    # contains the current LookupClient options with any overridden values.

    $cOpts = Get-LookupClientOptions
    $qOpts = $null
    $psbKeys = $PSBoundParameters.Keys

    # loop through the parameters to see if any are different
    $paramNames = @(
        'UseCache'
        'Recursion'
        'Timeout'
        'Retries'
        'ThrowDnsErrors'
        'UseRandomNameServer'
        'ContinueOnDnsError'
        'ContinueOnEmptyResponse'
        'UseTcpFallback'
        'UseTcpOnly'
        'ExtendedDnsBufferSize'
        'EnableAuditTrail'
        'RequestDnsSecRecords'
    )
    foreach ($pName in $paramNames) {
        if ($pName -in $psbKeys -and $cOpts.$pName -ne $PSBoundParameters[$pName]) {
            if (-not $qOpts) {
                Write-Debug "Cloning query specific options from client options"
                $qOpts = [DnsClient.DnsQueryOptions]::new()
                $paramNames | ForEach-Object {
                    $qOpts.$_ = $cOpts.$_
                }
            }
            Write-Debug "$pName($($PSBoundParameters[$pName])) differs from current value"
            $qOpts.$pName = $PSBoundParameters[$pName]
        }
    }

    return $qOpts
}