Thursday, January 13, 2011

Active Directory – Find users with no login script specified in their user account

Recently I was trying to clean up the login scripts set in my user accounts. I have a lot of users, they had different scripts set and some had none at all. I made a lot of changes to my login script to streamline things and it was time to apply this change and also cleanup who was configured with what. Some were outdated and should have changed; some were missing the script, and so on.  Now to be truthful it wasn’t all that bad but I like things neat and proper when possible and time allows. Thing is, there was just no way I was going to go through one by one and look at and update each user, I would have never finished that one so I started working on a solution. As a whole, users were set as they should be; however, I had noticed that there were many users I kept seeing with no login script. This was my main concern. I wanted to be sure that all my users that need the script had it. Enter VBS. I’ll go ahead and note this is not my script; I’m not that good of a programmer. 

To create the file copy the text below into notepad and save as noscript.vbs or whatever you’d like to name it. 

Copy the vbs file to the AD server you want to run it on. From that server, in order to output a list of all users with no script listed in there account, run the following command (must be in the directory where the vbs file is located or specify the path with the file):

cscript noscript.vbs > outputfile.txt

For a better analysis if desired you can then pull the data from the txt file into a spreadsheet and break the data into columns.


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)(!scriptPath=*))"

' 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

No comments:

Post a Comment