Solved

ADSI Script to extract Member Servers

Posted on 2006-07-20
4
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 71

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 71

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 71

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

707 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