nslookup++ / How to Resolve a List of Hostnames to a List of IPs...

...with access to the DOS command prompt commands and the bare minimum set of PowerShell cmdlets.

Copy and paste the script below into a text editor and save as say nslookup++.ps1, and run in PowerShell as .\nslookup++.ps1.

Example image of it in use is below (using public website addresses since cannot share internal hostnames on this blog.)

Image: Example of nslookup++ in action

The Script


[Boolean]$Repeat = $TRUE
While($Repeat){
  Write-Host "nslookup++: RESOLVES LIST OF HOSTNAMES TO IP"
  Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  Write-Host "Enter list of hostnames (can paste a list)"
  Write-Host "  and press ENTER on empty line to continue (no input terminates program):"
  [System.Array]$Hosts = @()
  [Boolean]$do = $TRUE
 
  While($do){
    $ReadIn = Read-Host
    If($ReadIn -eq ""){$do = $FALSE}
    else{$Hosts += $ReadIn}
  }
 
  [System.Array]$HostsOut = @()
  $Hosts | Foreach{
    If($_ -eq "NONE"){$HostsOut += "NO CHECK"}
    else{
      [System.Array]$nslookup = (nslookup $_)
      [Int32]$TempCount = 0
      [String]$Result = ""
      Foreach($line in $nslookup){
        $TempCount++
        If($line.StartsWith("Name")){
          $Result = $nslookup[$TempCount].Split(" ")[2]
        }
      }
      If(!$Result){$HostsOut += "DNF"}
      else{$HostsOut += $Result}
    }
  }
 
  $HostsOut | Foreach{Write-Host $_}
  If($Hosts.count -eq 0){$Repeat = $FALSE}
  else{PAUSE}
}


Comments