dig-like functionality on windows?

Jason De Arte picture Jason De Arte · Jan 14, 2019 · Viewed 9.8k times · Source

On a windows system, I'm trying to gather all the IP addresses for a DNS name & call a tool with each IP address. I know how to do it from a shell script - but not how to do it from a batch or powershell file.

I want to port this to windows..

#!/usr/bin/env bash
# Get all the IPs for our server instance
# and pass it to "p4 trust" to update the .p4trust file
for address in $(dig perforce.example.com +short)
do
    echo "processing address: $address:1666"
    p4 -p "ssl:$address:1666" trust -y -f || true
done

Questions:

  1. is there a pre-installed windows dig equivalent that will only return the IPs of the DNS record?
  2. in a batch or powershell file, how do you iterate over multiple results from another application?

Answer

postanote picture postanote · Jan 14, 2019

Try this...

Get-Command -Name Resolve-Dns* | 
Format-Table -AutoSize

CommandType Name                   Version Source       
----------- ----                   ------- ------       
Cmdlet      Resolve-DnsName        1.0.0.0 DnsClient     

# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Resolve-DnsName).Parameters
Get-help -Name Resolve-DnsName -Examples
Get-help -Name Resolve-DnsName -Full
Get-help -Name Resolve-DnsName -Online


# Get all IPAddresses for the provided DNS name
$env:USERDNSDOMAIN | ForEach{Resolve-DnsName -Name $PSItem}

Name          Type   TTL   Section    IPAddress                                
----          ----   ---   -------    ---------                                
CONTOSO.COM A      600   Answer     192.168....                             
CONTOSO.COM A      600   Answer     10.10... 

# 
$env:USERDNSDOMAIN | 
ForEach{
    # Get all IP addresses for the provided DNS name
    $DNSIPA = (Resolve-DnsName -Name $PSItem).IPAddress
    
    # Check if the host is up for a given IPA and port number
    ForEach($IPA in $DNSIPA)
    {
        "Processing $IPA"
        Test-NetConnection -ComputerName $IPA -Port 1666
    }
}

Yet, this question would translate into you are new to PowerShell. So, before you cause yourself undue confusion/frustration, etc., it is vital you spend the time getting up to speed on it using all the available free PowerShell video and eBook training resources. Here are just a few.

MSDN, MSDocs, MVA, MSChannel9, YouTube, eBooks, use the help files as noted above.