Get-FQDNInfo.ps1

Someone recently asked me if I could write a script for them.  He had a list of several hundred fully qualified domain names (internet URLs essentially) in a file, and he had to get the IP address(es) of each FQDN and then some whois information about those IP addresses.  Running down a list of names and resolving them scriptomatically is a breeze of course, but the whois stuff sounded a little more tricky.  Luckily, ARIN has a sweet REST API ready to go, that made the whole script a snap.

I took the time to return all that data as objects so the output can be pipelined, and there is also an optional "save to CSV" parameter.  I think there are a couple more ways in which the script could be improved, but it works for now.  The output looks like this:

Get-FQDNInfo.ps1

And here's the whole script:

<#
.SYNOPSIS	
	Feed me a bunch of FQDNs, one per line, and I will give you as much info as
	I can about that IP address.
	
.DESCRIPTION
	This script takes an input file. The input file contains a list of FQDNs, one per line.
	With each FQDN, the script will attempt to resolve the name, and then find as much
	info as it can using ARIN REST services.
	
.PARAMETER InFile
	Specify a text file containing the FQDNs you want to scan.
	Each FQDN goes on a separate line. For example:
	
	host.foo.com
	barney.purpledinosaur.com
	et.phonehome.org

.PARAMETER OutFile
	Optional file to write the results to.

.INPUTS
	[System.String]$InFile - The name of the input file to read.
.INPUTS
	[System.String]$OutFile - Optional, the name of the output file to write.

.OUTPUTS
	[System.Object]$FQDNInfoCollection - Contains resolved FQDNInfo objects.

.OUTPUTS
	[System.IO.File]$OutFile - Optional, the output file to write.
	
.EXAMPLE
	PS C:\> .\Get-FQDNInfo.ps1 .\fqdns.txt outfile.txt

.EXAMPLE
	PS C:\> "fqdns.txt" | .\Get-FQDNInfo.ps1
	
.NOTES
	Name  : Get-FQDNInfo.ps1
	Author: Ryan Ries
	Email : ryanries09@gmail.com
	Date  : April 19, 2013

.LINK	
	http://www.myotherpcisacloud.com
#>

Param([Parameter(Mandatory=$True,ValueFromPipeline=$True)][ValidateScript({Test-Path $_ -PathType Leaf})][String]$InFile, [Parameter(Mandatory=$False,ValueFromPipeline=$False)][String]$OutFile)
$FQDNInfoCollection = @()
$EntriesProcessed = 0
Foreach($FQDN In Get-Content $InFile)
{
	$FQDNInfo = New-Object System.Object
	$FQDNInfo | Add-Member -Type NoteProperty -Name "FQDN"        -Value $FQDN
	$FQDNInfo | Add-Member -Type NoteProperty -Name "AddressList" -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "CSVSafeList" -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "NetRange"    -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "CIDR"        -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "NetName"     -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "NetType"     -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "RegDate"     -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "Updated"     -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "Comment"     -Value $null
	$FQDNInfo | Add-Member -Type NoteProperty -Name "SOA"         -Value $null
	
	Try	{ $FQDNInfo.AddressList = $([System.Net.Dns]::GetHostEntry($FQDN)).AddressList }
	Catch {	}
	If($FQDNInfo.AddressList -ne $null)
	{
		ForEach($A In $FQDNInfo.AddressList) { $FQDNInfo.CSVSafeList += "$($A)|" }
		$FQDNInfo.CSVSafeList = $FQDNInfo.CSVSafeList.TrimEnd('|')		
		Try
		{
			$ARINData = $(Invoke-WebRequest http://whois.arin.net/rest/ip/$($FQDNInfo.AddressList[0].ToString())`.txt).Content
			$ARINData = $ARINData.Split([Environment]::NewLine)
			Foreach($l In $ARINData)
			{
				If($l.StartsWith("NetRange:"))    { $FQDNInfo.NetRange = $l.SubString(16) }
				Elseif($l.StartsWith("CIDR:"))    { $FQDNInfo.CIDR     = $l.SubString(16) }
				Elseif($l.StartsWith("NetName:")) { $FQDNInfo.NetName  = $l.SubString(16) }
				Elseif($l.StartsWith("NetType:")) { $FQDNInfo.NetType  = $l.SubString(16) }
				Elseif($l.StartsWith("RegDate:")) { $FQDNInfo.RegDate  = $l.SubString(16) }
				Elseif($l.StartsWith("Updated:")) { $FQDNInfo.Updated  = $l.SubString(16) }
				Elseif($l.StartsWith("Comment:")) 
				{ 
					$FQDNInfo.Comment += $l.SubString(16)
					$FQDNInfo.Comment += " "
				}
			}
		}
		Catch { }
		& nslookup -q=soa $FQDN 2>&1> $Env:UserProfile`\temp.txt
		Foreach($_ In Get-Content $Env:UserProfile`\temp.txt)
		{
			If($_.Contains("primary name server =")) { $FQDNInfo.SOA = $_.Split('=')[1].Trim() }
		}		
	}	
	$FQDNInfoCollection += $FQDNInfo
	$EntriesProcessed   += 1
	Write-Host $EntriesProcessed "FQDNs processed."
}

If($OutFile.Length -gt 0)
{
	$FQDNInfoCollection | Export-CSV $OutFile -NoTypeInformation	
}
return $FQDNInfoCollection
Comments are closed