Jump to content

I need some help cleaning up a script function..


Recommended Posts

I have this function that I only need it to do 2 things. I need it to get the list of computers from active directory, wherever they may be in the directory, and then export them to a file called nodes2.txt. I have the following that does this but i know there has got to be a neater way of doing it than with the slop i have put together.. BUT the one thing I need fixed(changed) is that I also want it to be able to get the information for me from active directory WITHOUT me having to put the domain name in the script, as I want to be able to use it in different domains, I will paste my script below and would welcome anyone who could maybe tidy up my script function to be able to autodetect the domain, connect to active directory and export a list of all the computer accounts to a file.. Which is what is here, but with the exception of the fact that I have to specify my domain name (which I don't want) and I know my fuction has way too much slop.. Thanks!!

[/code]
Func EnumerateAd()
FileDelete("Nodes2.txt")
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=Domain,dc=Local>"
Local $strFilter = "(&(objectCategory=computer)(objectClass=user)(sAMAccountName=*))"
;Local $strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))"
Local $strAttributes = "cn,sAMAccountName,Name,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
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)
    FileWriteLine("Nodes2.txt",$objRecordSet.Fields ("Name").Value)
    $objRecordSet.MoveNext
WEnd
$objConnection.Close
$objConnection = ""
$objCommand = ""
$objRecordSet = ""
FileClose("Nodes2.txt")
EndFunc
[code]
Edited by Stealth111
Link to comment
Share on other sites

I have figured out the solution to my problem and am posting the revised function, if you see something in the function that can be eliminated or better way of performing function, please feel free to post any ideas..

[/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);displayName,Name;subtree"
    
    $objCommand.CommandText = $strQuery
    $objCommand.Properties("Page Size") = 100
    $objCommand.Properties("Timeout") = 30
    $objCommand.Properties("Cache Results") = False
    
    $objRecordSet = $objCommand.Execute
    While Not $objRecordSet.EOF
        $strComputerDN = $objRecordSet.Fields("Name").Value

    FileWriteLine("Nodes2.txt",$strComputerDN)
    $objRecordSet.MoveNext
WEnd
$objConnection.Close
$objConnection = ""
$objCommand = ""
$objRecordSet = ""
FileClose("Nodes2.txt")
[code]
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...