{"id":8291,"date":"2012-08-13T00:01:00","date_gmt":"2012-08-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/13\/use-powershell-to-search-ad-ds-and-produce-an-uptime-report\/"},"modified":"2012-08-13T00:01:00","modified_gmt":"2012-08-13T00:01:00","slug":"use-powershell-to-search-ad-ds-and-produce-an-uptime-report","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-search-ad-ds-and-produce-an-uptime-report\/","title":{"rendered":"Use PowerShell to Search AD DS and Produce an Uptime Report"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to Search Active Directory Domain Services for servers and produce an uptime report.<\/p>\n<p><img decoding=\"async\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" \/>&nbsp;Hey, Scripting Guy! I enjoyed your series of blogs last week, and I consider uptime reports to be a vital topic. In fact, this was the very task I was assigned to do in my new job&mdash;write a script to obtain an uptime report and a free disk space report. I tried in VBScript and it was too complicated. Eventually, I hit upon your blog. The problem is that we have a large domain, and creating a list of servers is not practical. I looked at using other tools to query Active Directory, but they bring back too much garbage. I need an easier solution. Can you help?<\/p>\n<p>&mdash;BB<\/p>\n<p><img decoding=\"async\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" \/>&nbsp;Hello BB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. There are many ways to searchActive Directory Domain Services (AD DS) from within Windows PowerShell. The techniques range from downloading and installing non-Microsoft tools, to installing the admin tools and quering a Windows Server 2008 R2 domain controller, to installing the Active Directory Management Service, to another technique. In fact, there are at least four ways to query AD DS without installing anything. Today I am going to look at using the ADSISearcher to search Active Directory and return only servers. I will then use this technique with the uptime report from last week.<\/p>\n<h2>Using the ADSISearcher<\/h2>\n<p>To bring back a listing of all computers in Active Directory, I use a command similar to the one here.<\/p>\n<p style=\"padding-left: 30px\">([adsisearcher]&#8221;objectcategory=computer&#8221;).findall()<\/p>\n<p>The command can be broken into several pieces, but I often type it as a single command for simplicities sake. The command is three parts:<\/p>\n<p>The adsi searcher:<\/p>\n<p style=\"padding-left: 30px\">[adsisearcher]<\/p>\n<p>The filter:<\/p>\n<p style=\"padding-left: 30px\">&#8220;objectcategory=computer&#8221;<\/p>\n<p>The command to find all matches:<\/p>\n<p style=\"padding-left: 30px\">findall()<\/p>\n<p>The object returning from the <b>findall<\/b><i> <\/i>method is a DirectorySearches.SearchResult object. It contains two properties: the <b>path<\/b> and the <b>properties<\/b> properties. An example of the command and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.HSG-8-13-12-01.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.HSG-8-13-12-01.png\" \/><\/a><\/p>\n<p>To find the names of the computers, I use the <b>path<\/b><i> <\/i>property with the <b>[adsi]<\/b> type accelerator, and I retrieve the <b>cn<\/b><i> <\/i>property. An example of such a command is shown here.<\/p>\n<p style=\"padding-left: 30px\">([adsisearcher]&#8221;objectcategory=computer&#8221;).findall() | ForEach {([adsi]$_.path).cn}<\/p>\n<p>To find the operating system, I use the <b>OperatingSystem<\/b> property from Active Directory. I can see the <b>OperatingSystem<\/b> information by using a command such as the one shown here.<\/p>\n<p style=\"padding-left: 30px\">([adsisearcher]&#8221;objectcategory=computer&#8221;).findall() | ForEach {([adsi]$_.path).operatingsystem}<\/p>\n<p>The results from the previous command appear in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2273.HSG-8-13-12-02.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2273.HSG-8-13-12-02.png\" \/><\/a><\/p>\n<p>All of the Server operating systems contain the word <b>server<\/b><i> <\/i>in the name. Therefore, I can use a compound LDAP query and filter out computers that contain the name <b>server<\/b><i> <\/i>in the name. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">([adsisearcher]&#8221;(&amp;(objectcategory=computer)(OperatingSystem=*server*))&#8221;).findall()<\/p>\n<p>To retrieve only the server names from the previous command, I pipe the results to the <b>ForEach-Object<\/b> cmdlet, use the <b>[adsi]<\/b> type accelerator, and select the <b>cn<\/b><i> <\/i>property. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">([adsisearcher]&#8221;(&amp;(objectcategory=computer)(OperatingSystem=*server*))&#8221;).findall() |&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Foreach-Object {([adsi]$_.path).cn}<\/p>\n<p>I store the retrieved servers in an array as shown here.<\/p>\n<p style=\"padding-left: 30px\">[array]$servers = ([adsisearcher]&#8221;(&amp;(objectcategory=computer)(OperatingSystem=*server*))&#8221;).findall() |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach-object {([adsi]$_.path).cn}<\/p>\n<p>When I run the script, the report in the image that follows appears.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4341.HSG-8-13-12-03.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4341.HSG-8-13-12-03.png\" \/><\/a><\/p>\n<p><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Query-AD-for-servers-and-65822e29\">I uploaded the complete script to the Scripting Guys Script Repository<\/a>. You can refer to that script for more details.<\/p>\n<p>&nbsp;BB, that is all there is to using Windows PowerShell to query Active Directory and to produce an uptime and disk free space report. Join me tomorrow for more Windows PowerShell cool 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><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to Search Active Directory Domain Services for servers and produce an uptime report. &nbsp;Hey, Scripting Guy! I enjoyed your series of blogs last week, and I consider uptime reports to be a vital topic. In fact, this was the very task I was assigned to do in my [&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,3,303,45],"class_list":["post-8291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to Search Active Directory Domain Services for servers and produce an uptime report. &nbsp;Hey, Scripting Guy! I enjoyed your series of blogs last week, and I consider uptime reports to be a vital topic. In fact, this was the very task I was assigned to do in my [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8291","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=8291"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8291\/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=8291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}