Resolve-DnsNames: Resolve DNS or IP for multiple Computers

Respective Microsoft’s Technet article regarding Resolve-DnsName I have created a function with the ability to run it against more than only one computer. Resolve-DnsNames performs a DNS name query resolution for the specified name.

 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
Function Resolve-DnsNames
{
<#
.SYNOPSIS
    Resolves IP or DNS name for one or more computers.

.DESCRIPTION
    Resolves IP or DNS name for one or more computers.

.PARAMETER ComputerName
    A single Computer or an array of computer names. The default is localhost ($env:COMPUTERNAME).

.PARAMETER IPAddress
    Commit IPAddress to resolve.

.PARAMETER Verbose
    Run in Verbose Mode.

.EXAMPLE
    PS C:> Resolve-DnsNames graef.io

    Name                                           Type   TTL   Section    IPAddress
    ----                                           ----   ---   -------    ---------
    graef.io                                       AAAA   86336 Answer     2a01:488:42:1000:50ed:84e8:ff91:1f91
    graef.io                                       A      86336 Answer     80.237.132.232


.EXAMPLE
	PS C:> Resolve-DnsNames 80.237.132.232

    Name                           Type   TTL   Section    NameHost
    ----                           ----   ---   -------    --------
    232.132.237.80.in-addr.arpa    PTR    40292 Answer     graef.io

.EXAMPLE
    PS C:> Resolve-DnsNames -ComputerName (Get-Content C:ServerList.txt)

.LINK

Home


.NOTES
    Author:  Sebastian Gräf
    Email:   sebastian@graef.io
    Date:    September 10, 2017
    PSVer:   2.0/3.0/4.0/5.0
#>

	[Cmdletbinding()]
	Param (
		[Parameter(
				   Mandatory = $false,
				   ValueFromPipeline = $true,
				   ValueFromPipelineByPropertyName = $true)]
		[string[]]$ComputerName = $Env:COMPUTERNAME
	)

	Begin
	{
		Write-Verbose " [$($MyInvocation.InvocationName)] :: Start Process"
        $ProgressCounter = 0
        $array=@()
	}

	Process
	{
        foreach ($Computer in $ComputerName)
		{
            $ProgressCounter++
			Write-Progress -activity "Running on $Computer" -status "Please wait ..." -PercentComplete (($ProgressCounter / $ComputerName.length) * 100)
				Write-Verbose " [$($MyInvocation.InvocationName)] :: Processing $Computer"
				try
				{
                    $Resolution = Resolve-DnsName $Computer -ErrorAction SilentlyContinue
                    $obj = New-Object PSObject
                    $obj | Add-Member NoteProperty ComputerName ($Computer)
                    $obj | Add-Member NoteProperty Name ($Resolution.Name)
                    $obj | Add-Member NoteProperty Type ($Resolution.Type)
                    $obj | Add-Member NoteProperty TTL ($Resolution.TTL)
                    $obj | Add-Member NoteProperty Section ($Resolution.Section)
                    $obj | Add-Member NoteProperty NameHost ($Resolution.NameHost)
                    $obj | Add-Member NoteProperty IPAddress ($Resolution.IPAddress)
                    $array += $obj

				}
				catch
				{
					Write-Verbose " Host [$Computer] Failed with Error: $($Error[0])"
				}
	    }
        $array | ft
    }
	End
	{
		Write-Verbose " [$($MyInvocation.InvocationName)] :: End Process"
	}
}
References