Welcome to Scripting Forums Sign in | Join | Help
in Search

Active directory into SQL

Last post 09-11-2008, 12:13 PM by AbqBill. 17 replies.
Page 1 of 2 (18 items)   1 2 Next >
Sort Posts: Previous Next
  •  04-25-2008, 3:46 PM 29585

    Active directory into SQL

    I have been fighting with this for so long.  I have a VB script that will export all the user and information to a text file so I can import into SQL and run all of my security audits that I wrote.  The problem is that this script binds to root and requires domain admin access.  My new job will not give me any access higher than domain user.  I do not know how to write the script so I can get the data with read only rights.

    Now the other option I hear is to do it all in SQL and have SQL pull the ADSI data directly into a table.

    Could somebody please help me out?  I need to get this work done.

     Thank you,

     

    Darla

     


    Darla Friel
  •  04-30-2008, 4:51 PM 29600 in reply to 29585

    Re: Active directory into SQL

    Hi dfriel,

    Please post the code you're using that isn't working, and the exact error message you're getting, and we might be able to help.

    Bill

  •  05-01-2008, 2:37 PM 29608 in reply to 29600

    Re: Active directory into SQL

    Thank you Bill,

    This is the code that I am using.  It will pull back one user only.  I need all accounts, any where in SD.  The account am using has domain user access only.

     

    ------------------------------------------------

    On Error Resume Next
    ' Option Explicit
    Dim strquery
    Dim strdbpath
    Dim objrecordset
    Dim objitem


    ' WRITE TO LOG FILE
    set FSO = CreateObject("Scripting.FileSystemObject")
    Set ResultFile = FSO.CreateTextFile ("C:\AD_Scripts\Populate.txt")
    Set ResultFile = FSO.OpenTextFile ("C:\AD_Scripts\Populate.txt", 2)
    arrComputers = Array("server.company.net")
    For Each strComputer In arrComputers


    ' - Connect to AD
    Set objCommand = CreateObject("ADODB.Command")
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    objCommand.ActiveConnection = objConnection
    strQuery = "<;(objectCategory=person/">LDAP://dc=Company,dc=Net>;(objectCategory=person);" & "adsPath;subtree"
    objCommand.CommandText = strQuery
    Set objRecordSet = objCommand.Execute


    Do Until objRecordSet.EOF

    Set objItem  = GetObject(objRecordSet.Fields ("adspath"))

          ResultFile.write ("sAMAccountName " & ";")  
          ResultFile.write ("distinguishedName " & ";")
          ResultFile.write ("displayName " & ";")
          ResultFile.write ("givenName " & ";")
          ResultFile.write ("LastName " & ";")
          ResultFile.write ("telephoneNumber " & ";")
          ResultFile.write ("mobile " & ";")
          ResultFile.write ("userAccountControl " & " |")
    '-----------------------------------------
    '-----------------------------------------
    ' - Loop through all of the users and insert their details into the DB
     Do Until objRecordSet.EOF

    While Not tsTextFile.AtEndOfStream
          ResultFile.write (objItem.sAMAccountName & " ;")
          ResultFile.write (objItem.distinguishedName & " ;")
          ResultFile.write (objItem.displayName & " ;")
          ResultFile.write (objItem.givenName & " ;")
          ResultFile.write (objItem.sn & " ;")
          ResultFile.write (objItem.telephoneNumber & " ;")
          ResultFile.write (objItem.mobile & " ;")
          ResultFile.write (";" & objItem.userAccountControl & "|")

     

      objRecordSet.MoveNext

    Wend
    loop
     loop
    Next

    -------------------------------------------------------------


    Darla Friel
  •  05-05-2008, 10:28 AM 29626 in reply to 29608

    Re: Active directory into SQL

    Hi dfriel,

    I wrote a quick JScript script that queries my AD domain for some of the attributes you listed, and it ran fine as a standard domain user. Here's a copy of the script I wrote:

    var defaultNC, connection, command, rs;
    defaultNC = GetObject("LDAP://rootDSE").Get("defaultNamingContext");
    connection = new ActiveXObject("ADODB.Connection");
    connection.Provider = "ADsDSOObject";
    connection.Open();
    command = new ActiveXObject("ADODB.Command");
    command.ActiveConnection = connection;
    command.Properties.Item("Cache results") = false;
    command.Properties.Item("Page size") = 1000;
    command.CommandText = "<LDAP://" + defaultNC + ">;" +
      "(&(objectCategory=person)(objectClass=user));" +
      "sAMAccountName,distinguishedName,displayName,givenName," +
      "sn,telephoneNumber,userAccountControl" +
      ";subtree";
    
    rs = command.Execute();
    while (! rs.EOF) {
      WScript.Echo('"' + rs.Fields.Item("sAMAccountName") + '",' +
        '"' + rs.Fields.Item("distinguishedName")  + '",' +
        '"' + rs.Fields.Item("displayName")        + '",' +
        '"' + rs.Fields.Item("givenName")          + '",' +
        '"' + rs.Fields.Item("sn")                 + '",' +
        '"' + rs.Fields.Item("telephoneNumber")    + '",' +
        '"' + rs.Fields.Item("userAccountControl") + '"');
      rs.MoveNext();
    }
    

    I'm note sure what some of your code is doing, but the sample code above does a serverless bind to rootDSE to determine the default naming context, and then it queries AD using ADO using the specified attributes.

    HTH,

    Bill

  •  05-05-2008, 12:10 PM 29629 in reply to 29626

    Re: Active directory into SQL

    Thank you.  That does work.  However, I am not familiar with Jscript at all.  How would I make this write to a file?I need it in a file so I can import it into SQL.
    Darla Friel
  •  05-05-2008, 12:25 PM 29630 in reply to 29629

    Re: Active directory into SQL

    You could simply redirect the output i.e.

    csript programX.js > MyFile.cvs

    where program X is the name of your script and MyFile.cvs is where you want the output stored.

     

  •  05-05-2008, 4:30 PM 29631 in reply to 29630

    Re: Active directory into SQL

    Hi dfriel,

    RKirchhof has the answer -- the script I wrote generates CSV data, so you can simply redirect its output to a CSV file. Make sure you run it with CScript is CScript is not your default host.

    HTH,

    Bill

  •  05-05-2008, 6:55 PM 29633 in reply to 29630

    Re: Active directory into SQL

    Thank you, Thank you, everybody.

    Where is the best place to learn more about jscript.  It look much more simple the VB.

     


    Darla Friel
  •  05-06-2008, 7:45 AM 29634 in reply to 29633

    Re: Active directory into SQL

    I like W3Schools http://www.w3schools.com/js/default.asp although it's more web

    oriented.

  •  05-06-2008, 12:48 PM 29637 in reply to 29634

    Re: Active directory into SQL

    Would you know how to add in the group membership for each user, into this same script?  When I followed the same format as the rest of the script, I am getting a null value for the group membership.  I also changed the script to be separtated semi colon between the colums because the data contains " ,"

     


    var defaultNC, connection, command, rs;defaultNC = GetObject("LDAP://rootDSE").Get("defaultNamingContext");
    connection = new ActiveXObject("ADODB.Connection");
    connection.Provider = "ADsDSOObject";
    connection.Open();command = new ActiveXObject("ADODB.Command");


    command.ActiveConnection = connection;
    command.Properties.Item("Cache results") = false;
    command.Properties.Item("Page size") = 1000;
    command.CommandText = "<LDAP://" + defaultNC + ">;" +
      "(&(objectCategory=person)(objectClass=user));" +
      "sAMAccountName,distinguishedName,displayName,givenName," +
      "sn,telephoneNumber,mobile,whenChanged,whenCreated,userAccountControl,memberOf" +
      ";subtree";
    rs = command.Execute();
    while (! rs.EOF) {
      WScript.Echo('"' + rs.Fields.Item("sAMAccountName") + '";' +
        '"' + rs.Fields.Item("distinguishedName")   + '";' +
        '"' + rs.Fields.Item("displayName")         + '";' +
        '"' + rs.Fields.Item("givenName")           + '";' +
        '"' + rs.Fields.Item("sn")                  + '";' +
        '"' + rs.Fields.Item("telephoneNumber")     + '";' +
        '"' + rs.Fields.Item("mobile")  + '";' +
        '"' + rs.Fields.Item("whenChanged")     + '";' +
        '"' + rs.Fields.Item("whenCreated")  + '";' +
        '"' + rs.Fields.Item("userAccountControl") + '";' +
        '"' + rs.Fields.Item("memberOf")  + '"|');
      rs.MoveNext();
    }


    Darla Friel
  •  05-09-2008, 11:15 AM 29650 in reply to 29637

    Re: Active directory into SQL

    Hi dfriel,

    I'm not sure it's possible to query every possible attribute using ADO. I would recommend looking perhaps at a different tool, such as the dsquery command-line tool.

    Bill 

  •  05-09-2008, 12:25 PM 29652 in reply to 29650

    Re: Active directory into SQL

    Here is a batch file I wrote to return the group membership(s) of a user.

    Copy into notepad and save as dsg.bat

    @Echo off
    If (%1)==() Goto Help
    If Exist Groups1.txt Del Groups1.txt
    dsquery  user -name %1  > Location.Txt
    dsget user < Location.txt -memberof >Groups.txt
    FOR /F "tokens=1 delims=," %%G IN (groups.txt) DO @echo %%G% >>Groups2.txt
    echo %1's Location in Active Directory has been found. >Groups1.txt
    type Location.txt >> Groups1.txt
    Echo This is the Group Membership report for %1.>>Groups1.txt
    FOR /F "tokens=2 delims==" %%G IN (groups2.txt) DO @echo %%G% >>Groups1.txt
    del Groups2.txt
    explorer groups1.txt
    goto End
    :Help
    echo Robert's Group Membership List Creator
    echo Will not list Expanded group membership. Use DSG-E.BAT
    echo To use, type dsg User Name (In quotes) i.e. dgs "RKirchhof"
    echo For Example: dsg "First Last" (First.Last is the naming convention in my domain)
    :End

    This one includes expanded member ships .  i.e. User is in a group thats in a group ... 
    Copy into notepad and save as dsg-e.bat

    @Echo off
    If (%1)==() Goto Help
    If Exist Groups1.txt Del Groups1.txt
    dsquery  user -name %1  > Location.Txt
    dsget user < Location.txt -memberof -expand >Groups.txt
    rem del Location.txt
    FOR /F "tokens=1 delims=," %%G IN (groups.txt) DO @echo %%G% >>Groups2.txt
    echo %1's Location in Active Directory has been found. >Groups1.txt
    type Location.txt >> Groups1.txt
    Echo This is the Group Membership report for %1.>>Groups1.txt
    FOR /F "tokens=2 delims==" %%G IN (groups2.txt) DO @echo %%G% >>Groups1.txt
    del Groups2.txt
    explorer groups1.txt
    goto End
    :Help
    echo Robert's Group Membership List Creator
    echo Expanded (Will list all groups to which the user belongs)
    echo To use, type dsg-e User Name (In quotes)  i.e. dgs-e "RKirchhof"
    echo For Example: dsg-e "First Last" (First.Last is the naming convention in my domain)
    :End

    Here is a neat one. It copies the members of GroupA to GroupB
    Save as CopyMBRS.bat

    @echo off
    If {%1} == {} goto Help
    for /F  "delims=" %%x in ('Dsquery Group -Name %1') do set DNS=%%x
    for /F  "delims=" %%x in ('Dsquery Group -Name %2') do set DND=%%x
    dsget group %DNS% -members > MBRS.Txt
    FOR /F "delims=" %%h in (MBRS.Txt) do dsmod group %DND% -addmbr %%h
    goto End
    :Help
    @echo Copies the members of Group A to Group B
    @echo Syntax:  CopyMbrs Sources Destination
    @echo Example: CopyMbrs GroupA GroupB
    :end

    Hope these help. Oh BTW allthese DS commands are in the Windows 2003 adminpac or resources kit I'm not sure which.


     

  •  09-10-2008, 9:42 AM 30047 in reply to 29626

    Re: Active directory into SQL

    Thanks for this script it works great and was easy to update to pull the fields i'm interested in. Question, does anyone know a way to get it to connect to root ds and traverse all the domains in our AD? It currently pulls the data from my domain only.

     Thanks!

    Charlie

  •  09-10-2008, 11:11 AM 30049 in reply to 30047

    Re: Active directory into SQL

    Hi Charlie,

    Can you post a copy of your script (use <pre> ... </pre> in your HTML tags)?

    Bill

  •  09-10-2008, 4:11 PM 30050 in reply to 30049

    Re: Active directory into SQL

    <pre>

    var defaultNC, connection, command, rs;
    defaultNC = GetObject("LDAP://rootDSE").Get("defaultNamingContext");
    connection = new ActiveXObject("ADODB.Connection");
    connection.Provider = "ADsDSOObject";
    connection.Open();

    command = new ActiveXObject("ADODB.Command");
    command.ActiveConnection = connection;
    command.Properties.Item("Cache results") = false;
    command.Properties.Item("Page size") = 1000;
    command.CommandText = "<LDAP://" + defaultNC + ">;" +
      "(&(objectCategory=person)(objectClass=user));" +
      "givenName,sn,displayName,sameaccountname,department,homeDirectory,legacyExchangeDN,l," +
      "st,telephoneNumber" +
      ";subtree";

    rs = command.Execute();
    while (! rs.EOF) {
      WScript.Echo('"' + rs.Fields.Item("givenName") + '",' +
        '"' + rs.Fields.Item("sn")  + '",' +
        '"' + rs.Fields.Item("displayName")  + '",' +
        '"' + rs.Fields.Item("samaccountname")  + '",' +
        '"' + rs.Fields.Item("department")          + '",' +
        '"' + rs.Fields.Item("homeDirectory")                 + '",' +
        '"' + rs.Fields.Item("legacyExchangeDN")    + '",' +
        '"' + rs.Fields.Item("l")    + '",' +
        '"' + rs.Fields.Item("st")    + '",' +
        '"' + rs.Fields.Item("telephoneNumber") + '"');
      rs.MoveNext();
    }
    </pre>

Page 1 of 2 (18 items)   1 2 Next >
View as RSS news feed in XML
SPONSORED LINKS FEATURED LINKS

Maximize speed, performance and reliablity of your PCs and servers—automatically!Speed Up Your PC! Try Diskeeper 2008 with InvisiTasking Free Now! Microsoft Learning Snack - Green IT Through Virtualization Many organizations face rising operating costs caused by excessive energy consumption. Virtualization and "Green IT" can help cut these costs. Get the information you need to bring Green IT savings to your business. Order Your Fundamentals CD Today!Register today for your in-depth copy of one of three Fundamental CDs on the following topics – Exchange, SQL, and SharePoint. Microsoft Learning Snack - Virtualization With Windows Server 2008Windows Server 2008 includes virtualization technology that allows many operating systems - including open source - to run on a single host. Come learn the basics of implementing these features. Microsoft Learning Snack - Virtualization BasicsWith virtualization, computing components essentially become on-demand services, freeing each element of a system from the others. This short video explains the needs, benefits, and technologies behind virtualization. Microsoft Learning Snack - Virtualization BasicsWith virtualization, computing components essentially become on-demand services, freeing each element of a system from the others. This short video explains the needs, benefits, and technologies behind virtualization. Microsoft Learning Snack - Virtualization With Windows Server 2008Windows Server 2008 includes virtualization technology that allows many operating systems - including open source - to run on a single host. Come learn the basics of implementing these features. Empower Your Processes with PowerShell 201Paul Robichaux delves deep into PowerShell how-tos in 3 informative lessons, each followed by live Q&A—all on your own computer! Register today! Microsoft Learning Snack - Green IT Through VirtualizationMany organizations face rising operating costs caused by excessive energy consumption. Virtualization and "Green IT" can help cut these costs. Get the information you need to bring Green IT savings to your business. New Release: Windows IT Pro Master CD13 years of content archives, fast answers with advanced search tools, and full access to WindowsITPro.com—order today!

 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing