![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Looking for assistance in building a script Hey guys, I am looking to discover all user profile folders that no longer have the designated user assigned to them. We need to clean up the profile directories and would like to identify which user profile folders are no longer valid because the uers AD account has been delete from AD. I would like to build a script which will parse a directory and compare the folder names to the AD users and move any folders that are not associated with a user account with the same name as the folder to an alternate directory. Can anyone assit with this? Thanks! |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Looking for assistance in building a script On Oct 24, 11:00*am, wla...@xxxxxx wrote: Quote: > Hey guys, > > I am looking to discover all user profile folders that no longer have > the designated user assigned to them. We need to clean up the profile > directories and would like to identify which user profile folders are > no longer valid because the uers AD account has been delete from AD. > > I would like to build a script which will parse a directory and > compare the folder names to the AD users and move any folders that are > not associated with a user account with the same name as the folder to > an alternate directory. > > Can anyone assit with this? > > Thanks! not the profile path. Thanks! |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Looking for assistance in building a script "Bill" <wlahay@xxxxxx> wrote in message news:24f6f98a-2068-44f9-8807-dd99bd3db3aa@xxxxxx On Oct 24, 11:00 am, wla...@xxxxxx wrote: Quote: > Hey guys, > > I am looking to discover all user profile folders that no longer have > the designated user assigned to them. We need to clean up the profile > directories and would like to identify which user profile folders are > no longer valid because the uers AD account has been delete from AD. > > I would like to build a script which will parse a directory and > compare the folder names to the AD users and move any folders that are > not associated with a user account with the same name as the folder to > an alternate directory. > > Can anyone assit with this? > > Thanks! not the profile path. Thanks! ---------- I would first use the FileSystemObject to enumerate all folders in the "Home" share and add them to a dictionary object. I would then use ADO to retrieve home directory paths for all users with home directories assigned. If a match is found in the dictionary object, indicate this. Then, for the folders not so indicated, move them. The following VBScript example has been tested in most regards: ===== Option Explicit Dim objRootDSE, strDNSDomain, adoCommand, adoConnection Dim strBase, strFilter, strAttributes, strQuery, adoRecordset Dim strHomeDir Dim objFSO, strUsersFolder, objUsersFolder, objFolder Dim objList, strDir, strTarget ' Specify where unused home folders will be moved. ' This folder must exist. strTarget = "\\MyServer\Old" ' Create dictionary object to track home folders. Set objList = CreateObject("Scripting.Dictionary") objList.CompareMode = vbTextCompare ' Add all subfolders of home folder share to dictionary object. Set objFSO = CreateObject("Scripting.FileSystemObject") strUsersFolder = "\\MyServer\Users" Set objUsersFolder = objFSO.GetFolder(strUsersFolder) For each objFolder In objUsersFolder.SubFolders ' Key value is the full folder path, ' item value is the folder name. objList(objFolder.Path) = objFolder.Name Next ' Determine DNS domain name. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") ' Use ADO to search Active Directory. Set adoCommand = CreateObject("ADODB.Command") Set adoConnection = CreateObject("ADODB.Connection") adoConnection.Provider = "ADsDSOObject" adoConnection.Open "Active Directory Provider" adoCommand.ActiveConnection = adoConnection ' Search entire domain. strBase = "<LDAP://" & strDNSDomain & ">" ' Search for all home directories. strFilter = "(&(objectCategory=person)(objectClass=user)" _ & "(homeDirectory=*))" ' Comma delimited list of attribute values to retrieve. strAttributes = "homeDirectory" ' Construct the LDAP query. strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" ' Run the query. adoCommand.CommandText = strQuery adoCommand.Properties("Page Size") = 100 adoCommand.Properties("Timeout") = 30 adoCommand.Properties("Cache Results") = False Set adoRecordset = adoCommand.Execute ' Enumerate the resulting recordset. Do Until adoRecordset.EOF ' Retrieve values. strHomeDir = adoRecordset.Fields("homeDirectory").Value If (objList.Exists(strHomeDir) = True) Then ' This folder is assigned as home directory to a user. ' Flag as used by blanking out the folder name. objList(strHomeDir) = "" End If adoRecordset.MoveNext Loop ' Enumerate home directories in the dictionary object. For Each strDir In objList.Keys ' Check if assigned to a user. If (objList(strDir) <> "") Then ' This folder is not assigned. Move the folder. ' The key value, strDir, is the full path of the home ' folder, the item value, objList(strDir), is the folder name. objFSO.MoveFolder strDir, strTarget & "\" & objList(strDir) End If Next ' Clean up. adoRecordset.Close adoConnection.Close -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Looking for assistance in building a script "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in message news:OjZQYrkNJHA.1552@xxxxxx Quote: > > "Bill" <wlahay@xxxxxx> wrote in message > news:24f6f98a-2068-44f9-8807-dd99bd3db3aa@xxxxxx > On Oct 24, 11:00 am, wla...@xxxxxx wrote: Quote: >> Hey guys, >> >> I am looking to discover all user profile folders that no longer have >> the designated user assigned to them. We need to clean up the profile >> directories and would like to identify which user profile folders are >> no longer valid because the uers AD account has been delete from AD. >> >> I would like to build a script which will parse a directory and >> compare the folder names to the AD users and move any folders that are >> not associated with a user account with the same name as the folder to >> an alternate directory. >> >> Can anyone assit with this? >> >> Thanks! > I should clarify, this is for the user Home directory on the network, > not the profile path. > > Thanks! > ---------- > I would first use the FileSystemObject to enumerate all folders in the > "Home" share and add them to a dictionary object. I would then use ADO to > retrieve home directory paths for all users with home directories > assigned. If a match is found in the dictionary object, indicate this. > Then, for the folders not so indicated, move them. The following VBScript > example has been tested in most regards: > ===== > Option Explicit > > Dim objRootDSE, strDNSDomain, adoCommand, adoConnection > Dim strBase, strFilter, strAttributes, strQuery, adoRecordset > Dim strHomeDir > Dim objFSO, strUsersFolder, objUsersFolder, objFolder > Dim objList, strDir, strTarget > > ' Specify where unused home folders will be moved. > ' This folder must exist. > strTarget = "\\MyServer\Old" > > ' Create dictionary object to track home folders. > Set objList = CreateObject("Scripting.Dictionary") > objList.CompareMode = vbTextCompare > > ' Add all subfolders of home folder share to dictionary object. > Set objFSO = CreateObject("Scripting.FileSystemObject") > strUsersFolder = "\\MyServer\Users" > Set objUsersFolder = objFSO.GetFolder(strUsersFolder) > For each objFolder In objUsersFolder.SubFolders > ' Key value is the full folder path, > ' item value is the folder name. > objList(objFolder.Path) = objFolder.Name > Next > > ' Determine DNS domain name. > Set objRootDSE = GetObject("LDAP://RootDSE") > strDNSDomain = objRootDSE.Get("defaultNamingContext") > > ' Use ADO to search Active Directory. > Set adoCommand = CreateObject("ADODB.Command") > Set adoConnection = CreateObject("ADODB.Connection") > adoConnection.Provider = "ADsDSOObject" > adoConnection.Open "Active Directory Provider" > adoCommand.ActiveConnection = adoConnection > > ' Search entire domain. > strBase = "<LDAP://" & strDNSDomain & ">" > > ' Search for all home directories. > strFilter = "(&(objectCategory=person)(objectClass=user)" _ > & "(homeDirectory=*))" > > ' Comma delimited list of attribute values to retrieve. > strAttributes = "homeDirectory" > > ' Construct the LDAP query. > strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" > > ' Run the query. > adoCommand.CommandText = strQuery > adoCommand.Properties("Page Size") = 100 > adoCommand.Properties("Timeout") = 30 > adoCommand.Properties("Cache Results") = False > Set adoRecordset = adoCommand.Execute > > ' Enumerate the resulting recordset. > Do Until adoRecordset.EOF > ' Retrieve values. > strHomeDir = adoRecordset.Fields("homeDirectory").Value > If (objList.Exists(strHomeDir) = True) Then > ' This folder is assigned as home directory to a user. > ' Flag as used by blanking out the folder name. > objList(strHomeDir) = "" > End If > adoRecordset.MoveNext > Loop > > ' Enumerate home directories in the dictionary object. > For Each strDir In objList.Keys > ' Check if assigned to a user. > If (objList(strDir) <> "") Then > ' This folder is not assigned. Move the folder. > ' The key value, strDir, is the full path of the home > ' folder, the item value, objList(strDir), is the folder name. > objFSO.MoveFolder strDir, strTarget & "\" & objList(strDir) > End If > Next > > ' Clean up. > adoRecordset.Close > adoConnection.Close folders on the server or the local profile folders on the workstation. It assumes that the folder name is IDENTICAL to the sAMAccountName, which is a relatively safe assumption in our environment. It basically looks at each folder, then does a NET USER /DOMAIN FOLDERNAME. If the ERRORLEVEL is zero, the account still exists; if non-zero, this could either mean that the account no longer exists, or there is some discrepancy between the username and the folder name. The script simply lists the non-zero cases, which, in our environment, is such a small list that we can easily check these manually. I typically look at who the folder is permitted to to see if it is either a non-existent account, or an account of a different name (likely renamed). In the latter case, I fix the anomaly by renaming the folder and updating the account to point to it properly. /Al |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Building a PC | Gaming | |||
| looking at building | General Discussion | |||
| Building your own Rig | General Discussion | |||
| Assistance with script for changing printing method | VB Script | |||
| Building a PC | General Discussion | |||