{"id":12041,"date":"2011-11-19T00:01:00","date_gmt":"2011-11-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/19\/query-ad-for-computers-and-use-ping-to-determine-status\/"},"modified":"2011-11-19T00:01:00","modified_gmt":"2011-11-19T00:01:00","slug":"query-ad-for-computers-and-use-ping-to-determine-status","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/query-ad-for-computers-and-use-ping-to-determine-status\/","title":{"rendered":"Query AD for Computers and Use Ping to Determine Status"},"content":{"rendered":"<p><strong>Summary:<\/strong> Learn how to use Windows PowerShell to query Active Directory for computers, ping for status, and display in green\/red.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. While the Scripting Wife and I were out in California speaking to the Microsoft Premier Field Engineers (PFE) about Windows PowerShell, a question arose. The PFE said he had a customer that needed to send a ping to a number of computers. The customer did not need a lot of information about the status, but more of a simple yes or no report on connectivity. Ping was the tool of choice due to simplicity. The problem is that Ping returns too much information. What the customer really wanted was an output that displayed the computer name in green (for up) and in red (for down). A simple status board to display the computer names. One problem, the PFE related, is that the collection of computers to monitor changes on nearly a daily basis. Therefore, the customer wants to query Active Directory for the computer names.<\/p>\n<p>We left the reception Thursday night, and I went up to the room and put together this script. It could have been a &ldquo;one-liner&rdquo; but, it would have been difficult to read, so I spread it out over a couple of different lines. Here is the complete Query Active Directory and Ping Computers script in Windows PowerShell (for ease of use, I uploaded the script to the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Query-Active-Directory-And-fbf2eba8\">Scripting Guys Script Repository<\/a>):<\/p>\n<p style=\"padding-left: 30px\"><b>Query Active Directory and Ping Computers<\/b><\/p>\n<p style=\"padding-left: 30px\">Import-Module active*<\/p>\n<p style=\"padding-left: 30px\">$rtn = $null<\/p>\n<p style=\"padding-left: 30px\">Get-ADComputer -Filter * |<\/p>\n<p style=\"padding-left: 30px\">ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet<\/p>\n<p style=\"padding-left: 30px\">&nbsp; IF($rtn -match &#8216;True&#8217;) {write-host -ForegroundColor green $_.dnshostname}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; ELSE { Write-host -ForegroundColor red $_.dnshostname }<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>The first thing the Query Active Directory and Ping Computers script does is import the <b>ActiveDirectory<\/b> module. I have written about this module quite a bit. In the <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/install-active-directory-management-service-for-easy-powershell-access\/\">Install Active Directory Management Service for Easy PowerShell Access<i> <\/i>post<\/a>, I go into detail about the requirements to set up the Active Directory Management gateway. Next, I set the value of the <b>$rtn<\/b> variable to <b>$null<\/b>. This helps to avoid problems if there is already a <b>$rtn<\/b> variable with a different value.<\/p>\n<p>I then use the <b>Get-ADComputer<\/b> cmdlet from the <b>ActiveDirectory<\/b> module (I have used the <b>Get-ADComputer<\/b> cmdlet numerous times on the <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=Get-ADComputer&amp;sections=7618\">Hey, Scripting Guy! Blog<\/a>) to return all computers. The following line of code returns an <b>ADComputer<\/b> object from the <b>Microsoft.ActiveDirectory.Management<\/b> namespace:<\/p>\n<p style=\"padding-left: 30px\">Get-ADComputer -Filter *<\/p>\n<p>By default, the <b>ADComputer<\/b> object contains only a few properties. The default properties are shown here:<\/p>\n<p style=\"padding-left: 30px\">DistinguishedName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">UserPrincipalName&nbsp;&nbsp;<\/p>\n<p>Other properties exist on a computer object. For example, the following figure illustrates properties that contain a value for the W7Client computer. The figure from ADSI Edit is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3515.hsg-11-19-11-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3515.hsg-11-19-11-1.png\" alt=\"Image of properties containing a value for W7Client computer\" title=\"Image of properties containing a value for W7Client computer\" \/><\/a><\/p>\n<p>To obtain access to additional property values, I need to add them to the <i>property <\/i>parameter (the reason for the small subset of properties returned by default is because of performance concerns). Here is an example of returning the <b>DNSHostname<\/b> and the <b>lastlogon<\/b> attribute values.<\/p>\n<p style=\"padding-left: 30px\">Get-ADComputer -Filter * -Properties lastlogon | select dnshostname, lastlogon<\/p>\n<p>Unfortunately, the <b>Test-Connection<\/b> cmdlet does not accept piped input, and I need to use the <b>Foreach-Object<\/b> cmdlet to walk through the collection of <b>ADComputer<\/b> objects and ping the <b>DNSHostname<\/b> of each computer. To speed things along, I send one ping and reduce the <b>BufferSize<\/b>. I store the results in a variable named <b>$rtn<\/b>:<\/p>\n<p style=\"padding-left: 30px\">ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 &ndash;Quiet<\/p>\n<p>The Test-Connection returns a <b>True<\/b><i> <\/i>or a <b>False<\/b><i> <\/i>depending upon whether the connection succeeds. But unfortunately, it is a string and not a true <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.boolean.aspx\">Boolean value<\/a>. Therefore, in my decision portion of the script, I used the <b>IF<\/b> statement and did a match on the string <b>True<\/b><i>. <\/i>If <b>Test-Connection<\/b> returns true, I display the DNSHostname of the computer in green. Otherwise, I display the DNSHostname of the computer in red. This portion of the script is shown here:<\/p>\n<p style=\"padding-left: 30px\">ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet<\/p>\n<p style=\"padding-left: 30px\">&nbsp; IF($rtn -match &#8216;True&#8217;) {write-host -ForegroundColor green $_.dnshostname}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; ELSE { Write-host -ForegroundColor red $_.dnshostname }<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>The script and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1261.hsg-11-19-11-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1261.hsg-11-19-11-2.png\" alt=\"Image of script and associated output\" title=\"Image of script and associated output\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Well, that is about all there is to querying Active Directory Domain Services for computer accounts, and pinging them to see if they are up and running or not. Join me tomorrow for more cool Windows PowerShell stuff.<\/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\">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>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p><b>&nbsp;<\/b><\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to query Active Directory for computers, ping for status, and display in green\/red. &nbsp; Microsoft Scripting Guy Ed Wilson here. While the Scripting Wife and I were out in California speaking to the Microsoft Premier Field Engineers (PFE) about Windows PowerShell, a question arose. The PFE said he [&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":[7,46,25,3,4,61,45],"class_list":["post-12041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to query Active Directory for computers, ping for status, and display in green\/red. &nbsp; Microsoft Scripting Guy Ed Wilson here. While the Scripting Wife and I were out in California speaking to the Microsoft Premier Field Engineers (PFE) about Windows PowerShell, a question arose. The PFE said he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12041","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=12041"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12041\/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=12041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}