Musings on the splendour of PowerShell One-Liner: Get Windows Server 2003 Server Count - PoSh Chap - Site Home - TechNet Blogs

PoSh Chap

Musings on the splendour of PowerShell

One-Liner: Get Windows Server 2003 Server Count

One-Liner: Get Windows Server 2003 Server Count

  • Comments 2
  • Likes

Extended support for Windows Server 2003 ends on July 14, 2015...

 


   In Search of Lost Time

 

Hopefully, you've already migrated away from this operating system. If you haven't and you're looking to understand the scale of the task facing you, the following one-liner examples may prove useful.

This one will work with all versions of PowerShell and gives you a count of Windows Server 2003 operating systems from the Active Directory instance that you are logged on to:

 

([System.DirectoryServices.DirectorySearcher]"(&(&(sAMAccountType=805306369)(objectCategory=computer)(operatingSystem=Windows\20Server\202003*)))").FindAll().Count



System.DirectoryServices.DirectorySearcher
is the TypeName of the .NET DirectorySearcher class. As the name suggests, we can use this to search Active Directory. The square brackets let us access the class and we pass an LDAP search string that filters on Windows 2003 Servers. The FindAll() method of the class is called with dot notation to retrieve objects from AD matching the LDAP filter. We then use more dot notation to display the information contained in the Count property, i.e. all Windows Server 2003 instances found.

The next example is exactly the same, except that we access System.DirectoryServices.DirectorySearcher by using the ADSISearcher Type Accelerator. Think of a Type Accelerator as a shortcut to an underlying TypeName. The following example will work with v2 of PowerShell and later.

 

([ADSISearcher]"(&(&(sAMAccountType=805306369)(objectCategory=computer)(operatingSystem=Windows\20Server\202003*)))").FindAll().Count

 

Finally, let's use the Get-ADComputer cmdlet, introduced with v2 of PowerShell, to apply the same LDAP filter. This one looks less complicated.

 

(Get-ADComputer -LdapFilter "(&(&(sAMAccountType=805306369)(objectCategory=computer)(operatingSystem=Windows\20Server\202003*)))").Count

  

And, what about that LDAP filter? Basically, we're asking for ALL of the following conditions to be met:

  • the sAMAccountType=805306369 (SAM_MACHINE_ACCOUNT)
  • objectCategory=computer (self explanatory; an objectCategory search is more efficient than an objectClass search BTW)
  • operatingSystem=Windows\20Server\202003* (notice the wildcard to include all Windows Server 2003 versions)

 

Finally, take a look at my Get-ADServerCount function. This will produce a count of different operating system types for active (password changed within the last 90 days) servers.

http://gallery.technet.microsoft.com/scriptcenter/Get-ADServerCount-Function-6cb3e704

 

Comments
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment