{"id":11051,"date":"2012-02-24T00:01:00","date_gmt":"2012-02-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/02\/24\/use-powershell-to-test-connectivity-on-remote-servers\/"},"modified":"2012-02-24T00:01:00","modified_gmt":"2012-02-24T00:01:00","slug":"use-powershell-to-test-connectivity-on-remote-servers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-test-connectivity-on-remote-servers\/","title":{"rendered":"Use PowerShell to Test Connectivity on Remote Servers"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy talks about using Windows PowerShell to test connectivity after a network configuration change.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It seems that there are always good news\/bad news<i> <\/i>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<i>, <\/i>but GREAT NEWS<i>. <\/i>Now for the bad news<i> <\/i>part of the equation&hellip;<\/p>\n<p>The router they showed up with at the house&mdash;part of the free installation&mdash;would not accept a static IP address on the Ethernet side of the equation that was compatible with my current network configuration&mdash;and I have a relatively complex configuration that involves multiple subnets.<\/p>\n<h2>Writing a quick script to ensure connectivity<\/h2>\n<p>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 <b>$servers<\/b>. To walk through the array of servers, I used the <b>ForEach<\/b><i> <\/i>statement. The <b>$s<\/b> variable represents the actual computer name inside the loop. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">$servers = &#8220;dc1&#8243;,&#8221;dc3&#8243;,&#8221;sql1&#8243;,&#8221;wds1&#8243;,&#8221;ex1&#8221;<\/p>\n<p style=\"padding-left: 30px\">Foreach($s in $servers)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p>To ping the computers, I use the <b>Test-Connection <\/b>cmdlet. In the definition of the <b>Test-Connection <\/b>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 <i>ea <\/i>parameter (<i>ea <\/i>is an alias for the <i>ErrorAction <\/i>parameter) value of 0. The value of 0 tells the cmdlet to not display error information (an error displays when a ping is unsuccessful).<\/p>\n<p><b>Note&nbsp;&nbsp;&nbsp;<\/b>For more information about using the <b>Test-Connection<\/b>, cmdlet refer to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/11\/19\/query-ad-for-computers-and-use-ping-to-determine-status.aspx#comments\" target=\"_blank\">Query AD for Computers and Use Ping to Determine Status<\/a>.<\/p>\n<p>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.<\/p>\n<p style=\"padding-left: 30px\">if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p>Because I am hiding errors that are returned from the <b>Test-Connection <\/b>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 <b>NSLookup<\/b> on the server. Finally, I ping the computer again. This portion of the script is shown here.<\/p>\n<p style=\"padding-left: 30px\">&#8220;Problem connecting to $s&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Flushing DNS&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ipconfig \/flushdns | out-null<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Registering DNS&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ipconfig \/registerdns | out-null<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;doing a NSLookup for $s&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; nslookup $s<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Re-pinging $s&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;Problem still exists in connecting to $s&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE {&#8220;Resolved problem connecting to $s&#8221;} #end if<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; } # end if<\/p>\n<p style=\"padding-left: 30px\">} # end foreach<\/p>\n<p>When I ran the PingTest.ps1 script on my computer, the following output appeared in the Windows PowerShell ISE.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5383.hsg-2-24-12-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5383.hsg-2-24-12-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The complete PingTest.ps1 script is shown here.<\/p>\n<p style=\"padding-left: 30px\"><strong>PingTest.PS1<\/strong><\/p>\n<p style=\"padding-left: 30px\">$servers = &#8220;dc1&#8243;,&#8221;dc3&#8243;,&#8221;sql1&#8243;,&#8221;wds1&#8243;,&#8221;ex1&#8221;<\/p>\n<p style=\"padding-left: 30px\">Foreach($s in $servers)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Problem connecting to $s&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Flushing DNS&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ipconfig \/flushdns | out-null<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Registering DNS&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ipconfig \/registerdns | out-null<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &#8220;doing a NSLookup for $s&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; nslookup $s<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8220;Re-pinging $s&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; if(!(Test-Connection -Cn $s -BufferSize 16 -Count 1 -ea 0 -quiet))<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&#8220;Problem still exists in connecting to $s&#8221;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE {&#8220;Resolved problem connecting to $s&#8221;} #end if<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; } # end if<\/p>\n<p style=\"padding-left: 30px\">} # end foreach<\/p>\n<p>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.<\/p>\n<p><b>Note<\/b>&nbsp;&nbsp;&nbsp;For more information about writing to text files, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/17\/scripting-wife-learns-to-work-with-text-files.aspx\" target=\"_blank\">The Scripting Wife Learns to Work with Text Files<i>.<\/i><\/a><\/p>\n<p>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.<\/p>\n<p><b>Note&nbsp;&nbsp;&nbsp;<\/b>For more information about Windows PowerShell remoting, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/remoting\/\" target=\"_blank\">these Hey, Scripting Guy! blogs<\/a>.<\/p>\n<p>I invite you back tomorrow for the Weekend Scripter, when I will talk about copying Windows PowerShell Scripts to a network share.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,218,188,37,3,4,45,77],"class_list":["post-11051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-dhcp-server","tag-dns-server","tag-networking","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11051"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11051\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}