Powershell | Check PING, RDP, and NSLookup for Bulk Server | Easy Way

Powershell
How to check ping
Check ping Powershell
Check RDP Powershell

In this Covid19 pandemic, I hope you all are doing well and safe!!

Today we will discuss a PowerShell  script considering the following scenario,

Scenario Details:

     Let’s say you performed a change/activity in your infrastructure wherein you need to test the server connectivity (Ping), remote desktop port 3389 is listening (RDP) and DNS A record is resolving (NSLookup) for multiple servers

So from a safer side, you need to get the result prior and post the activity.

Based on the above scenario we will write our script step by step.

First– We need to collect all our server names and put them in a file. ( Here C:\temp\server.txt)

          And then read/fetch the content and keep it in a variable

So the script line would be as following 

    $list = gc -Path C:\temp\server.txt

Second– Now we will start a loop for each server, create  a custom property and store the result in a variable

 foreach ($server in $list){

$result = Test-NetConnection -ComputerName $server -Port 3389

$output  = [PSCustomObject]@{


"Hostname" = $server
"Ping_Pass_?" = $result.PingSucceeded
"RDP_Pass_?" = $result.TcpTestSucceeded
"DNS_Resolved_?" = $result.NameResolutionSucceeded


}

$output | select -Property Hostname,Ping_Pass_?,RDP_Pass_?,DNS_Resolved_?

}

Third – Now we will write the output to a csv file. So the complete script would be as below

$list = gc -Path C:\temp\server.txt

$output = ""

foreach ($server in $list){

$result = Test-NetConnection -ComputerName $server -Port 3389

$output  = [PSCustomObject]@{


"Hostname" = $server
"Ping_Pass_?" = $result.PingSucceeded
"RDP_Pass_?" = $result.TcpTestSucceeded
"DNS_Resolved_?" = $result.NameResolutionSucceeded


}

$output | select -Property Hostname,Ping_Pass_?,RDP_Pass_?,DNS_Resolved_? | Export-Csv C:\temp\ping_rdp_nslookup_result.csv -Append

}

The Powershell script can be found at following Github repo for Download

https://github.com/Jagat45106/SysAdmin_PSTools

File Name: PSTool_Ping_RDP_NSLookup_Check.ps1

Following is the result for reference that exported to a CSV file

Hope you enjoyed the blog!!! Please share if you liked it