expirat001
expirat001

Reputation: 2195

Resolve-DNSName for Windows 2008

I have a script working for Windows 2012 (PowerShell v4) but it has to work also for Windows 2008 (PowerShell v2), what is the equivalent of the cmdlet "Resolve-DNSName" for Windows 2008?

Resolve-DnsName -Name client01 -Server server01

I know it exists the same for nslookup and this is what I would like as a cmdlet (one-liner, with no input required from my part)

nslookup
server server01
client01

The following works for DNS resolution but is missing the -server parameter :

[Net.DNS]::GetHostEntry("MachineName")

Thanks

Upvotes: 2

Views: 7701

Answers (3)

Rumyan Tsekov
Rumyan Tsekov

Reputation: 1

I use this code to input FQDNs one per line and output respective IPs.

$Server = Get-Content servers.txt
$OutArray = @()
$output = foreach ($Server in $Server) {
    $IP = [System.Net.Dns]::GetHostAddresses($Server)
    $OutArray += $Server + "  " + $IP.IPAddressToString
}

$OutArray | Out-File IPs.txt

Upvotes: 0

krismanme
krismanme

Reputation: 63

Unfortunately there isn't a way to do this natively in powershell prior to Version 4 in Windows 8.1 or Server 2012. There are .NET methods however:

https://stackoverflow.com/a/8227917/4292988

The simplest solution in powershell is to call nslookup, and cleanup the output

&nslookup.exe client01 server01

I removed select-string from the original sample, it left less to work with

The function you posted following mine doesnt work very well, and will never work in PowershellV2, [PSCustomObject] wasn't supported until v3. Furthermore if you send a dns query that would normally return a single address, it returns nothing. For queries with aliases, it returns the aliases where the ipaddress should be. Test Resolve-DnsName2008 -name www.stackoverflow.com -server 8.8.8.8.

The Following is a function that should do what your asking, at least for ipv4addresses:

function Resolve-DnsName2008
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [string]$Name,
        [string]$Server = '127.0.0.1'
    )
    Try
    {
        $nslookup = &nslookup.exe $Name $Server
        $regexipv4 = "^(?:(?:0?0?\d|0?[1-9]\d|1\d\d|2[0-5][0-5]|2[0-4]\d)\.){3}(?:0?0?\d|0?[1-9]\d|1\d\d|2[0-5][0-5]|2[0-4]\d)$"

        $name = @($nslookup | Where-Object { ( $_ -match "^(?:Name:*)") }).replace('Name:','').trim()

        $deladdresstext = $nslookup -replace "^(?:^Address:|^Addresses:)",""
        $Addresses = $deladdresstext.trim() | Where-Object { ( $_ -match "$regexipv4" ) }

        $total = $Addresses.count
        $AddressList = @()
        for($i=1;$i -lt $total;$i++)
        {
            $AddressList += $Addresses[$i].trim()
        }

        $AddressList | %{

        new-object -typename psobject -Property @{
            Name = $name
            IPAddress = $_
            }
        }
    }
    catch 
    { }
}

Upvotes: 2

expirat001
expirat001

Reputation: 2195

The problem is that if I use :

&nslookup.exe client01 server01 | select-string "Name", "Addresses"

It will only display the first record, in my case I had 5 records found and only one displayed.

The solution I found works very well :

function Resolve-DNSName2008
{
    Param
    (
        [string]$Name,
        [string]$Server
    )

    $nslookup = &nslookup.exe $Name $Server

    $name = [string]($nslookup | Select-String "Name")
    $nameClean = ([regex]::match($name,'(?<=:)(.*\n?)').value).Trim()

    $addresses = (([regex]::match($nslookup,'(?<=Addresses:)(.*\n?)').value).Trim()).Split(' ')
    $addressesClean = $addresses.Split('',[System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object

    $addressesClean | %{

        [PSCustomObject]@{
            Name = $nameClean
            IPAddress = $_
        }
    }

}

Usage:

Resolve-DNSName2008 -Name server.domain.com -Server 10.0.0.0

Output:

Name            IPAddress      
----            ---------      
server.domain.com 10.0.0.1
server.domain.com 10.0.0.2    
server.domain.com 10.0.0.3  
server.domain.com 10.0.0.4
server.domain.com 10.0.0.5

Upvotes: -1

Related Questions