Use PowerShell to Test Connectivity on Remote Servers

Use PowerShell to Test Connectivity on Remote Servers

  • Comments 11
  • Likes

Summary: Microsoft Scripting Guy talks about using Windows PowerShell to test connectivity after a network configuration change.

Microsoft Scripting Guy, Ed Wilson, is here. It seems that there are always good news/bad news types of scenarios. After months of trying to obtain faster Internet connectivity at the house, I finally found a source that doubled the upload and download speeds for the Scripting Wife and I, at literally half the price we had been paying. Talk about a deal. Talk about not just good news, but GREAT NEWS. Now for the bad news part of the equation…

The router they showed up with at the house—part of the free installation—would not accept a static IP address on the Ethernet side of the equation that was compatible with my current network configuration—and I have a relatively complex configuration that involves multiple subnets.

Writing a quick script to ensure connectivity

When I ran into problems, I decided to write a really quick script to ping the most critical servers on my network to ensure connectivity. I wanted to ensure that DNS resolution worked, so I did the ping by name instead of by IP address. I manually created an array of computer names that I stored in a variable called $servers. To walk through the array of servers, I used the ForEach statement. The $s variable represents the actual computer name inside the loop. This is shown here.

$servers = "dc1","dc3","sql1","wds1","ex1"

Foreach($s in $servers)

{

To ping the computers, I use the Test-Connection cmdlet. In the definition of the Test-Connection command, I determine the buffer size, the number of pings to submit, and whether it returns detailed ping status information or a simple Boolean value. I specify the ea parameter (ea is an alias for the ErrorAction parameter) value of 0. The value of 0 tells the cmdlet to not display error information (an error displays when a ping is unsuccessful).

Note   For more information about using the Test-Connection, cmdlet refer to Query AD for Computers and Use Ping to Determine Status.

Because the cmdlet returns a Boolean value, I am able to simplify the code a bit. I therefore state that if the command does not return a True value, I want to perform additional actions. This portion of the script is shown here.

if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))

  {

Because I am hiding errors that are returned from the Test-Connection cmdlet, I decided to display my own status information. I print out a message that lets me know a problem occurs with reaching the computer. I then decide to run three commands. The first flushes the DNS cache, the second updates the DNS registration, and the third performs an NSLookup on the server. Finally, I ping the computer again. This portion of the script is shown here.

"Problem connecting to $s"

   "Flushing DNS"

   ipconfig /flushdns | out-null

   "Registering DNS"

   ipconfig /registerdns | out-null

   "doing a NSLookup for $s"

   nslookup $s

   "Re-pinging $s"

     if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))

      {"Problem still exists in connecting to $s"}

       ELSE {"Resolved problem connecting to $s"} #end if

   } # end if

} # end foreach

When I ran the PingTest.ps1 script on my computer, the following output appeared in the Windows PowerShell ISE.

Image of command output

The complete PingTest.ps1 script is shown here.

PingTest.PS1

$servers = "dc1","dc3","sql1","wds1","ex1"

Foreach($s in $servers)

{

  if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))

  {

   "Problem connecting to $s"

   "Flushing DNS"

   ipconfig /flushdns | out-null

   "Registering DNS"

   ipconfig /registerdns | out-null

  "doing a NSLookup for $s"

   nslookup $s

   "Re-pinging $s"

     if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))

      {"Problem still exists in connecting to $s"}

       ELSE {"Resolved problem connecting to $s"} #end if

   } # end if

} # end foreach

This script took me less than five minutes to write, and it saved me much more time than that if I had to do a lot of manual troubleshooting. If I needed the output to a text file, I would have called the script from the Windows PowerShell console and redirected the output to a text file. I did not need to do that, but it would have been simple.

Note   For more information about writing to text files, see The Scripting Wife Learns to Work with Text Files.

If I needed to run the script from each of the critical servers (so they could check for connectivity to each other), I would have copied the script to a network share, and then used Windows PowerShell remoting to run the script on each of the servers. I would also have redirected the output to a text file, but in this example, I would have used a text file on the same network share as the script.

Note   For more information about Windows PowerShell remoting, see these Hey, Scripting Guy! blogs.

I invite you back tomorrow for the Weekend Scripter, when I will talk about copying Windows PowerShell Scripts to a network share.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Awesome little script! :D

  • Hi Ed,

    thank you for this new tool for my toolkit!

    I think it is useful in some situations and could well replace the usual Test-connection or Ping command!

    Klaus (Schulte)

  • I added some color and a outline for sucessfull test. Hope it is OK for you:

    $servers = "srv01","srv02","srv03","dc01"

    Foreach($s in $servers)

    {

     if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))

     {

      write-host "Problem connecting to $s" -foregroundcolor “yellow”

      "Flushing DNS"

      ipconfig /flushdns | out-null

      "Registering DNS"

      ipconfig /registerdns | out-null

     "doing a NSLookup for $s"

      nslookup $s

      "Re-pinging $s"

        if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))

         {write-host "Problem still exists in connecting to $s" -foregroundcolor “red”}

          ELSE {write-host "Resolved problem connecting to $s" -foregroundcolor “green”}  #end if

      } # end if

    ELSE {write-host "No pproblems with: $s" -foregroundcolor “green”}

    } # end foreach

  • Thanks for posting the script ;)

  • What about running Test-Connection multi-threaded. I am noticing that it doesn't allow another connection to the same node. Any insight?

  • @chace See this Hey Scripting Guy blog posting: blogs.technet.com/.../use-powershell-workflow-to-ping-computers-in-parallel.aspx

  • Thanks Ed!

  • not always right. I used this to test a remote server and got incorrect result. reason: If I ping my server with -v 4, it return ok but without -v 4, it returns ipv6 and fails. wish test-connection had -v 4 option.

  • @ Free

    -v TOS         Type Of Service (IPv4-only. This setting has been deprecated

                  and has no effect on the type of service field in the IP Header).

  • my point was, when i ping one of my servers it failed with -v 6 and succeeded  with -v 4 but test-connection always fails and there is no way to force test-connection to use ipv4

  • @Fred

    Just ask for the IPv4 address only on a lookup:

    $hostname='omega'

    $ipv4=([system.net.dns]::GetHostAddresses($hostname)|?{!$_.IsIPv6LinkLocal}).IPAddressToString

    Test-Connection $ipv4