How to Set Custom User Mailbox Limits in Exchange 2003 using vbScript

If you inherited an Exchange Server(s) from another Administrator (who died or otherwise), you might find yourself scratching your head or just cussing because of the issues that might have been left behind for you to deal with :)

However, you must understand that they did not intend to leave you those problems (or maybe they did). One of which is that you may find that there are users who do not have mailbox quotas. Of course its bad enough that they don't have quotas, what more if there are hundreds of them? Some of you would say "why not just create a system policy for mailbox size limits?" --- Valid point, but what if these users must have custom mailbox limits and not follow the standard policy for the mail stores?

If there are just 10 of them, I guess modifying the storage limits manually is fine. But again, if there are hundreds or more of these rouge mailboxes it would be best to do the task via script. 




'==========================================================================
'
' NAME: SetCustomMailboxLimits.vbs
'
' AUTHOR: Tito Castillote , june.castillote@gmail.com, shaking-off-the-cobwebs.blogspot.com
' DATE  : 11/12/2012
'
'
' Usage instructions:
' 1. You must create a text file (MailboxLimits.txt) containing the list of user DN (distinguished names)
'     and the storage limits to apply.
'
'        Format: [Distinguished Name]    [Prohibit Send and Receive]    [Prohibit Send]    [Issue Warning]
'        example: CN=Administrator,CN=Users,DC=LabWorks,DC=local    500000    450000    400000
'
'        - the DN and each values must be seperated with a TAB.
'            This script is designed for TAB-delimited Input file.
'
' 2. This script and the Input file must be in the same location and then run this from the Command Prompt.
'     Step 1. Create a backup of existing limits.
'        cscript //nologo SetCustomMailboxLimits.vbs BACKUP
'     Step 2. Modify the limits as found in MailboxLimits.txt
'        cscript //nologo SetCustomMailboxLimits.vbs MODIFY
'    Step 3. Restore the limits from backup (IF NEEDED)
'        cscript //nologo SetCustomMailboxLimits.vbs RESTORE
'
' COMMENT: If you plan to modify a bulk of users, I'd suggest that you do it in batches and not all at once.
'
'========================================================================

'Declare Constants to avoid "Invalid Procedure Calls or Arguments" errors.
Const ADS_PROPERTY_CLEAR = 1, ADS_PROPERTY_UPDATE = 2, ADS_PROPERTY_APPEND = 3
Const ForReading = 1, ForWriting = 2, ForAppending = 8

If WScript.Arguments.Count < 1 Then
    WScript.Echo "Pleas supply the Action (BACKUP or MODIFY)"
ElseIf WScript.Arguments(0)="BACKUP" Then
    WScript.Echo "=================================================="
    WScript.Echo (Now & ": Begin backup of user mailbox limits listed in MailboxLimits.txt")
    CreateBackup
    WScript.Echo (Now & ": Backup is saved to Backup.txt")
    WScript.Echo "=================================================="
ElseIf WScript.Arguments(0)="MODIFY" Then
    WScript.Echo "=================================================="
    WScript.Echo (Now & ": Begin modification of user mailbox limits listed in MailboxLimits.txt")
    ModifyLimits("MailboxLimits.txt")
    WScript.Echo (Now & ": Complete!")
    WScript.Echo "=================================================="
ElseIf WScript.Arguments(0)="RESTORE" Then
    WScript.Echo "=================================================="
    WScript.Echo (Now & ": Begin restoration of user mailbox limits listed in Backup.txt")
    ModifyLimits("Backup.txt")
    WScript.Echo (Now & ": Complete!")
    WScript.Echo "=================================================="
End If

'-----------------Backup Original Values---------------
Sub CreateBackup()

'Create File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO1 = CreateObject("Scripting.FileSystemObject")

Set BackUpName = objFSO1.CreateTextFile("Backup.txt",True)
Set MbxName = objFSO.OpenTextFile("MailboxLimits.txt", ForReading)

Do Until MbxName.AtEndofStream
    'Read the current line
    nUser=MbxName.ReadLine
    nuser=Replace(nUser,"/","\/")
   
    'Split the current line into an array with TAB as delimiter
    userArray=Split(nUser, vbTAB)

On Error Resume Next
    'Get the Object with DN name stored in UserArray(0)
    Set objUser = GetObject ("LDAP://" & UserArray(0))
    If Err.Number=0 Then
        'Check if user mailbox exists
        If objUser.homeMDB<>"" then
            'Get information about the Object (User) and start modifying values.
            objUser.GetInfo
           
            'This is just to display which user is being processed.
            WScript.Echo Now & ": Backup limits for - " & objUser.DisplayName
           
            'Write to file
            BackUpName.WriteLine(objUser.DistinguishedName & vbTab & objUser.mDBOverHardQuotaLimit & vbTab & objUser.mDBOverQuotaLimit & vbTab & objUser.mDBStorageQuota)
        Else
            WScript.Echo Now & ": Not an Exchange user - " & objUser.DisplayName
        End If
    Else
        'Set uName = Split(userarray(0),",")
        WScript.Echo Now & ": NOT FOUND!!! - " & userArray(0) 'Replace(uName(0),"CN=","")
        Err.Clear
    End If   
Loop


BackUpName.Close
MbxName.Close
Set BackUpName = Nothing
Set MbxName = Nothing
Set nUser = Nothing
Set userArray = Nothing
Set objFSO = Nothing
Set objFSO1 = Nothing
End Sub
'-----------------End Backup---------------------------


'-----------------Start Modification-------------------

Sub ModifyLimits(strInputFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the Input file
Set MbxName = objFSO.OpenTextFile(strInputFile, ForReading)

'Read the inout file from beginning to end
Do Until MbxName.AtEndofStream

    'Read the current line
    nUser=MbxName.ReadLine
    nuser=Replace(nUser,"/","\/")
   
    'Split the current line into an array with TAB as delimiter
    userArray=Split(nUser, vbTAB)

On Error Resume Next
    'Get the Object with DN name stored in UserArray(0)
    Set objUser = GetObject ("LDAP://" & UserArray(0))

    If Err.Number=0 Then
        If objUser.homeMDB<>"" then
            'Get information about the Object (User) and start modifying values.
            objUser.GetInfo
           
            'This is just to display which user is being processed.
            WScript.Echo Now & ": Processing User - " & objUser.DisplayName
       
            'Disable the "User Mailbox Store Defaults"
            objUser.Put "mDBUseDefaults", "FALSE"
           
            If UserArray(3) = "" Then
                objUser.PutEx ADS_PROPERTY_CLEAR, "mDBStorageQuota", 0
            Else
                objUser.Put "mDBStorageQuota", UserArray(3)
            End If
           
            If userArray(2) = "" Then
                objUser.PutEx ADS_PROPERTY_CLEAR, "mDBOverQuotaLimit", 0
            Else
                objUser.Put "mDBOverQuotaLimit", UserArray(2)
            End If
           
            If userArray(1) = "" Then
                objUser.PutEx ADS_PROPERTY_CLEAR, "mDBOverHardQuotaLimit", 0
            Else
                objUser.Put "mDBOverHardQuotaLimit", UserArray(1)
            End If
               
            'Save
            objUser.SetInfo   
        Else
            WScript.Echo Now & ": Not an Exchange user - " & objUser.DisplayName
        End If
    Else
        WScript.Echo Now & ": Not found - " & UserArray(0)
        Err.Clear
    End If   
Loop

'Close the Input file
MbxName.Close

'Clean up
Set objUser = Nothing
Set MbxName = Nothing
Set nUser = Nothing
Set UserArray = Nothing
Set objFSO = Nothing
End Sub
'-----------------End Modification-------------------

Share:

Popular Posts

Powered by Blogger.