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
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.
ReplyDeleteDo you know how I could do this?
PS- thanks for the script above, great tool which has helped me a lot.
Sure Thing!
ReplyDeleteFind 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!
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?
ReplyDeleteThanks.
Oz
Thanks OZ, Sure you would just do something like this:
ReplyDeletecscript script.vbs > outputfile.txt
cscript script.vbs > outputfile.csv
Is there a way to add an additional column to the output, such as:
ReplyDeleteCompany
Department
Displayname
mail
streetaddress
City
State
Zip
telephonenumber
title
OU of user
Thanks,
Stephen
That request is really a different script, but the answer is yes it can be done. Try this one out:
DeleteSET 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
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
ReplyDeletecscript script.vbs > outputfile.txt
cscript script.vbs > outputfile.csv
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
DeleteI 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.
ReplyDeleteVery quick and effective, Thanks!