Jump to content

Recommended Posts

Posted

Is there a way or a program that will look through my folders containing the User Areas of people, and look up in Active Directory to see if there's an account associated with it?

 

Long shot I know.

 

Just I know that when accounts are deleted from AD the User Area remains, and although we all try to remember to delete I imagine there'll be a couple somewhere.

Posted

^ that looks good..but does it just go ahead and delete any that arnt a home directory or just notify you which folders arent? Because there may be a few folders that because people have left, need to be saved somewhere else, not just deleted

 

Though it is all backed up I suppose

Posted
The first script deletes - it's a bit high risk if you have admins who put folders in random places (eg d:\whome should only have user home folders in there. If someone thinks "oh, that's a good place for my temp stuff" and bungs it in there then it will get lost). Wouldn't be hard to change the delete to a move so that you just move stuff to an archive location and then do a manual check (or wait for screams; no screams means it's safe to go!)
  • 6 months later...
Posted
Would it be possible to change the script so that it output the findings to a text file / csv instead of deleting them?

 

Here's one I use to do that. Just copy and paste into notepad and change extension to .vbs and run it.

 

'Created by Matthew Hull on 11/06/03 
'Last Updated 12/29/03 
'Documented on 4/25/04

'Version 1.01

'This script will look for orphaned folders.

'Version History
'~~~~~~~~~~~~~~~
'Version 1.01 - The script now searchs the whole domain for the   

'Version 1.0 - First version of this script released.
'*************************************************************************************

Option Explicit

On Error Resume Next

Dim objFSO, strUser, strRootPath, strPath, intCount, strMessage
Dim objFolder, objRootFolder, colFolders, bolCheck, txtOutput, strOutputFile
Dim objConnection, objCommand, objRootDSE, objRecordSet

'*************************************************************************************
'Set the root folder and the location of the output file

strRootPath = "\\win2003\users"
strOutputFile = "C:\Orphaned Folders Verification.txt"

'*************************************************************************************

'Create a RootDSE object for the domain
Set objRootDSE = GetObject("LDAP://RootDSE")

'Establish a connection to Active Directory using ActiveX Data Object
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADSDSOObject;"

'Create the command object and attach it to the connection object
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

'Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Set the orphan folder count to 0
intCount = 0

'Create a folder object that points to the root
Set objRootFolder = objFSO.GetFolder(strRootPath)

'Create a collection of folders that contain the subfolders of the root
Set colFolders = objRootFolder.Subfolders

'Start the message that will be displayed to the user
strMessage = "The following folders are orphaned in " & strRootPath & vbCRLF

For each objFolder in colFolders

  'objCommand defines the search base (in this case the whole domain) a filter
  '(All object of type user that match the foldername) and some attributes
  'associated with the returned objects (SamAccountName)
  objCommand.CommandText = "   ">;(&(objectClass=user)(SamAccountName=" & objFolder.Name & "));SamAccountName"
  
  'Initiate the LDAP query and return results to a RecordSet object.
  Set objRecordSet = objCommand.Execute
  
  'Set the Check to false, it will turn true if a folder isn't orphaned
  bolCheck = False
  
  'Loop thru each item in the Record Set and compare it to the folder name
  'if they match then the folder is not orphaned.  There should only be one
  'item returned in the Record set so this procedure is fast.
  While Not objRecordset.EOF      
     If UCase(objFolder.Name) = UCase(objRecordset.Fields("SamAccountName")) Then
        bolCheck = True
  
     End If
     objRecordset.MoveNext
  Wend
  objRecordSet.MoveFirst

  'If the folder is orphaned increase the count and add the folder to the message
  If Not bolCheck Then
     intCount = intCount + 1
     strMessage = strMessage & intCount & " - " & objFolder.Name & vbCRLF
      
  End If
  

Next

'Close the connection to Active Directory
objConnection.Close

'Change the message if no orphaned folders are located
If intCount = 0 Then
  strMessage = "There are no orphaned folders in " & strRootPath
End If

'Create a text file, write the message to it and close the text file
Set txtOutput = objFSO.CreateTextFile(strOutPutFile)
txtOutput.Write strMessage
txtOutput.Close

'Display a message to the user when complete
MsgBox strMessage,vbOkOnly,"Orphan Folder Check"

'Close objects
Set objFSO = Nothing
Set objConnection = Nothing
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing

  • Thanks 3

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...