Sign in to follow this  
Followers 0
RagnaroktA

Enumerate AD Servers on Domain

8 posts in this topic

Will this code work properly when converted?

The original vb script:

Option Explicit

Dim objRootDSE, strDNSDomain, objConnection, objCommand, strQuery
Dim objRecordSet, strComputerDN, strOS

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory for all computers.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

strQuery = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer);distinguishedName,operatingSystem;subtree"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute

' Enumerate computer objects with server operating systems.
Do Until objRecordSet.EOF
  strOS = objRecordSet.Fields("operatingSystem")
  If InStr(UCase(strOS), "SERVER") > 0 Then
    strComputerDN = objRecordSet.Fields("distinguishedName")
    Wscript.Echo strComputerDN
  End If
  objRecordSet.MoveNext
Loop

' Clean up.
objConnection.Close
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing

Wscript.Echo "Done"

My (probably borked) converted AutoIt code:

; Determine DNS domain name from RootDSE object.
    $objRootDSE = ObjGet("LDAP://RootDSE")
    $strDNSDomain = $objRootDSE.Get("defaultNamingContext")
    
; Use ADO to search Active Directory for all computers.
    $objCommand = ObjCreate("ADODB.Command")
    $objConnection = ObjCreate("ADODB.Connection")
    $objConnection.Provider = "ADsDSOObject"
    $objConnection.Open "Active Directory Provider"
    $objCommand.ActiveConnection = $objConnection
    
    $strQuery = "<LDAP://" & $strDNSDomain & ">;(objectCategory=computer);distinguishedName,operatingSystem;subtree"
    
    $objCommand.CommandText = $strQuery
    $objCommand.Properties("Page Size") = 100
    $objCommand.Properties("Timeout") = 30
    $objCommand.Properties("Cache Results") = False
    
    $objRecordSet = $objCommand.Execute
    
; Enumerate computer objects with server operating systems.
    Do
      $strOS = $objRecordSet.Fields("operatingSystem")
      If StringInStr(StringUpper($strOS), "SERVER") > 0 Then
        $strComputerDN = $objRecordSet.Fields("distinguishedName")
        ConsoleWrite($strComputerDN)
      EndIf
      $objRecordSet.MoveNext
    Until $objRecordSet.EOF
    
; Clean up.
    $objConnection.Close

Will it convert and work? If so, what am I doing wrong? Thanks!


Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki

Share this post


Link to post
Share on other sites



There's nothing glaringly wrong syntax-wise.

What happens when you try to run it? Does the vbs script work for you?

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

There's nothing glaringly wrong syntax-wise.

What happens when you try to run it? Does the vbs script work for you?

Dale

When run, the VBScript will enumerate all servers on the domain and list them in the console. (I use PrimalScript, so it's putting the list in the Output window.)

The au3 seems to just exit with the following exit code:

Exit code: 0 , 0000h

So nothing seems to be wrong with it persey... it's just not listing the domain servers...

Edited by RagnaroktA

Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki

Share this post


Link to post
Share on other sites

I'd suggest some basic troubleshooting to narrow down where the problem is.

Use isObj() to test your object variables. If your COM commands fail they typically will not be. Check your recordset .Count to see if it actually gets filled with anything etc.

Narrow down where the trouble lies and then you can focus on specific operations.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Did you ever get this script debugged? I'm trying to do a similar script, but am lost...

Option Explicit

Dim objRootDSE, strDNSDomain, objConnection, objCommand, strQuery
Dim objRecordSet, strComputerDN, strOS
......

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

Did you ever get this to work? I can get the VB script to work, but can't see what's wrong with the AutoIT script.

Have you incorporated it into your Remote Administration Suite? (This is looking very impressive by the way).

My requirement is to compare records of PCs built using Symantec iCommand, recorded in Active Directory and Centennial, and see which ones still exist and are still in use! I'm fairly happy with extracting the info from other sources, but getting the list of PCs from multiple OUs in 4 domains is a bit of a pain. The VB script shows it can be done. One extra bit of info I will need to track down is the last time the computer password (not user) was changed within AD. I can get a manual export using Hyena, but doing it programmatically would be great. Ideally I'd like to delete redundant computers, which will be the last stage. Lots and lots of requirements. I keep trawling the AutoIT forums, picking up bits here and there, and reading MSDN, but I've still not solved it yet. To give you some idea of the scale of my problem - I can find references to nearly 8,000 PCs, and yet we should only have less than 5,000. So which 3,000 PCs should be deleted? Get it wrong and up to 3,000 users could be complaining...

Share this post


Link to post
Share on other sites

This is how you would search users in AD

Local $objCommand = ObjCreate("ADODB.Command")
Local $objConnection = ObjCreate("ADODB.Connection")

$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

Local $strBase = "<GC://dc=YOURDOMAINNAME,dc=COM>"
Local $strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))"
Local $strAttributes = "cn,sAMAccountName,displayName,sn,distinguishedName"
Local $strQuery = $strBase & ";" & $strFilter & ";" & $strAttributes & ";subtree"


$objCommand.CommandText = $strQuery
$objCommand.Properties ("Page Size") = 100
$objCommand.Properties ("Timeout") = 30
$objCommand.Properties ("Cache Results") = False
$ADS_SCOPE_SUBTREE = 2
$objCommand.Properties ("searchscope") = $ADS_SCOPE_SUBTREE

Local $objRecordSet = $objCommand.Execute

ConsoleWrite("Users"&@CR&"---------"&@CR)

While Not $objRecordSet.EOF
         $strName = $objRecordSet.Fields ("sAMAccountName").Value
         $strCN = $objRecordSet.Fields ("cn").value
         $strdisplayName = $objRecordSet.Fields ("displayName").value
         $strSN = $objRecordSet.Fields ("SN").value
         $strdistinguishedName = $objRecordSet.Fields ("distinguishedName").value
    ;FileWriteLine("Users.txt",$objRecordSet.Fields ("sAMAccountName").Value)
        ConsoleWrite($objRecordSet.Fields ("sAMAccountName").Value&@cr)
    $objRecordSet.MoveNext
WEnd

$objConnection.Close
$objConnection = ""
$objCommand = ""
$objRecordSet = ""

regards

ptrex

Share this post


Link to post
Share on other sites

or add the objget("LDAP//RootDSE") to connect to the default domain (the domain where you are connected)


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0