Thursday, January 13, 2011

Active Directory – List login script for all user accounts

This is simply a continuation from the last post. This version of the VBS will return a listing of all users and what script if any is listed in their account. See previous post if you need help saving and running it.

VBS Script: 


'Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes

Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strScript

' Setup ADO objects.

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.

Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,scriptPath"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF

' Retrieve values and display.
strName = adoRecordset.Fields("sAMAccountName").Value

strScript = adoRecordset.Fields("scriptPath").value

Wscript.Echo strName & "," & strScript

' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop

' Clean up.

adoRecordset.Close

adoConnection.Close

9 comments:

  1. Hi, I need to search AD for users who have a specific login script eg ChicagoUsers1.bat and replace this with a new login script eg \live\ChicagoUsers.bat.

    Do you know how I could do this?

    PS- thanks for the script above, great tool which has helped me a lot.

    ReplyDelete
  2. Sure Thing!

    Find this line:
    strFilter = "(&(objectCategory=person)(objectClass=user))"

    And replace it with this one:
    strFilter = "(&(objectCategory=person)(objectClass=user)(scriptPath=ChicagoUsers1.bat))"

    You may also want to change this one just to clean up the output and remove the listing of the path name in the output but you don't have to.

    Find:
    Wscript.Echo strName & "," & strScript

    Replace with:
    Wscript.Echo strName

    After you get your results just look at the next post after this one and it shows you the script to update the login script. feed the results of this into that one and your done!

    ReplyDelete
  3. Hi Jeff, great script! is there a way of outputting the results to a text file onto the desktop? Better yet to a CSV file?

    Thanks.
    Oz

    ReplyDelete
  4. Thanks OZ, Sure you would just do something like this:

    cscript script.vbs > outputfile.txt
    cscript script.vbs > outputfile.csv

    ReplyDelete
  5. Is there a way to add an additional column to the output, such as:

    Company
    Department
    Displayname
    mail
    streetaddress
    City
    State
    Zip
    telephonenumber
    title
    OU of user

    Thanks,
    Stephen

    ReplyDelete
    Replies
    1. That request is really a different script, but the answer is yes it can be done. Try this one out:

      SET objRootDSE = GETOBJECT("LDAP://RootDSE")
      strExportFile = "C:\temp\MyExport.xls"

      strRoot = objRootDSE.GET("DefaultNamingContext")
      strfilter = "(&(objectCategory=Person)(objectClass=User))"
      strAttributes = "sAMAccountName,userPrincipalName,givenName,sn," & _
      "initials,displayName,physicalDeliveryOfficeName," & _
      "telephoneNumber,mail,wWWHomePage,profilePath," & _
      "scriptPath,homeDirectory,homeDrive,title,department," & _
      "company,manager,homePhone,pager,mobile," & _
      "facsimileTelephoneNumber,ipphone,info," & _
      "streetAddress,postOfficeBox,l,st,postalCode,c"
      strScope = "subtree"
      SET cn = CREATEOBJECT("ADODB.Connection")
      SET cmd = CREATEOBJECT("ADODB.Command")
      cn.Provider = "ADsDSOObject"
      cn.Open "Active Directory Provider"
      cmd.ActiveConnection = cn

      cmd.Properties("Page Size") = 1000

      cmd.commandtext = ";" & strFilter & ";" & _
      strAttributes & ";" & strScope

      SET rs = cmd.EXECUTE

      SET objExcel = CREATEOBJECT("Excel.Application")
      SET objWB = objExcel.Workbooks.Add
      SET objSheet = objWB.Worksheets(1)

      FOR i = 0 To rs.Fields.Count - 1
      objSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
      objSheet.Cells(1, i + 1).Font.Bold = TRUE
      NEXT

      objSheet.Range("A2").CopyFromRecordset(rs)
      objWB.SaveAs(strExportFile)


      rs.close
      cn.close
      SET objSheet = NOTHING
      SET objWB = NOTHING
      objExcel.Quit()
      SET objExcel = NOTHING

      Wscript.echo "Script Finished..Please See " & strExportFile

      http://gallery.technet.microsoft.com/scriptcenter/4d192f4d-2830-4a3e-9352-64a7e696a36e

      Delete
  6. Hi there, I cannot get the output to a file, I have tried the below by start; run and typing the commands the script runs but no files anywhere, am I doing something wrong

    cscript script.vbs > outputfile.txt
    cscript script.vbs > outputfile.csv

    ReplyDelete
    Replies
    1. Using the script in the comment above, the path is specified inside the script itself you do not have to specify an output file. see path above: C:\temp\MyExport.xls is where it is going to unless this is changed

      Delete
  7. I could only get an output file to be created (script from main post), I had to run an admin cmd prompt first, then navigate to the file, then run cscript logon_script.vbs > outputfile.csv.

    Very quick and effective, Thanks!

    ReplyDelete