{"id":17321,"date":"2010-08-25T00:01:00","date_gmt":"2010-08-25T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/08\/25\/query-active-directory-and-ping-each-computer-in-the-domain-by-using-powershell\/"},"modified":"2010-08-25T00:01:00","modified_gmt":"2010-08-25T00:01:00","slug":"query-active-directory-and-ping-each-computer-in-the-domain-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/query-active-directory-and-ping-each-computer-in-the-domain-by-using-powershell\/","title":{"rendered":"Query Active Directory and Ping Each Computer in the Domain by Using PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Query Active Directory and ping each computer in the domain by using Windows PowerShell. The Scripting Guys show you how.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/p>\n<p>Hey, Scripting Guy! I am THE IT person at my company. I am also a chemical engineer who has daily production responsibilities in our process plant. You see, I am supposed to be a part-time IT person, the problem is my collateral duty is rapidly becoming my primary duty. I feel as if I should become a MCSE, but it should not be this hard. I have been learning Windows PowerShell because I think it will make my job easier. The first thing I need to do is to find out what computers are on the network. We are running Windows Server 2008, and cannot upgrade to Windows Server 2008 R2 because our hardware will not support a 64 bit OS. Therefore, the Windows PowerShell Active Directory cmdlets you have been talking about are not available to me. I think if I could get a list of all the computers in our domain and send them a ping to find out if they actually exist it would be helpful. Can this be done?<\/p>\n<p>&#8212; CM<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/><\/p>\n<p>Hello CM, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. School has officially started. The yellow school buses are trolling the neighborhood looking for passengers; newly minted students are clinging anxiously to their parent&rsquo;s hands as they struggle to choose the correct bus for the prospective passenger in hand. The teacher across the street headed out early this morning with a strong sense of purpose and mission tattooed to her face. I am merely an observer to this annual ritual; watching the transactions that take place in view of my upstairs window. I have the Alan Parsons Project cranked up on my Zune HD, and as the parents pass off the passengers to the yellow conveyances, they seem to dance to the beat of unheard tunes &ndash; sort of like the film of the astronauts who first walked on the surface of the moon. <\/p>\n<p>CM, in IT, we are often called upon to do things we have never done before. It does not mean that we have to go boldly where no IT pro has ever gone before, but for us the experience may be new. Most of the time, when we get to our destination, we realize we were not the first to ever accomplish the feat. The trick is to find that information prior to the task, and seek advice beforehand. One great place to find assistance is the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. A great place to find scripts, of all kinds, is the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/\">Script Center Script Repository<\/a>. <\/p>\n<p>This week we are looking at using the <strong>[adsisearcher]<\/strong> type accelerator to search Active Directory Domain Services (AD DS). I decided to write a script that returns a list of all computers in the domain, and then pings each one to see if it responds or not. The complete Get-ADComputersTestConnection.ps1 script is seen here. <\/p>\n<blockquote>\n<p class=\"CodeBlockScreenedHead\"><span style=\"font-size: x-small\"><strong><span style=\"font-size: small\">Get-ADComputersTestConnection.ps1<\/span><\/strong><\/span><\/p>\n<p><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">Function Get-ADComputersTestConnection<br \/>{<br \/><span>&nbsp;<\/span>Param(<br \/><span>&nbsp; <\/span>[switch]$showErrors<br \/><span>&nbsp;<\/span>)<br \/><span>&nbsp;<\/span>([adsisearcher]&#8221;objectcategory=computer&#8221;).findall() |<br \/><span>&nbsp;<\/span>ForEach-Object {<br \/><span>&nbsp; <\/span>try <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>{ <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Test-Connection -ComputerName ([adsi]$_.path).cn -BufferSize 16 `<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-Count 1 -TimeToLive 1 -EA stop }<br \/><span>&nbsp; <\/span>Catch [system.exception]<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>{<span>&nbsp; <\/span><br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>if($showErrors)<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{ $error[0].tostring() } <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>}<br \/><span>&nbsp;<\/span>} #end foreach-object<br \/>} #End function Get-ADComputersTestConnection<\/p>\n<p># *** EntryPoint to Script ***<\/p>\n<p>Get-ADComputersTestConnection -showErrors<\/span><\/p>\n<\/blockquote>\n<p>CM, I decided to ensconce the code in a function named <strong>Get-ADComputersTestConnection<\/strong>. This function, does not really meet my requirements for a function, in that it is rather specialized, not configurable, and limited in its reuse capability. For example, because the function queries for all computers in the domain, it will have limited appeal for use in large domains. However, I did not want to clutter the function with lots of confusing options, and parameters. In addition, the use of the <strong>Test-Connection<\/strong> cmdlet does not expose the myriad options available for utilizing that cmdlet. Other than that, the <strong>Get-ADComputersTestConnection<\/strong> function is relatively cool. <\/p>\n<p>CM, the Get-ADComputersTestConnection.ps1 script simply calls the <strong>Get-ADComputersTestConnection<\/strong> function and passes the <strong>showErrors<\/strong> switch. This switch causes the <strong>Get-ADComputersTestConnection<\/strong> function to display status information about computers that are online, as well as the error that is generated when the computer is not online. If you feel that most of the time when you use the script, you only want information on computers that are online you should remove the switch. If, on the other hand, you want to know the status of all computers, you will use the script exactly as written. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4834.HSG08251001_7E6B03CB.jpg\"><img decoding=\"async\" height=\"434\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0121.HSG08251001_thumb_70B847C3.jpg\" alt=\"Image of script having run\" border=\"0\" title=\"Image of script having run\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>If you see yourself needing both capabilities, on a regular basis, the script will need a command line switch. There are only two changes for the Get-ADComputersTestConnectionSwitch.ps1 script. The first is the command line parameter to allow the showing of the errors. This is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Param([switch]<\/span><span style=\"color: #2b91af\">$showErrors<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>The next change is the logic at the entry point of the script to detect if the command line switch has been used when launching the script. A simple if &hellip; else statement is used. If the <strong>$showErrors <\/strong>variable is present on the Windows PowerShell variable drive, it means the script was launched with the <strong>&ndash;showErrors <\/strong>command line parameter. The if &hellip; else statement checks for the existence of this variable. If the <strong>$showErrors<\/strong> variable is found, the Get-<strong>ADComputersTestConnection<\/strong> function is called while passing the <strong>&ndash;showErrors<\/strong> parameter. Once the function completes, the script exists. If the variable is not found, the function is called with no parameters. This code is seen here. <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$showErrors<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-ADComputersTestConnection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-showErrors<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Else<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-ADComputersTestConnection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<\/blockquote>\n<p>The modification to use the script with a command line switch is shown here. <\/p>\n<blockquote>\n<p><strong>Get-ADComputersTestConnectionSwitch.ps1<\/strong><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">Param([switch]$showErrors)<\/p>\n<p>Function Get-ADComputersTestConnection<br \/>{<br \/><span>&nbsp;<\/span>Param(<br \/><span>&nbsp; <\/span>[switch]$showErrors<br \/><span>&nbsp;<\/span>)<br \/><span>&nbsp;<\/span>([adsisearcher]&#8221;objectcategory=computer&#8221;).findall() |<br \/><span>&nbsp;<\/span>ForEach-Object {<br \/><span>&nbsp; <\/span>try <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>{ <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Test-Connection -ComputerName ([adsi]$_.path).cn -BufferSize 16 `<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-Count 1 -TimeToLive 1 -EA stop }<br \/><span>&nbsp; <\/span>Catch [system.exception]<br \/><span>&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>{<span>&nbsp; <\/span><br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>if($showErrors)<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{ $error[0].tostring() } <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>}<br \/><span>&nbsp;<\/span>} #end foreach-object<br \/>} #End function Get-ADComputersTestConnection<\/p>\n<p># *** Entry Point to Script ***<\/p>\n<p>if($showErrors) { Get-ADComputersTestConnection -showErrors ; exit }<br \/>Else { Get-ADComputersTestConnection ; exit }<\/span><\/span> <\/p>\n<\/blockquote>\n<p>When the Get-ADComputersTestConnectionSwitch.ps1 script is run with no parameters, the status of each computer in AD DS is displayed. Errors in testing the connection to the remote computers are not displayed. When the script is run with the <strong>&ndash;showErrors<\/strong> parameter, both the connection status and errors are displayed. The command line syntax and sample output for each is shown in the following image.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7128.HSG-08-25-10-02.jpg\" alt=\"Image of command-line syntax and sample output\" style=\"border: 0px initial initial\" \/><\/p>\n<p>Inside the <strong>Get-ADComputersTestConnection<\/strong> function, after the parameter statement, the first thing that is done is the <strong>[adsisearcher] <\/strong>type accelerator is used to search AD DS. The <strong>&ldquo;objectcategory=computer&rdquo;<\/strong> filter is used, and the <strong>findall<\/strong> method is called to return all instances of computer objects in AD DS. This section of code was discussed in detail in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/08\/24\/use-the-powershell-adsisearcher-type-accelerator-to-search-active-directory.aspx\">yesterday&rsquo;s Hey Scripting Guy! Blog post<\/a>. <\/p>\n<p>Inside a pipeline, when you need to work on one item at a time, use the <strong>Foreach-Object<\/strong> cmdlet. You will often see its&rsquo; alias on blogs &ldquo;%&rdquo; or occasionally you will see it referred to by its&rsquo; other alias <strong>foreach<\/strong>. While the first alias is simply obscure, the second alias is downright confusing because <strong>foreach<\/strong> is also a keyword for the <strong>foreach<\/strong> statement. If the two commands worked the same, it would be merely inconvenient, but for people attempting to learn Windows PowerShell, they expect <strong>foreach<\/strong> to always be the <strong>foreach<\/strong> statement, and that is not the case. <\/p>\n<p>The ForeachComputer.ps1 script is an example about which I am talking. <\/p>\n<blockquote>\n<p><strong>ForeachComputer.ps1<\/strong><\/p>\n<p><span style=\"color: #2b91af\"><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">$computers = ([adsisearcher]&#8221;objectcategory=computer&#8221;).findall() <br \/>foreach ($computer in $computers)<br \/>{<br \/><span>&nbsp;<\/span>$computer.path<br \/>}<\/span><\/span> <\/p>\n<\/blockquote>\n<p>On the first line of the script, the results of the directory searcher query are stored in the <strong>$computers<\/strong> variable. Because this is a collection of objects, it is necessary to use the <strong>foreach<\/strong> statement to work through the collection to display the path of each computer. The <strong>foreach<\/strong> statement is comprised of three parts. The first is the variable to use as the placeholder (enumerator) in the collection. The second part is the collection itself. These first parts go inside a set of parentheses. The script block (code block) is placed inside a pair of curly brackets (braces). The code in the script block is executed on each item in the collection. That item is referenced by the placeholder variable. <\/p>\n<p>The <strong>foreach-object<\/strong> cmdlet is always used in a pipeline. The <strong>foreach-object<\/strong> statement does not use the set of parentheses. It uses the <strong>$_<\/strong> automatic variable to work with each object as it comes down the pipeline. The results from the two scripts are the same. One advantage of using the pipeline, and the <strong>foreach-object<\/strong> cmdlet is the code is more compact; there is no need to create a variable to store the results into, nor is there a need to create a variable for the placeholder as the script walks through the collection of computers. The Foreach-ObjectComputer.ps1 script is seen here. <\/p>\n<blockquote>\n<p><strong>Foreach-objectComputer.ps1<\/strong><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">([adsisearcher]&#8221;objectcategory=computer&#8221;).findall() |<br \/>ForEach-Object {<br \/><span>&nbsp;<\/span>$_.path<br \/>}<\/span><\/span> <\/p>\n<\/blockquote>\n<p>Which approach is faster? Using my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/27\/hey-scripting-guy-weekend-scripter-timing-the-windows-powershell-pipeline.aspx\">Test-TwoScripts.ps1 script<\/a>, on my network the Foreach-objectComputer.ps1 script is 26% faster. You will need to test to see if you achieve similar results on your network. <\/p>\n<p>Inside the <strong>foreach-object<\/strong> block of the Get-ADComputersTestConnection.ps1 script, the first thing that is done, is the try statement is used to attempt to use the Test-Connection cmdlet to send a PING to the remote computer. The problem, is the result of the <strong>[adsisearcher]<\/strong> is a search result, not a directory entry object. Therefore, access to the normal ADSI types of properties are not available. One thing that can be access is the path. This is seen here. <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\"><\/p>\n<p class=\"CodeBlock\" style=\"margin: 4pt 0in 7pt\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">PS C:\\&gt; $computers = ([adsisearcher]&#8221;objectcategory=computer&#8221;).findall()<br \/>PS C:\\&gt; ($computers[0]).gettype()<\/p>\n<p>IsPublic IsSerial Name<span>&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; <\/span>BaseType<br \/>&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&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;<\/span>&#8212;&#8212;&#8211;<br \/>True<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>False<span>&nbsp;&nbsp;&nbsp; <\/span>SearchResult<span>&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; <\/span>System.Object<\/p>\n<p>PS C:\\&gt; $computers[0].path<br \/>LDAP:\/\/CN=HYPERV,OU=Domain Controllers,DC=NWTraders,DC=Com<br \/>PS C:\\&gt;<\/span><\/span><\/span><\/p>\n<p><\/span><span style=\"color: #808080\"> <\/span><\/div>\n<\/blockquote>\n<p>This path is exactly what is required to obtain a <strong>directoryentry<\/strong> object from the <strong>[adsi]<\/strong> type accelerator. This is seen here. <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\"><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">PS C:\\&gt; [adsi]$computers[0].path<\/p>\n<p>distinguishedName : {CN=HYPERV,OU=Domain Controllers,DC=NWTraders,DC=Com}<br \/>Path<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <\/span>: LDAP:\/\/CN=HYPERV,OU=Domain Controllers,DC=NWTraders,DC=Com<\/p>\n<p>PS C:\\&gt; ([adsi]$computers[0].path).gettype()<\/p>\n<p>IsPublic IsSerial Name<span>&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; <\/span>BaseType<br \/>&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp; &#8212;-<span>&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; <\/span>&#8212;&#8212;&#8211;<br \/>True<span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<\/span>False<span>&nbsp;&nbsp;&nbsp; <\/span>DirectoryEntry<span>&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;<\/span>System.ComponentModel&#8230;.<\/p>\n<p>PS C:\\&gt;<\/span><\/span><span style=\"color: #808080\"> <\/span><\/div>\n<\/blockquote>\n<p>The code inside the parentheses seen above returns a <strong>DirectoryEntry<\/strong> object. It can be used to access the normal ADSI attributes. The <strong>CN<\/strong> attribute is retrieved from the first computer in the computers collection. This is seen here. <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">([adsi]<\/span><span style=\"color: #2b91af\">$computers<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">].path).cn<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #000000\">HYPERV<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span>&nbsp;&nbsp;<\/div>\n<\/blockquote>\n<p>The <strong>cn<\/strong> attribute is the name of the computer that can be pinged. If the computer is not found, the <strong>erroraction<\/strong> preference is set to stop (<strong>EA<\/strong> is an alias for the <strong>erroraction<\/strong> parameter). By using stop here, it will force the script to enter the catch block. The try block is seen here.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\"><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">try <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>{ <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Test-Connection -ComputerName ([adsi]$_.path).cn -BufferSize 16 `<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-Count 1 -TimeToLive 1 -EA stop }<\/span><\/span><\/p>\n<p><\/span><\/div>\n<\/blockquote>\n<p>The catch block is used to catch any system exception. This is needed because the <strong>Test-Connection<\/strong> cmdlet returns a rather nasty looking error when a computer is not found. This is seen here. <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\"><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">PS C:\\&gt; Test-Connection bogus -BufferSize 16 -Count 1<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">Test-Connection : Testing connection to computer &#8216;bogus&#8217; failed: The requested name<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">is valid, but no data of the requested type was found<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">At line:1 char:16<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">+ Test-Connection &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>bogus -BufferSize 16 -Count 1<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: ResourceUnavailable: (bogus:String) [Test-Connection]<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;&nbsp; <\/span>, PingException<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\"><span>&nbsp;&nbsp; <\/span>.TestConnectionCommand<\/span><\/span>&nbsp;<\/div>\n<\/blockquote>\n<p>This error is stored inside the <strong>$error<\/strong> variable. The <strong>$error<\/strong> variable holds an array of errors, with the most recent error always occupying position [0]. To retrieve the most recent error, you access <strong>$error[0]<\/strong> as seen here.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\"><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">PS C:\\&gt; $error[0]<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">Test-Connection : Testing connection to computer &#8216;bogus&#8217; failed: The requested name<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">is valid, but no data of the requested type was found<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">At line:1 char:16<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">+ Test-Connection &lt;&lt;&lt;&lt;<span>&nbsp; <\/span>bogus -BufferSize 16 -Count 1<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: ResourceUnavailable: (bogus:String) [Test-Connection]<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;&nbsp; <\/span>, PingException<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\"><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\"><span>&nbsp;&nbsp; <\/span>.TestConnectionCommand<\/span><\/span>&nbsp;<\/div>\n<\/blockquote>\n<p>In this example, the error shown in <strong>$error[0]<\/strong> looks pretty much like the one that was displayed to the Windows PowerShell console. The <strong>$error<\/strong> variable contains really rich objects, however, and there is a lot of information that can be obtained by working through the error object model. However, to obtain a more friendly looking error, all I need to do is call the <strong>tostring()<\/strong> method as seen here.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\"><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">PS C:\\&gt; $error[0].tostring()<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\"><span style=\"font-size: 10pt\"><span style=\"color: #000000\"><span style=\"font-family: Lucida Sans Typewriter\">Testing connection to computer &#8216;bogus&#8217; failed: The requested name is valid, but no d<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">ata of the requested type was found<\/span><\/span>&nbsp;<\/div>\n<\/blockquote>\n<p>I decided to display the error by using the <strong>tostring()<\/strong> method because the information returned is actually helpful in determining why the computer is not responsive. For example, in using <strong>test-connection<\/strong> to test a connection to a computer named bogus, it says the name is valid, but no data of the requested type was found. Other errors that are returned are likewise informative. Here is the catch portion of the script. <\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\"><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">Catch [system.exception]<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>{<span>&nbsp; <\/span><br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>if($showErrors)<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{ $error[0].tostring() } <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span> <\/div>\n<\/blockquote>\n<p>CM, that is all there is to using Windows PowerShell to search AD DS. Searching Active Directory Domain Services week will continue tomorrow when we talk about <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/ad\/search\/default.mspx?mfr=true\">searching AD<\/a> DS for computers,. We would love for you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n<p><strong><br \/><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Query Active Directory and ping each computer in the domain by using Windows PowerShell. The Scripting Guys show you how. &nbsp; Hey, Scripting Guy! I am THE IT person at my company. I am also a chemical engineer who has daily production responsibilities in our process plant. You see, I am supposed to be [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,68,51,3,4,8,45],"class_list":["post-17321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-error-handling","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-searching-active-directory","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Query Active Directory and ping each computer in the domain by using Windows PowerShell. The Scripting Guys show you how. &nbsp; Hey, Scripting Guy! I am THE IT person at my company. I am also a chemical engineer who has daily production responsibilities in our process plant. You see, I am supposed to be [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17321","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17321"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17321\/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=17321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}