Use PowerShell to Search AD DS and Produce an Uptime Report

Use PowerShell to Search AD DS and Produce an Uptime Report

  • Comments 4
  • Likes

Summary: Learn how to use Windows PowerShell to Search Active Directory Domain Services for servers and produce an uptime report.

Hey, Scripting Guy! Question 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—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?

—BB

Hey, Scripting Guy! Answer Hello BB,

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.

Using the ADSISearcher

To bring back a listing of all computers in Active Directory, I use a command similar to the one here.

([adsisearcher]"objectcategory=computer").findall()

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:

The adsi searcher:

[adsisearcher]

The filter:

"objectcategory=computer"

The command to find all matches:

findall()

The object returning from the findall method is a DirectorySearches.SearchResult object. It contains two properties: the path and the properties properties. An example of the command and the associated output are shown in the image that follows.

Image of command output

To find the names of the computers, I use the path property with the [adsi] type accelerator, and I retrieve the cn property. An example of such a command is shown here.

([adsisearcher]"objectcategory=computer").findall() | ForEach {([adsi]$_.path).cn}

To find the operating system, I use the OperatingSystem property from Active Directory. I can see the OperatingSystem information by using a command such as the one shown here.

([adsisearcher]"objectcategory=computer").findall() | ForEach {([adsi]$_.path).operatingsystem}

The results from the previous command appear in the following image.

Image of command output

All of the Server operating systems contain the word server in the name. Therefore, I can use a compound LDAP query and filter out computers that contain the name server in the name. The command is shown here.

([adsisearcher]"(&(objectcategory=computer)(OperatingSystem=*server*))").findall()

To retrieve only the server names from the previous command, I pipe the results to the ForEach-Object cmdlet, use the [adsi] type accelerator, and select the cn property. This command is shown here.

([adsisearcher]"(&(objectcategory=computer)(OperatingSystem=*server*))").findall() | 

Foreach-Object {([adsi]$_.path).cn}

I store the retrieved servers in an array as shown here.

[array]$servers = ([adsisearcher]"(&(objectcategory=computer)(OperatingSystem=*server*))").findall() |

      foreach-object {([adsi]$_.path).cn}

When I run the script, the report in the image that follows appears.

Image of command output

I uploaded the complete script to the Scripting Guys Script Repository. You can refer to that script for more details.

 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.

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Is there a reason we would use the ADSI rather than Get-ADcomputer?

    Using ADSI seems less straightforward than using the cmdlets that come with the active directory module.

  • @Neil using [adsisearcher] is already built in, and perhaps access to either the RSAT tools, or the cmdlets might not be accessible. In fact tomorrow I will talk about using the Get-ADComputer cmdlet (I will actually use the LDAP query derived today :-) ) Bottom line, if you work with AD very much AND you know ADSI you are always in great shape. But there are many other tools that minimize this need. The great thing about PowerShell is you can use what you are comfortable with doing.

  • How could I modify this script to give me all the child domain DC's as well? We have and empty forest root with 16 childs and would like to get this report for all the root and child domains too if possible.

    Thank you.

  • thanks