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 #

Connectivity to Dc´s


You need PORTQRY and a txt file to enter the hostnames of the servers or IPs.
Just copy the script below and save it as batch file.

Enjoy.

:::::::::::::::::::::::::::: BEGIN SCRIPT :::::::::::::::::::::::::

@ECHO OFF
:: Test connectivity.
:: using PORTQRY utility.
:: The SERVERS.TXT contains a list of servers (one server per line)
:: to check connectivity to.
ECHO     DATE: %DATE% > PORTQRY_%COMPUTERNAME%.TXT
ECHO     TIME: %TIME% >> PORTQRY_%COMPUTERNAME%.TXT
ECHO     USER: %USERNAME% >> PORTQRY_%COMPUTERNAME%.TXT
ECHO COMPUTER: %COMPUTERNAME% >> PORTQRY_%COMPUTERNAME%.TXT
ECHO. >> PORTQRY_%COMPUTERNAME%.TXT
ECHO. >> PORTQRY_%COMPUTERNAME%.TXT
ECHO. >> PORTQRY_%COMPUTERNAME%.TXT
FOR /F “tokens=1” %%i in (servers.txt) DO (
ECHO ::::::::::::::::::::::  %%i  :::::::::::::::::::::::::: >> PORTQRY_%COMPUTERNAME%.TXT
ECHO Testing %%i
ECHO. >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 88 -p TCP | findstr /i “88”  >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 53 -p TCP | findstr /i “53” >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 137 -p TCP | findstr /i “137” >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 138 -p TCP | findstr /i “138” >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 139 -p TCP | findstr /i “139” >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 445 -p TCP | findstr /i “445” >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 389 -p TCP | findstr /i “389” >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 3268 -p TCP | findstr /i “3268”  >> PORTQRY_%COMPUTERNAME%.TXT
PORTQRY -n %%i -e 3269 -p TCP | findstr /i “3269” >> PORTQRY_%COMPUTERNAME%.TXT

ECHO. >> PORTQRY_%COMPUTERNAME%.TXT
ECHO. >> PORTQRY_%COMPUTERNAME%.TXT
)

:::::::::::::::::::::::: END SCRIPT :::::::::::::::::::::::::