Get-DnsConfiguration.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#
.SYNOPSIS
    Retrives DNS Servers from a computer.
.DESCRIPTION
    Retrives primary, secondary, tertiery DNS Servers from on online system using Windows Management Instrimentation.
.INPUTS
    System.String.
.OUPUTS
    System.Management.Automation.PSCustomObject.
.PARAMETER ComputerName
    The computer to retrieve information from.
.PARAMETER Credential
    PSCredential object with rights to the remotly access WMI.
.EXAMPLE
    Get-DnsConfiguration
 
    PSComputerName : MyPC
    NetworkAdapter : Hyper-V Virtual Ethernet Adapter [192.168.1.1]
    PrimaryDNSServer : 8.8.8.8
    SecondaryDNSSserver : 8.8.4.4
    TertieryDNSServer :
.EXAMPLE
    Get-DNSConfiguration -ComputerName 'remotepc.my.org' -Credential (Get-Credential)
 
    PSComputerName : remotepc
    NetworkAdapter : Hyper-V Virtual Ethernet Adapter [192.168.1.1]
    PrimaryDNSServer : 8.8.8.8
    SecondaryDNSSserver : 8.8.4.4
    TertieryDNSServer :
.LINK
    http://dotps1.github.io
#>

[OutputType([PSCustomObject])]

Param (
    [Parameter(
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true
    )]
    [Alias(
        'ComputerName',
        'PSComputerName'
    )]
    [String[]]
    $Name = $env:COMPUTERNAME,

    [Parameter()]
    [ValidateNotNull()]
    [PSCredential]
    $Credential
)

Process {
    for ($i = 0; $i -lt $Name.Length; $i++) {
        try {
            $gwmiParams = @{
                ComputerName = $Name[$i]
                Class = 'Win32_NetworkAdapterConfiguration'
                Namespace = 'root\cimV2'
                Filter = "IPEnabled='TRUE'"
                ErrorAction = 'Stop'
            }

            if ($Name[$i] -ne $env:COMPUTERNAME -and $Credential -ne $null) {
                $gwmiParams.Add('Credential', $Credential)
            }

            $networkAdapters = Get-WmiObject @gwmiParams
        } catch {
            Write-Error -Message $_.ToString()
            continue
        }

        foreach ($networkAdapater in $networkAdapters) {
            $dnsServers = $networkAdapater.DNSServerSearchOrder

            [PSCustomObject]([Ordered]@{
                PSComputerName = $Name[$i]
                NetworkAdapter = "$($networkAdapater.Description) [$($networkAdapater.IPAddress[0])]"
                PrimaryDNSServer = $dnsServers[0]
                SecondaryDNSSserver = $dnsServers[1]
                TertieryDNSServer = $dnsServers[2]
            })
        }
    }
}

<#PSScriptInfo
.DESCRIPTION
    Retrives primary, secondary, tertiery DNS Servers from on online system using Windows Management Instrimentation.
.VERSION
    1.0
.GUID
    85a1387f-b20b-443c-bea0-ba4181ed7444
.AUTHOR
    Thomas Malkewitz @dotps1
.TAGS
    Dns
.RELEASENOTES
    Intial Release.
#>