' Get the context in which the script is running Set objRootDSE = getobject("LDAP://RootDSE") ' Define a filesystem object (will be usefull to create the users home directories) Set objFSO = CreateObject("Scripting.FileSystemObject") ' Define an OU (can be empty if the whole domain is to be checked) strOU = "ou=my_OU," strExportFile = "G:\Scripts\UserAD.xls" ' Get the Naimg Context of the domain strRoot = objRootDSE.Get("DefaultNamingContext") ' Define a filter to get only the requested objects strfilter = "(&(objectCategory=Person)(objectClass=User))" ' Define the attributes to be fetched from AD for an object 'strAttributes = "sAMAccountName,userPrincipalName,givenName,sn," strAttributes = "sAMAccountName," ' The OU and all child entries will be parsed. strScope = "subtree" ' Prepare the connection set cn = createobject("ADODB.Connection") set cmd = createobject("ADODB.Command") cn.open "Provider=ADsDSOObject;" cmd.ActiveConnection = cn ' Define the LDAP query cmd.commandtext = ";" & strFilter & ";" & _ strAttributes & ";" & strScope ' Execute the query set rs = cmd.execute '' Not used at this time ''' Get the result in an Excel sheet ''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) ''' Save the workbook ''objWB.SaveAs(strExportFile) ' Move to the first element of the result set rs.MoveFirst ' Get through the results Do Until rs.EOF ' Get the user login userLogin = rs.Fields(0).Value Wscript.Echo userLogin & " :" ' Test if the user home directory exists or not If objFSO.FolderExists("\\fileserver\UserDir$\" & userLogin) Then ' Don't do anything but display a message if the folder already exists. Wscript.Echo "Folder \\fileserver\UserDir$\" & userLogin & " already exists." Else ' Create the directory and set the permissions using XCACLS.vbs MS script. Wscript.Echo "Creating folder: " & "\\fileserver\UserDir$\" & userLogin Set objFolder = objFSO.CreateFolder("\\fileserver\UserDir$\" & userLogin) Set WshShell = WScript.CreateObject("WScript.Shell") Wscript.Echo "Setting permissions on folder: " & "\\fileserver\UserDir$\" & userLogin WshShell.Run "cscript G:\Scripts\XCACLS.vbs \\fileserver\UserDir$\" & _ userLogin & " /I COPY /R DOMAINNAME\USERGROUP /G DOMAINNAME\" & _ userLogin & ":F /O DOMAINNAME\" & userLogin ' Destroy any no longer used var Set WshShell = Nothing Set objFolder = Nothing End If rs.MoveNext Loop ' Destroy any no longer used var rs.close cn.close 'set objSheet = Nothing 'set objWB = Nothing 'objExcel.Quit() 'set objExcel = Nothing