{"id":115,"date":"2008-05-08T13:18:29","date_gmt":"2008-05-08T13:18:29","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?page_id=115"},"modified":"2010-12-15T16:01:13","modified_gmt":"2010-12-15T16:01:13","slug":"archive_mailboxesvbs","status":"publish","type":"page","link":"https:\/\/www.wapshere.com\/missmiis\/archive_mailboxesvbs","title":{"rendered":"Archive_Mailboxes.vbs"},"content":{"rendered":"<pre><code>\r\n'--------------------------------------------------------------------------------------\r\n'  ARCHIVE_MAILBOX.VBS\r\n'\r\n'  Usage:  cscript archive_mailbox.vbs\r\n'\r\n'  Script actions:\r\n'     + Finds Disabled mail users\r\n'     + Checks the extensionAttribute used for the Archive flag\r\n'     + Archives the mailbox if the flag was not set\r\n'\r\n'  All script messages (Errors, Warning, Information) are written to the\r\n'  Application Events Log with a type of WSH.\r\n'\r\n'  Written By Carol Wapshere\r\n'\r\n'--------------------------------------------------------------------------------------\r\n\r\nOption Explicit\r\n\r\n'------------------------------------------------------------------------\r\n'\r\n'  GLOBAL DECLARATIONS\r\n'\r\n'------------------------------------------------------------------------\r\n\r\nConst DISABLE_RECEIVE = TRUE\r\nConst ARCHIVE_FLAG_ATTRIB = \"extensionAttribute15\"\r\nConst ARCHIVE_FLAG_TEXT = \"Mailbox Archived\"\r\nConst EXCH_MB_SERVER = \"MAILSERVER\"\r\nConst AD_ROOT_DN = \"DC=myorg,DC=com\"\r\nConst EXCH_POWERSHELL_SNAPIN = \"C:\\Program Files\\Microsoft\\Exchange Server\\Bin\\ExShell.psc1\"\r\nConst POWERSHELL_SCRIPT = \"C:\\scripts\\archive_mailbox\\archive_mailbox.ps1\"\r\nConst LOG_FILE = \"C:\\scripts\\archive_mailbox\\archive_mailbox_log.txt\"\r\nConst POWERSHELL_TIMEOUT = 4000 'seconds\r\n\r\n'If the following are changed they must also be changed in ps1 script file\r\nConst ARCHIVE_FOLDER = \"C:\\Archived_Mailboxes\"\r\nConst CSV_FILE = \"c:\\scripts\\archive_mailbox\\archive_mailbox_todo.csv\"\r\nConst PS1_OUTPUT_FILE = \"c:\\scripts\\archive_mailbox\\ps1output.txt\"\r\n\r\nConst EVENT_SUCCESS = 0\r\nConst EVENT_ERROR = 1\r\nConst EVENT_WARNING = 2\r\nConst EVENT_INFORMATION = 4\r\nConst ForReading = 1\r\nConst ForWriting = 2\r\nConst ForAppending = 8\r\nConst ASCII = 0\r\nConst ADS_UF_ACCOUNTDISABLE = &amp;H02\r\nConst ADS_PROPERTY_CLEAR = 1\r\n\r\nDim strDN, strPST, strLine, powershellCommand\r\nDim objFSO, objFile_CSV, objShell\r\nDim objUser, objConnection, objCommand, objRecordSet\r\nDim bPowershellFinished, objFile_Ps1Output\r\nDim arrToArchive()\r\nDim i, count\r\n\r\n'------------------------------------------------------------------------\r\n'\r\n'  MAIN BODY\r\n'\r\n'------------------------------------------------------------------------\r\n\r\n'--------------------------------------------------\r\n' Find Disabled mailusers with no Archive flag\r\n'--------------------------------------------------\r\n\r\nSet objConnection = CreateObject(\"ADODB.Connection\")\r\nSet objCommand =   CreateObject(\"ADODB.Command\")\r\nobjConnection.Provider = \"ADsDSOObject\"\r\nobjConnection.Open \"Active Directory Provider\"\r\nSet objCommand.ActiveConnection = objConnection\r\nobjCommand.Properties(\"Page Size\") = 1000\r\n\r\nobjCommand.CommandText = _\r\n\"&lt;LDAP:\/\/\" &amp; AD_ROOT_DN &amp; \"&gt;;(&amp;(objectCategory=User)\" &amp; _\r\n\"(userAccountControl:1.2.840.113556.1.4.803:=2)(homeMDB=*));\" &amp;_\r\n\"distinguishedName,extensionAttribute15;Subtree\"\r\nSet objRecordSet = objCommand.Execute\r\n\r\ni = 0\r\nobjRecordSet.MoveFirst\r\nDo Until objRecordSet.EOF\r\n  If IsNull(objRecordSet.Fields(\"extensionAttribute15\").Value) Then\r\n    ReDim Preserve arrToArchive(i)\r\n    arrToArchive(i) = objRecordSet.Fields(\"distinguishedName\").Value\r\n    i = i + 1\r\n  End If\r\n  objRecordSet.MoveNext\r\nLoop\r\n\r\nIf i=0 Then\r\n  LogEvent EVENT_INFORMATION, \"EXIT\", \"No mailboxes to archive.\"\r\nEnd If\r\n\r\n'-------------------------------------------------------------------\r\n' Create the CSV file\r\n'-------------------------------------------------------------------\r\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\r\nSet objFile_CSV = objFSO.OpenTextFile(CSV_FILE, ForWriting, TRUE, ASCII)\r\nobjFile_CSV.Writeline(\"DN\")\r\n\r\nFor each strDN in arrToArchive\r\n  objFile_CSV.Writeline(chr(34) &amp; strDN &amp; chr(34))\r\nNext\r\nobjFile_CSV.Close\r\n\r\n'-------------------------------------------------------------------\r\n' Call the Powershell script to export mailboxes\r\n' See <a href=\"https:\/\/www.wapshere.com\/missmiis\/archiving-exchange-2007-mailboxes\">https:\/\/www.wapshere.com\/missmiis\/archiving-exchange-2007-mailboxes\r\n<\/a>'-------------------------------------------------------------------\r\n\r\nIf objFSO.FileExists(PS1_OUTPUT_FILE) Then\r\n  objFSO.DeleteFile(PS1_OUTPUT_FILE)\r\nEnd If\r\n\r\npowershellCommand = \"PowerShell.exe -PSConsoleFile \" &amp; chr(34) &amp; EXCH_POWERSHELL_SNAPIN &amp; chr(34) &amp; \" -Command \" &amp; chr(34) &amp; \". '\" &amp; POWERSHELL_SCRIPT &amp; \"'\" &amp; chr(34) &amp; \"-NoExit\"\r\n\r\nSet objShell = CreateObject(\"Wscript.Shell\")\r\nobjShell.Run(powershellCommand)\r\n\r\n' Loop until powershell has finished writing to the output file\r\n' If we wait more than the timeout then fail with an error.\r\nbPowershellFinished = False\r\ncount = 0\r\nOn Error Resume Next\r\nDo\r\n  count = count + 1\r\n  set objFile_Ps1Output = objFSO.OpenTextFile(PS1_OUTPUT_FILE, ForReading, ASCII)\r\n  If Err.Number &lt;&gt; 0 Then\r\n    WScript.Sleep 1000\r\n  Else\r\n    bPowershellFinished = True\r\n  End If\r\n  Err.Clear\r\nLoop Until bPowershellFinished Or (count = POWERSHELL_TIMEOUT)\r\nOn Error GoTo 0\r\n\r\nIf count = POWERSHELL_TIMEOUT Then\r\n  LogEvent EVENT_ERROR, \"EXIT\", \"Fatal: Powershell script did not complete in a reasonable lenth of time.\"\r\nElse\r\n  wscript.echo objFile_Ps1Output.ReadAll\r\n  objFile_Ps1Output.Close\r\nEnd If\r\n\r\n'------------------------------------------\r\n' Verify Archive\r\n' Update attribute flag and logging\r\n'------------------------------------------\r\nSet objFile_CSV = objFSO.OpenTextFile(PS1_OUTPUT_FILE, ForReading, ASCII)\r\n\r\nDo Until objFile_CSV.AtEndOfStream\r\n  strLine = objFile_CSV.ReadLine\r\n  Do Until InStr(strLine, \"DistinguishedName\") Or objFile_CSV.AtEndOfStream\r\n    strLine = objFile_CSV.ReadLine\r\n  Loop\r\n\r\n  If InStr(strLine, \"DistinguishedName\") Then\r\n    strDN = Trim(Split(strLine,\": \")(1))\r\n    If InStr(LCase(strDN), LCase(AD_ROOT_DN)) = 0 Then\r\n      ' Deal with DN split across two lines\r\n      strLine = objFile_CSV.ReadLine\r\n      strDN = strDN &amp; Trim(strLine)\r\n    End If\r\n    wscript.echo \"Verifying \" &amp; strDN\r\n\r\n    Do Until InStr(strLine, \"PSTFilePath\")\r\n      strLine = objFile_CSV.ReadLine\r\n    Loop\r\n\r\n    strPST = Trim(Split(strLine,\": \")(1))\r\n\r\n    Do Until InStr(strLine, \"StatusCode\")\r\n      strLine = objFile_CSV.ReadLine\r\n    Loop\r\n\r\n    If( Trim(Split(strLine,\": \")(1)) = \"0\" ) Then\r\n      LogToFile \"Archived \" &amp; strPST\r\n      LogEvent EVENT_SUCCESS, \"RETURN\", \"The mailbox of disabled user \" &amp; strDN &amp;_\r\n          \" has been archived to \" &amp; strPST\r\n      Set objUser = GetObject(\"LDAP:\/\/\" &amp; strDN)\r\n      objUser.Put \"extensionAttribute15\", ARCHIVE_FLAG_TEXT &amp; \" to \" &amp; strPST &amp; \" on \" &amp; Now()\r\n      objUser.SetInfo\r\n\r\n    Else 'Archive Failed\r\n      LogEvent EVENT_ERROR, \"RETURN\", \"Mailbox archiving failed for disabled user \" &amp; strDN\r\n\r\n    End If\r\n  End If 'InStr(strLine, \"DistinguishedName\")\r\nLoop\r\n\r\nobjFile_CSV.Close\r\n\r\n'------------------------------------------------------------------------\r\n'\r\n'  SUBROUTINES\r\n'\r\n'------------------------------------------------------------------------\r\n\r\n'---------------------------------------------------------------\r\n'  SUB LOGEVENT\r\n'\r\n'  Writes Messages into the Application Event Log\r\n'---------------------------------------------------------------\r\nSub LogEvent(eventType, action, message)\r\n  Dim objShell\r\n\r\n  wscript.echo message\r\n  message = \"ARCHIVE_MAILBOX Script\" &amp; VBNewLine &amp; message\r\n  Set objShell = Wscript.CreateObject(\"WScript.Shell\")\r\n  objShell.LogEvent eventType, message\r\n\r\n  If action = \"EXIT\" then\r\n    Wscript.Quit\r\n  End If\r\nEnd Sub\r\n\r\n'---------------------------------------------------------------\r\n'  SUB LOGTOFILE\r\n'\r\n'  Writes Status into the Log File\r\n'---------------------------------------------------------------\r\nSub LogToFile(message)\r\n  Dim objLogFSO, objLogFile\r\n\r\n  Set objLogFSO = CreateObject(\"Scripting.FileSystemObject\")\r\n  Set objLogFile = objLogFSO.OpenTextFile(LOG_FILE, ForAppending, TRUE)\r\n\r\n  objLogFile.Writeline(Now() &amp; \": \" &amp; message)\r\n  objLogFile.Close\r\nEnd Sub\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8216; ARCHIVE_MAILBOX.VBS &#8216; &#8216; Usage: cscript archive_mailbox.vbs &#8216; &#8216; Script actions: &#8216; + Finds Disabled mail users &#8216; + Checks the extensionAttribute used for the Archive flag &#8216; + Archives the mailbox if the flag was not set &#8216; &#8216; All script messages (Errors, Warning, Information) are written to the &#8216; Application Events Log&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-115","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/Pkp1o-1R","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":2,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/115\/revisions"}],"predecessor-version":[{"id":1160,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/pages\/115\/revisions\/1160"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}