Nslookup in Powershell


This script is needed when we have multiple ip’s, and we need to know which server to which it belongs.
Instructions:
Powershell_Script_Nslookup.ps1 servers_IP_List.txt output_file_list.txt

# Start Script #

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
}
}

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

#
# Start here 😀
#

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
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
}

# End of script #

Advertisements

One thought on “Nslookup in Powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s