Automatic E-Mail Purging Script

35 pts.
Tags:
Batch files
Microsoft Exchange
Scripts
I was wondering if anyone knew of a better way or knew of access to a resource that would assist with providing a better script and/or batch file to execute for automatically purging e-mail from my exchange server..... Currently the script I am using to execute a batch file is as follows wscript /b c:arcfolderarcmailbox.vbs (enter mailbox name) I have to go in and edit the script as I add new mailboxes to this list for the batch file to execute and run on....more or less...it's a pain...to enter individual mailboxes instead of the entire mailbox store! Please advice...much appreciated!

Answer Wiki

Thanks. We'll let you know when a new response is added.

It’s possible that with some modifications the script you have may work just fine with some modifications. Any change you can post the code you need looked at? (please consider if there is anything proprietary to your business prior to posting)

If not, I can provide some guidance. Basically, you can pass a argument to vbscript using the command line if you use the WScript.Arguments object in your script to grab the item you pass to the script.

You could easily add another parameter to make it do any numer of things.

Post more of how you’d desire it to work and possibly we can help.

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Jerry Lees
    Be sure and check out my blog here on ITKE for more Network administration and VBSCRIPT tips. The VBScript Network and Systems Administrator's Cafe
    5,335 pointsBadges:
    report
  • H techy
    Okay....here is the code for the entire script we are using... Dim obArgs,cArgs,iSize,ndate,tmailbox Set obArgs = WScript.Arguments tmailbox = obArgs.Item(0) Main Sub Main() Dim sConnString,domainname rem On Error Resume Next domainname = "bogusdomain.com" ' Use the Primary SMTP Exchange Domain name ' Set up connection string to mailbox. sConnString = "file://./backofficestorage/" & domainname sConnString = sConnString & "/mbx/" & obArgs.Item(0) & "" iSize = 0 RecurseFolder(sConnString) End Sub Public Sub RecurseFolder(sConnString) Dim oConn Dim oRecSet Dim sSQL ' Set up SQL SELECT statement. sSQL = "SELECT ""http://schemas.microsoft.com/mapi/proptag/x0e080003"", " sSQL = sSQL & """DAV:href"",""DAV:hassubs"",""DAV:displayname"" " sSQL = sSQL & "FROM SCOPE ('SHALLOW TRAVERSAL OF """ & sConnString sSQL = sSQL & """') WHERE ""DAV:isfolder"" = true" ' Create Connection object. Set oConn = CreateObject("ADODB.Connection") if Err.Number <> 0 then WScript.Echo "Error creating ADO Connection object: " & Err.Number & " " & Err.Description end if ' Create RecordSet object. Set oRecSet = CreateObject("ADODB.Recordset") if Err.Number <> 0 then WScript.Echo "Error creating ADO RecordSet object: " & Err.Number & " " & Err.Description Set oConn = Nothing Exit Sub end if ' Set provider to EXOLEDB. oConn.Provider = "Exoledb.DataSource" ' Open connection to folder. wscript.echo sConnString oConn.Open sConnString if Err.Number <> 0 then WScript.Echo "Error opening connection: " & Err.Number & " " & Err.Description Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if ' Open Recordset of all subfolders in folder. oRecSet.CursorLocation = 3 oRecSet.Open sSQL, oConn.ConnectionString if Err.Number <> 0 then WScript.Echo "Error opening recordset: " & Err.Number & " " & Err.Description oRecSet.Close oConn.Close Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if if oRecSet.RecordCount = 0 then oRecSet.Close oConn.Close Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if ' Move to first record. oRecSet.MoveFirst if Err.Number <> 0 then WScript.Echo "Error moving to first record: " & Err.Number & " " & Err.Description oRecSet.Close oConn.Close Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if ' Loop through all of the records, and then add the size of the ' subfolders to obtain the total size. While oRecSet.EOF <> True ' Increment size. iSize = iSize + oRecSet.Fields.Item("http://schemas.microsoft.com/mapi/proptag/x0e080003") workfolderfp = oRecSet.Fields("DAV:href").value wscript.echo workfolderfp archivemail(workfolderfp) ' If the folder has subfolders, recursively call RecurseFolder to process them. If oRecSet.Fields.Item("DAV:hassubs") = True then RecurseFolder oRecSet.Fields.Item("DAV:href") End If ' Move to next record. oRecSet.MoveNext if Err.Number <> 0 then WScript.Echo "Error moving to next record: " & Err.Number & " " & Err.Description Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if wend ' Close Recordset and Connection. oRecSet.Close if Err.Number <> 0 then WScript.Echo "Error closing recordset: " & Err.Number & " " & Err.Description Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if oConn.Close if Err.Number <> 0 then WScript.Echo "Error closing connection: " & Err.Number & " " & Err.Description Set oRecSet = Nothing Set oConn = Nothing Exit Sub end if ' Clean up memory. Set oRecSet = Nothing Set oConn = Nothing wscript.echo sConnString End Sub Sub archivemail(workfolderfp) mailboxurl = workfolderfp set Rec = CreateObject("ADODB.Record") set Rs = CreateObject("ADODB.Recordset") Set Conn = CreateObject("ADODB.Connection") Conn.Provider = "ExOLEDB.DataSource" Rec.Open mailboxurl, ,3 SSql = "SELECT ""DAV:href"", ""DAV:contentclass"" FROM scope('shallow traversal of """ & mailboxurl & """') " SSql = SSql & " WHERE (""urn:schemas:httpmail:datereceived"" < CAST(""" & isodateit(now()-14) & """ as 'dateTime')) AND ""DAV:isfolder"" = false" SSql = SSql & " AND ""DAV:contentclass"" = 'urn:content-classes:message'" Rs.CursorLocation = 2 'adUseServer = 2, adUseClient = 3 rs.open SSql, rec.ActiveConnection, 3 while not rs.eof rs.delete 1 rs.movenext wend rs.close end sub function isodateit(datetocon) strDateTime = year(datetocon) & "-" if (Month(datetocon) < 10) then strDateTime = strDateTime & "0" strDateTime = strDateTime & Month(datetocon) & "-" if (Day(datetocon) < 10) then strDateTime = strDateTime & "0" strDateTime = strDateTime & Day(datetocon) & "T" & formatdatetime(datetocon,4) & ":00Z" isodateit = strDateTime end function And to clarify....the portion that I am going in and editing everytime at this point is the batch file..and that is where i would add the following wscript /b c:\arcfolder\arcmailbox.vbs (enter mailbox name)
    35 pointsBadges:
    report
  • H techy
    Ideally what I want the script to do is execute my batch file that I have....so that it runs on on all my mailboxed residing on our exchange server....and eliminate anthing in there older than the most current 14 days!
    35 pointsBadges:
    report
  • Justthefacts
    From your last response it appears you want to clean up old mailbox data from all your mailboxes on a specific server. Have you considered using the Exchange Mailbox Manager utility. The 3 links below provide information on how to use the utility as well as exclude mailboxes from being cleaned. in short, its a matter a creating an additional Recipient Policy and chose Mailbox Manger Settings when prompted. http://support.microsoft.com/kb/319188/EN-US/ http://support.microsoft.com/kb/288115/en-us http://support.microsoft.com/kb/302804/en-us
    15 pointsBadges:
    report
  • H techy
    Thanks for the links...I wil check them out...and yes...I want to clean up old e-mail....in a sense...but this is an ongoing process...due to our e-mail retention policy....anything older than 14 days needs to be automatically purged from the exchange server....as of right now...the script I provided is doing this...and running through my batch file where I have added each mailbox individually....my qestion with this...is if there is a way to run a script on EVERY mailbox instead of populating my batch file with individual mailboxes!
    35 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following