'Constants Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D Const ADS_PROPERTY_CLEAR = 1 Const ADS_PROPERTY_DELETE = 4 'Objects Set FSO=CreateObject("Scripting.FileSystemObject") Set wshShell = WScript.CreateObject ("WSCript.shell") Set objMessage = CreateObject("CDO.Message") 'Variables ThisComputer=wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") LogDir="C:\autoterm\prunelog" WatchlistFile="c:\autoterm\watchlist.txt" TemporaryWatchlist="c:\autoterm\tmpWatchList.txt" 'Set up Temporary Watchlist If Not FSO.FileExists(TemporaryWatchlist) Then Set tmpWatchlist=FSO.CreateTextFile(TemporaryWatchlist, ForAppending) tmpWatchlist.Close Set tmpWatchlist=FSO.OpenTextFile(TemporaryWatchlist, ForAppending) Else Set tmpWatchlist=FSO.OpenTextFile(TemporaryWatchlist, ForAppending) End If 'Set up logging 'vbscript Date() function returns single digits <10, so pad a 0..... If Month(Date) < 10 Then CurMonth="0" & Month(Date) Else CurMonth=Month(Date) End If If Day(Date) < 10 Then CurDay="0" & Day(Date) Else CurDay=Day(Date) End If LogFile=Year(Date) & CurMonth & CurDay & ".txt" FullLogfilePath=LogDir & "\" & LogFile If Not FSO.FileExists(FullLogfilePath) Then Set Logging=FSO.CreateTextFile(FullLogfilePath, ForAppending) Logging.Close Set Logging=FSO.OpenTextFile(FullLogFilePath, ForAppending) Else Set Logging=FSO.OpenTextFile(FullLogFilePath, ForAppending) End If 'Read through the watchlist. Set Watchlist=FSO.OpenTextFile(WatchlistFile, ForReading) Do Until Watchlist.AtEndofstream 'Read the line in the watchlist and make 2 vars from it. tmpVar=Watchlist.Readline tmpElements=split(tmpVar,"~") LanID=lcase(tmpElements(0)) TermDate=tmpElements(1) 'Check the date to see if it's time to blow away this person's mailbox/share. DaysSinceTerm=abs(datediff("d",date,TermDate)) 'WScript.Echo DaysSinceTerm & " " & TermDate & " " & LanID If DaysSinceTerm<91 Then 'WScript.Echo Watchlist.readline tmpWatchlist.WriteLine(LanID & "~" & TermDate) Logging.WriteLine(Date & " " & Time & " ID: " & LanID & ", Not ready for termination yet: (" & DaysSinceTerm & " days ago.)") Else Logging.WriteLine(Date & " " & Time & " Found ID: " & LanID & ", termed " & DaysSinceTerm & " ago.") 'Get some critical stats HomeDirectory=RetrieveValue("sAMAccountName",LanID,"homeDirectory") HomeDirectory=LCase(HomeDirectory) 'normalize "netapp4" If vartype(HomeDirectory)<>1 Then HomeDirectory=Replace(HomeDirectory,"netapp4","ntdisk01") If InStr(HomeDirectory,LanID) then Logging.WriteLine(Date & " " & Time & " HomeDir: " & HomeDirectory) 'Determine if it's a share... IsHomeDiraShare=CheckShare(HomeDirectory) If IsHomeDiraShare="Yes" Then ShareStatus=NukeShare(HomeDirectory) tmpShareStatus=Split(ShareStatus,",") ShareOutput=tmpShareStatus(0) DirectoryOutput=tmpShareStatus(1) End If End If End If IsMailboxDisconnected=DisconnectMailbox(LanID) If ShareOutput="" Then ShareOutput="Already Deleted" If DirectoryOutput="" Then DirectoryOutput="Already Deleted" If StatusReport="" Then StatusReport = "Account: " & LanID & " (" & DaysSinceTerm & " days ago)" & VbCrLf & "Share/Homedir Removal: " & ShareOutput & "/" & DirectoryOutput & " | " & "Mailbox Disconnect: " & isMailboxDisconnected & VbCrLf & VbCrLf Else StatusReport = StatusReport & "Account: " & LanID & " (" & DaysSinceTerm & " days ago)" & VbCrLf & "Share/Homedir Removal: " & ShareOutput & "/" & DirectoryOutput & " | " & "Mailbox Disconnect: " & isMailboxDisconnected & VbCrLf & VbCrLf End If End If Loop if len(StatusReport)>5 then Call SendMail(StatusReport) 'Done with the watchlist, delete it and move the temporary one to the new one. Watchlist.close FSO.DeleteFile(WatchlistFile) tmpWatchlist.Close FSO.MoveFile TemporaryWatchlist, WatchListFile WScript.Quit 'Here be functions! Function DisconnectMailbox(pLanId) FullDN=RetrieveValue("sAMAccountName",pLanID,"distinguishedName") Set strUser=GetObject("LDAP://" & FullDN) If strUser.HomeMDB="" Then DisconnectMailbox="Not Mail Enabled" Else On Error Resume Next strUser.DeleteMailbox strUser.SetInfo If Err.Number<>0 Then DisconnectMailbox=Err.Number & ": " & Err.Description Else DisconnectMailbox="Success" End If End If End Function Function CheckShare(pHomeDirectory) CheckShare="No" Set objExec=wshShell.Exec("rmtshare " & pHomeDirectory) Do While objExec.StdOut.AtEndOfStream <> True ReadSTDOUT=objExec.StdOut.ReadLine If InStr(ReadSTDOUT,"Permissions:")>0 Then CheckShare="Yes" Logging.WriteLine(Date & " " & Time & " HomeDirectory " & pHomeDirectory & " is a share.") End If Loop If CheckShare="No" Then Logging.WriteLine(Date & " " & Time & " HomeDirectory " & pHomeDirectory & " is NOT a share.") End Function Function NukeShare(pHomeDirectory) Set objExec=wshShell.Exec("rmtshare " & pHomeDirectory) Do While objExec.StdOut.AtEndOfStream <> True ReadSTDOUT=objExec.StdOut.ReadLine If InStr(ReadSTDOut,"Path")>0 and InStr(ReadSTDOUT,"\")>0 Then LiteralPath=replace(ReadSTDOUT,"Path","") LiteralPath=LTrim(LiteralPath) Logging.WriteLine(Date & " " & Time & " HomeDirectory literal path: " & LiteralPath) If lcase(Left(literalPath,10))="c:\vol\nt1" And instr(pHomeDirectory,"ntdisk01")>0 Then AdminShare="\\ntdisk01\y$" & Mid(LiteralPath,11) If lcase(Left(literalPath,10))="c:\vol\nt2" And instr(pHomeDirectory,"ntdisk01")>0 Then AdminShare="\\ntdisk01\x$" & Mid(LiteralPath,11) If lcase(Left(literalPath,10))="c:\vol\nt3" And instr(pHomeDirectory,"ntdisk01")>0 Then AdminShare="\\ntdisk01\w$" & Mid(LiteralPath,11) If lcase(Left(literalPath,10))="c:\vol\nt5" And instr(pHomeDirectory,"ntdisk01")>0 Then AdminShare="\\ntdisk01\u$" & Mid(LiteralPath,11) End If Loop Set objExec=wshShell.Exec("rmtshare " & pHomeDirectory & " /DELETE") Do While objExec.StdOut.AtEndOfStream <> True ReadSTDOUT=objExec.StdOut.ReadLine If InStr(ReadSTDOut,"successfully.")>0 Then DeletedShare="Yes" If InStr(ReadSTDOUT,"failed:")>0 Then DeletedShare="No" Loop If DeletedShare="Yes" Then NukeShare="Success," Logging.WriteLine(Date & " " & Time & " Share " & pHomeDirecotry & " disconnected.") 'Nuke the corresponding directory. Set objExec=wshShell.Exec("cmd /c rd /s /q " & AdminShare) If objExec.ExitCode=0 Then Logging.WriteLine(Date & " " & Time & " Successfully removed dir " & AdminShare) NukeShare="Success,Success" Else Logging.WriteLine(Date & " " & Time & " ***Error removing dir: " & AdminShare) NukeShare="Success,Failed" End If Else NukeShare="Failed,Failed" Logging.WriteLine(Date & " " & Time & " ***Error removing share: " & pHomeDirecotry & " disconnected.") End If End Function Sub MoveFile(sSource,sDestination) 'WScript.Echo sSource 'On Error Resume Next Set objFileCopy = FSO.GetFile(sSource) objFileCopy.Move(sDestination) End Sub function RetrieveValue(DataType,Data,ObjName) 'WScript.Echo datatype 'WScript.Echo Data 'WScript.Echo objName Set oRootDSE = GetObject("LDAP://rootDSE") Set oConnection = CreateObject("ADODB.Connection") oConnection.Open "Provider=ADsDSOObject;" Set oCommand = CreateObject("ADODB.Command") oCommand.ActiveConnection = oConnection If DataType="EmployeeID" then oCommand.CommandText = ";(&(objectCategory=User)(" & DataType & "=" & Data & "*));" & objName & ";subtree" Else oCommand.CommandText = ";(&(objectCategory=User)(" & DataType & "=" & Data & "));" & objName & ";subtree" End If 'WScript.Echo oCommand.CommandText Set oRecordSet = oCommand.Execute on error resume Next 'WScript.Echo oRecordSet.Fields(objName) RetrieveValue = oRecordSet.Fields(objName) Set oRecordSet = Nothing Set oCommand = Nothing Set oConnection = Nothing Set oRootDSE = Nothing End function Sub SendMail(pStatusReport) Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Home Directory/Mailbox Purge Report" objMessage.From = "dsusapcserver@rbccm.com" objMessage.To = "karl.weckstrom@rbccm.com" 'Read in the temp file you created Mailbody = "90 days has passed since one or more user(s) has been terminated. As such, the following home directories have been purged and their mailboxes disconnected: " & VbCrLf & VbCrLf & pStatusReport objMessage.TextBody = MailBody 'objMessage.AddAttachment(c:.txt) '======== Config ============ objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailer.rbc.com" objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '======== End Config ========= objMessage.Configuration.Fields.Update objMessage.Send end Sub