'--------------------------------------------------------------------------------------
' ARCHIVE_MAILBOX.VBS
'
' Usage: cscript archive_mailbox.vbs
'
' Script actions:
' + Finds Disabled mail users
' + Checks the extensionAttribute used for the Archive flag
' + Archives the mailbox if the flag was not set
'
' All script messages (Errors, Warning, Information) are written to the
' Application Events Log with a type of WSH.
'
' Written By Carol Wapshere
'
'--------------------------------------------------------------------------------------
Option Explicit
'------------------------------------------------------------------------
'
' GLOBAL DECLARATIONS
'
'------------------------------------------------------------------------
Const DISABLE_RECEIVE = TRUE
Const ARCHIVE_FLAG_ATTRIB = "extensionAttribute15"
Const ARCHIVE_FLAG_TEXT = "Mailbox Archived"
Const EXCH_MB_SERVER = "MAILSERVER"
Const AD_ROOT_DN = "DC=myorg,DC=com"
Const EXCH_POWERSHELL_SNAPIN = "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1"
Const POWERSHELL_SCRIPT = "C:\scripts\archive_mailbox\archive_mailbox.ps1"
Const LOG_FILE = "C:\scripts\archive_mailbox\archive_mailbox_log.txt"
Const POWERSHELL_TIMEOUT = 4000 'seconds
'If the following are changed they must also be changed in ps1 script file
Const ARCHIVE_FOLDER = "C:\Archived_Mailboxes"
Const CSV_FILE = "c:\scripts\archive_mailbox\archive_mailbox_todo.csv"
Const PS1_OUTPUT_FILE = "c:\scripts\archive_mailbox\ps1output.txt"
Const EVENT_SUCCESS = 0
Const EVENT_ERROR = 1
Const EVENT_WARNING = 2
Const EVENT_INFORMATION = 4
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ASCII = 0
Const ADS_UF_ACCOUNTDISABLE = &H02
Const ADS_PROPERTY_CLEAR = 1
Dim strDN, strPST, strLine, powershellCommand
Dim objFSO, objFile_CSV, objShell
Dim objUser, objConnection, objCommand, objRecordSet
Dim bPowershellFinished, objFile_Ps1Output
Dim arrToArchive()
Dim i, count
'------------------------------------------------------------------------
'
' MAIN BODY
'
'------------------------------------------------------------------------
'--------------------------------------------------
' Find Disabled mailusers with no Archive flag
'--------------------------------------------------
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.CommandText = _
"<LDAP://" & AD_ROOT_DN & ">;(&(objectCategory=User)" & _
"(userAccountControl:1.2.840.113556.1.4.803:=2)(homeMDB=*));" &_
"distinguishedName,extensionAttribute15;Subtree"
Set objRecordSet = objCommand.Execute
i = 0
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
If IsNull(objRecordSet.Fields("extensionAttribute15").Value) Then
ReDim Preserve arrToArchive(i)
arrToArchive(i) = objRecordSet.Fields("distinguishedName").Value
i = i + 1
End If
objRecordSet.MoveNext
Loop
If i=0 Then
LogEvent EVENT_INFORMATION, "EXIT", "No mailboxes to archive."
End If
'-------------------------------------------------------------------
' Create the CSV file
'-------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile_CSV = objFSO.OpenTextFile(CSV_FILE, ForWriting, TRUE, ASCII)
objFile_CSV.Writeline("DN")
For each strDN in arrToArchive
objFile_CSV.Writeline(chr(34) & strDN & chr(34))
Next
objFile_CSV.Close
'-------------------------------------------------------------------
' Call the Powershell script to export mailboxes
' See http://www.wapshere.com/missmiis/archiving-exchange-2007-mailboxes
'-------------------------------------------------------------------
If objFSO.FileExists(PS1_OUTPUT_FILE) Then
objFSO.DeleteFile(PS1_OUTPUT_FILE)
End If
powershellCommand = "PowerShell.exe -PSConsoleFile " & chr(34) & EXCH_POWERSHELL_SNAPIN & chr(34) & " -Command " & chr(34) & ". '" & POWERSHELL_SCRIPT & "'" & chr(34) & "-NoExit"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(powershellCommand)
' Loop until powershell has finished writing to the output file
' If we wait more than the timeout then fail with an error.
bPowershellFinished = False
count = 0
On Error Resume Next
Do
count = count + 1
set objFile_Ps1Output = objFSO.OpenTextFile(PS1_OUTPUT_FILE, ForReading, ASCII)
If Err.Number <> 0 Then
WScript.Sleep 1000
Else
bPowershellFinished = True
End If
Err.Clear
Loop Until bPowershellFinished Or (count = POWERSHELL_TIMEOUT)
On Error GoTo 0
If count = POWERSHELL_TIMEOUT Then
LogEvent EVENT_ERROR, "EXIT", "Fatal: Powershell script did not complete in a reasonable lenth of time."
Else
wscript.echo objFile_Ps1Output.ReadAll
objFile_Ps1Output.Close
End If
'------------------------------------------
' Verify Archive
' Update attribute flag and logging
'------------------------------------------
Set objFile_CSV = objFSO.OpenTextFile(PS1_OUTPUT_FILE, ForReading, ASCII)
Do Until objFile_CSV.AtEndOfStream
strLine = objFile_CSV.ReadLine
Do Until InStr(strLine, "DistinguishedName") Or objFile_CSV.AtEndOfStream
strLine = objFile_CSV.ReadLine
Loop
If InStr(strLine, "DistinguishedName") Then
strDN = Trim(Split(strLine,": ")(1))
If InStr(LCase(strDN), LCase(AD_ROOT_DN)) = 0 Then
' Deal with DN split across two lines
strLine = objFile_CSV.ReadLine
strDN = strDN & Trim(strLine)
End If
wscript.echo "Verifying " & strDN
Do Until InStr(strLine, "PSTFilePath")
strLine = objFile_CSV.ReadLine
Loop
strPST = Trim(Split(strLine,": ")(1))
Do Until InStr(strLine, "StatusCode")
strLine = objFile_CSV.ReadLine
Loop
If( Trim(Split(strLine,": ")(1)) = "0" ) Then
LogToFile "Archived " & strPST
LogEvent EVENT_SUCCESS, "RETURN", "The mailbox of disabled user " & strDN &_
" has been archived to " & strPST
Set objUser = GetObject("LDAP://" & strDN)
objUser.Put "extensionAttribute15", ARCHIVE_FLAG_TEXT & " to " & strPST & " on " & Now()
objUser.SetInfo
Else 'Archive Failed
LogEvent EVENT_ERROR, "RETURN", "Mailbox archiving failed for disabled user " & strDN
End If
End If 'InStr(strLine, "DistinguishedName")
Loop
objFile_CSV.Close
'------------------------------------------------------------------------
'
' SUBROUTINES
'
'------------------------------------------------------------------------
'---------------------------------------------------------------
' SUB LOGEVENT
'
' Writes Messages into the Application Event Log
'---------------------------------------------------------------
Sub LogEvent(eventType, action, message)
Dim objShell
wscript.echo message
message = "ARCHIVE_MAILBOX Script" & VBNewLine & message
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.LogEvent eventType, message
If action = "EXIT" then
Wscript.Quit
End If
End Sub
'---------------------------------------------------------------
' SUB LOGTOFILE
'
' Writes Status into the Log File
'---------------------------------------------------------------
Sub LogToFile(message)
Dim objLogFSO, objLogFile
Set objLogFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objLogFSO.OpenTextFile(LOG_FILE, ForAppending, TRUE)
objLogFile.Writeline(Now() & ": " & message)
objLogFile.Close
End Sub
About this blog
This blog started out being about MIIS, but has extended to whatever I happen to be working on - particularly when I've had to struggle through incomplete documentation, blog and forum trawls, and good old trial and error to work through a problem. The posts reflect my own homegrown approach to problems I encountered and are entirely based on my own experiences - I will try to avoid theorising!Copyright Notice
All text in this blog is original and the copyright is owned by the author. You are welcome to use the code (without warranty) but please do not copy the articles without asking first.My social media policy
+ I will only accept facebook friend requests from people I know in person.
+ I will only accept linkedin requests from people I have worked with or had at least a few email exchanges with (remind me on the request if this is the case).
+ You are of course welcome to follow me on twitter (@miss_miis).RSS
AD ADAM ADFS Best Practice BPOS Cloud Conferences Email Template Excel Exchange 2003 Exchange 2007 Exchange 2010 FIM 2010 FIM 2010 R2 FIM Sync Service Groups ILM ILM "2" ILM 2007 Logs Lotus Notes MIIS 2003 MiisApp MPR newbie Novell Office 365 OpenLDAP Password Sync Performance Philosophising powershell Quest RCDC Reporting SAP Scripting Sets Sharepoint SQL Uncategorized Unify VB.NET VBScript Windows Server 2003 Windows Server 2008 Workflow
Popular Posts
- A GALSync powershell script
- Adding Exchange 2007 mailboxes to existing user accounts
- Exchange 2007 Cross-Forest Migration
- FIM Walkthroughs – Create the FIM MA
- FIM Walkthroughs – Planning and Installation
- Importing groups from AD to the FIM Portal using classic flow rules
- Password Sync from AD to BPOS
- Powershell Custom WF Activity
- Running Remote Powershell scripts from VB.NET
Pages
Archives
-
Blogroll
- Kim Cameron's Indentity Weblog
Close preview
Loading... - Jackson's Identity Management & Active Directory Reality Tour Travelblog
Close preview
Loading... - Tomek's DS World
Close preview
Loading... - IT Mum
Close preview
Loading... - 1dent1ty cHa0s
Close preview
Loading... - Clint Boessen
Close preview
Loading... - TechNet Flash Feed
Close preview
Loading... - You Had Me At EHLO
Close preview
Loading... - PuttyQ
Close preview
Loading... - Jorge 's Quest For Knowledge!
Close preview
Loading... - FIM / ILM Best Practices
Close preview
Loading... - IdM Crisis
Close preview
Loading... - ADdict
Close preview
Loading... - Anthony Ho
Close preview
Loading... - CShark
Close preview
Loading... - Darryl Russi's Blog
Close preview
Loading... - Gil's Blog
Close preview
Loading... - Identity Management Extensibility
Close preview
Loading... - Identity Underground
Close preview
Loading... - IdM for Real
Close preview
Loading... - Identity Trench
Close preview
Loading... - FIM 2010 Technet Wiki Articles
Close preview
Loading... - Identity Minded
Close preview
Loading...
- Kim Cameron's Indentity Weblog