Solved

ADSI Script to extract Member Servers

Posted on 2006-07-20
4
673 Views
Last Modified: 2012-06-27
Hi All,

I making some scripts prior to a planned domain migration. In this phase I need to retrieve reports on domain structure, objects and configuration. I'm looking for a script to extract the following:

.All Domain Controllers in the domain
.All Member servers in the domain
.All Exchange servers in the domain
.All Web servers in the domain (They are running IIS)
.All Desktops in the domain
.All Laptops in the domain

I have decided to search the active directory and have put together the following which returns me all the computer objects. But how do I filter results via the above ? I need the CN of the servers.

'-----------------------Start of Script------------------------------
'Make a connection to Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection


Set objRootDSE = GetObject("LDAP://RootDSE")
strConfig = objRootDSE.Get("defaultNamingContext")

strBase = "<LDAP://" & strConfig & ">"
strFilter = "(objectclass=computer)"
strAttributes = "samaccountname,adspath,distinguishedname"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

objCommand.Commandtext= strQuery

Set objRecordset = objCommand.Execute

counter=0

objRecordset.MoveFirst
Do until objRecordset.EOF
      set objGroup = getObject(objRecordset.fields("adspath").value)
      'wscript.echo objGroup.SID
      wscript.echo objRecordset.fields("distinguishedname").value&"+"&objRecordset.fields("samaccountname").value
      objRecordset.movenext
      counter=counter+1
Loop

Msgbox ("Number of computer objects :"&counter)
'-----------------------End of Script------------------------------
0
Comment
Question by:gtrivedi
  • 3
4 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 17147707

Some parts of that are quite tricky.

> .All Domain Controllers in the domain

These are going to be about the easiest to find - they can be identified directly from Active Directory - although the quickest way is going to be going through the configuration subtree rather than the domain subtree. However, that won't quite give you the Distinguished Name associated with the account location - is that something you really need for DCs?

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT distinguishedName, aDSPath FROM " & _
      "'LDAP://" & objRootDSE.Get("configurationNamingContext") & _
      "' WHERE objectClass='nTDSDSA'"
Set objRootDSE = Nothing

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
      Wscript.Echo "Computer Name: " & _
      objRecordSet.Fields("distinguishedName").Value
      objRecordSet.MoveNext
Loop

> .All Member servers in the domain

Member Servers can be tentatively identified from their Operating System Name - after all, it's about the only indicator you have. This is a property of the account (unsurprisingly) called operatingSystem, if you were to add that to your original query you could test against it.

> .All Exchange servers in the domain

These don't have a specific role listed in AD, however, they do make an appearance under the Configuration subtree. We can use the same type of query as we did for the Domain Controllers:

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT distinguishedName, aDSPath FROM " & _
      "'LDAP://" & objRootDSE.Get("configurationNamingContext") & _
      "' WHERE objectClass='msExchExchangeServer'"
Set objRootDSE = Nothing

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
      Wscript.Echo "Computer Name: " & _
      objRecordSet.Fields("distinguishedName").Value
      objRecordSet.MoveNext
Loop

> .All Web servers in the domain (They are running IIS)

Nothing about the Web Service is recorded in AD - you would have to run a query against the machine name itself to determine whether IIS was there. You could use WMI for that:

Const WBEM_FLAG_IMMEDIATELY = &h10
Const WBEM_FORWARD_ONLY = &h20

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", _
      WBEM_FLAG_IMMEDIATELY + WBEM_FORWARD_ONLY)

For Each objService In colServices
      If objService.Name = "W3SVC" Then
            WScript.Echo strComputer & ": Is a Web Server"
      End If
Next

Where strComputer is the name of the computer you want to test.

> .All Desktops in the domain

Potentially the Operating System Name again, you could at least narrow it down that way.

> .All Laptops in the domain

This one is hardware level, I haven't a clue how you could get this. You may be able to use WMI to extract enough information for you to identify it, but there's certainly nothing in ADSI that would help you.

You would have to wrap around a fairly bit of code to produce an accurate list in one script, and I can help you look into doing that if you would like, but for now at least it's time for me to go home.

Chris
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 17147721

I should have noted that without a bit of planning all your Exchange Servers will also be flagged as Web Servers since they'll be running the W3SVC - in that case the test for Web Servers isn't quite as accurate as we'd like.

Chris
0
 

Author Comment

by:gtrivedi
ID: 17152918
Hey Chris,

Thanks for the reply. I have been tryint to comprehend the information contained in the configuration partion. Although I got the domain controllers name (thanks for that), do you know where I can find more information on how to dig into configuration partition of active directory using ADSI?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 17159683

The best place to start is ADSIEdit (part of the Windows 200x Support Tools set - adsiedit.msc). It presents all of them as trees you can explore quite happily, it will allow you to open up the properties of any object and see what attributes it has available.

Once you've had a little look at that hopefully you'll see that it's possible to dig into the configuration partition as easily as the domain partition.

Chris
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now