Saturday, July 2, 2011

Check your ip/computername with nslookup in powershell

Nslookup is a great tool for testing and troubleshooting. However, if you need to check a large range of IP/Computername in mass, nslookup is not the better choice. So, this below script could help you to do the same in powershell. (Download script)


#
# NSLookup
#
# by F.Richard 2011-05
#


# ***********************************************

Function NSlookup {
Param($hostname)
#$iphostEntry = [System.Net.Dns]::GetHostEntry($hostname)
# Rexolve obsolete but better than GetHostEntry
$iphostEntry = [System.Net.Dns]::Resolve($hostname)
$line = $iphostEntry.HostName

$strSeparator = ";"
foreach ($addr in $iphostEntry.AddressList) {
$line = $line + $strSeparator + $Addr.IPAddressToString
$strSeparator = "|"
}

$strSeparator = ";"
foreach ($alias in $iphostEntry.aliases) {
$line = $line + $strSeparator + $alias
$strSeparator = "|"
}

return $line
}



# ***********************************************

Function TestFile {
Param(
[String] $strFilename,
[String] $strCurDir
)
if ($strFilename) {
$strCurDir = $(if ($strCurDir) {$strCurDir} else {if ($MyInvocation.MyCommand.CommandType -eq "Function") {(Get-Location).Path} else {Split-Path -parent $MyInvocation.MyCommand.Path} })
If ((Test-Path("$strFilename")) -eq $False){
If ($strFilename.ToUpper().Contains($strCurDir.ToUpper()) ) {
Write-Host "ERROR: file $strFilename NOT exist"
return
} Else {
If ((Test-Path("$strCurDir\$strFilename")) -eq $False){
Write-Host "ERROR: file $strFilename NOT exist"
Write-Host "ERROR: file $strCurDir\$strFilename NOT exist"
return
} Else {
$strFilename = "$strCurDir\$strFilename"
}
}
}
return $strFilename
}
}


# ***********************************************

Function GetFileIntoArr {
Param(
[String] $strFilename,
[String] $strCurDir,
[Ref]$arrFile,
[String] $strComment
)
$strComment = $(if ($strComment) {$strComment} else {"#"})
$strCurDir = $(if ($strCurDir) {$strCurDir} else {if ($MyInvocation.MyCommand.CommandType -eq "Function") {(Get-Location).Path} else {Split-Path -parent $MyInvocation.MyCommand.Path} })

$retFilename = TestFile -strFilename $strFilename -strCurDir $strCurDir
if ($retFilename) {
$Content = Get-Content "$retFilename"
Foreach ($line in $Content) {
$comment = $False
If ($line.Trim().length -gt 0) { # take only line with data and without # at begininng of the line
# if we do not want to use comment
If ($strComment -eq "#") {
If ($line.substring(0,1) -eq "#") {
$comment = $True
}
}
If ($comment -eq $False) {
# Get Search & Replace
$arrFile.Value += $line.Trim()
}
}
}
return $True
} else {
return $False
}
}

# ***********************************************

#
# MAIN PROGRAM
#

# Verify Arguments Number
If($Args.Count -lt 2) {
Write-Host "Syntax:"$MyInvocation.MyCommand.Name "inputfilename outputfilename"
Write-Host " Example:"$MyInvocation.MyCommand.Name" 'inputfile.txt' 'outputfile.txt' "
Write-Host " Example:"$MyInvocation.MyCommand.Name" -inputfile 'inputfile.txt' -outputfile 'outputfile.txt' "
Write-Host
Break
}

$inputfile = $args[0]
$outputfile = $args[1]
$date = get-date -format "yyyy-MM-dd"
$Content = "#Date;Computer;Hostname;IP`r`n"
$Content | Out-File -encoding ASCII $outputfile
[Array] $arrComputers = @( )

if ($inputfile) {
if (!(GetFileIntoArr -strFilename $inputfile -arrFile ([Ref]$arrComputers))) {
Break
}
} else {
Write-Host "ERROR: you need a filename.txt as second parameter inputfile"
Break
}
Foreach ($computer in $arrComputers) {
$nslookup = NSlookup($computer)
$line = $date + ";" + $computer + ";" + $nslookup
#+ "`r`n"
Write-Host $line
$line | Out-File -append -encoding ASCII $outputfile
}


Example of Result:

#Date;Computer;Hostname;IP
2011-06-01;COMPUTER1;computer1.dns1.my.net;192.1.1.45;COMPUTER1.dns2.my.net
2011-06-01;COMPUTER2;COMPUTER2.si.net;192.1.1.100
2011-06-01;COMPUTER3;COMPUTER3.si.net;192.1.1.101
2011-06-01;COMPUTER4;COMPUTER4.si.net;192.1.102