It seems no matter how much you try you cannot ever get those damned orphaned homedirs cleaned up. Well, this helps. Our org always has additional groups in the homedir (no, we don’t just let the users have whatever they want in there, so we have to monitor). This causes a little confusion amongst most orphaned file checkers (as there is still a group in there that resolves). Read on for the code and an example.
What this script does is it scans a directory’s subdirectories (as with many homedirs, the subdirectories are usually the AD account name). It then tries to match the subdirectory to an AD account name. If this proves that one doesn’t exist, it prompts and spits out the ACL info and a prompt to move the files. If you say yes, it moves them to the directory you specified in arg1.
'Example: cscript orphaned-files.vbs "T:" "T:~archive" where T: is a mapped drive
strDomain = "dc=yourdomain,dc=com"
strFromDir = wscript.arguments(0)
strToDir = wscript.arguments(1)
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder(strFromDir)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
'Wscript.Echo Subfolder.Path
sUserName = replace(Subfolder.Path, strFromDir,"")
UserExist(sUserName)
Next
End Sub
Sub UserExist(sUserName)
dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://" & strDomain & ">;(&(objectCategory=User)" & _
"(samAccountName=" & sUserName & "));samAccountName;subtree"
Set objRecordSet = objCommand.Execute
If objRecordset.RecordCount = 0 Then
WScript.Echo "*******************sAMAccountName: " & sUserName & " does not exist."
DisplayACLS(sUserName)
End If
objConnection.Close
End Sub
Sub DisplayACLS(sUserName)
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec("ICACLS " & strFromDir & sUserName & "")
Set objStdOut = objWshScriptExec.StdOut
strLine = objStdOut.ReadAll
Wscript.Echo strLine
intAnswer = _
Msgbox("Do you want to move these files?", _
vbYesNo, "Move Files")
If intAnswer = vbYes Then
MoveFiles(sUserName)
Else
wscript.echo "Skipping Files"
wscript.echo "*******************"
End If
End Sub
Sub MoveFiles(sUserName)
wscript.echo "Moving Files"
wscript.echo "*******************"
Set wshShell = WScript.CreateObject ("WScript.shell")
rc=wshShell.run("cmd /c robocopy """ & strFromDir & sUserName & """ """ & strToDir & sUserName & """ /S /E /MOVE /COPY:DAT /V /NP /NFL /ZB /R:3 /W:3 /TEE",1,False)
Set wshShell = nothing
End Sub
*******************sAMAccountName: username does not exist.
S:username BUILTINAdministrators:(OI)(CI)(F)
CREATOR OWNER:(OI)(CI)(IO)(F)
(OI)(CI)(F)
Successfully processed 1 files; Failed processing 0 files
Moving Files
*******************
This is a quick one, it's been forever since I've posted here. After moving back…
Simple command turned crazy. I ended up coming up with this due to the fact…
Phew, this one took a minute to figure out. ConnectWise has a form based documents…
I've found myself at a new job, recreating many of the processes that I spent…
Wow, it's been a while since I've done a real post on this site. I've…
When using AutoTask's API it's required to lookup a various amount of picklist values that…