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.
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
0 comments