HOWTO: Set Mailbox Quotas using CDOEXM

Are you someone who maintains Exchange 2003 servers? Did you ever have a requirement to set the Mailbox quotas for a lot of users at one go?  Here is your solution. The sample code below uses ADSI and Cdoexm to set mailbox quotas for multiple user.

The following sample is a simple VBScript code sample that sets the StoreQuota, OverQuotaLimit, Hardlimit for a mailbox. To use this sample, paste the following code in a new text file, and then name the file SetMailboxQuota.txt:

'This script sets the StoreQuota, OverQuotaLimit, Hardlimit for a mailbox.

' USAGE: cscript SetMailboxQuota.vbs DATAFILE
' This code has to be run on a box that has ESM or Exchange 2003 Installed.

Dim obArgs
Dim cArgs

Set obArgs = WScript.Arguments
cArgs = obArgs.Count


Sub Main()
Dim FileSysObj
Dim DataFileName
Dim DataFile
Dim sAMAccoutname
Dim vDistinguishedName

Const ForReading = 1, ForWriting = 2, ForAppending = 8, TristateFalse = 0

On error Resume Next
If cArgs <> 1 Then
      WScript.Echo "Usage: cscript SetMailboxQuota.vbs DATAFILE"
      Exit Sub
End If

Set FileSysObj = CreateObject("Scripting.FileSystemObject")

DataFileName = obArgs.Item(0)

Set DataFile = FileSysObj.OpenTextFile(DataFileName, ForReading, False,0)

Do While Not DataFile.AtEndOfStream
    sAMAccoutname = DataFile.ReadLine

    vDistinguishedName = GetDNFromSam(sAMAccoutname)

    If vDistinguishedName <> "" then
        WScript.Echo "Sam Account Name:" & sAMAccoutname
        WScript.Echo "Distinguished Name:" & vDistinguishedName

        'Please change limits as per your needs
        'The Size Specified is in Kb
        Call SetLimits(vDistinguishedName, 18432,20480,25600)

        If Err.Number <> 0 Then
              sMsg = "Error Setting Limits"
           sMsg = sMsg & Err.Number & " " & Err.Description
           WScript.Echo sMsg
    End If
       WScript.Echo "Could not get Distinguished Name for " & sAMAccoutname
    end if

Set DataFile = Nothing
Set FileSysObj = Nothing

End Sub

Function GetDNFromSam(strsAMAccountName)

Dim iAdCont
Dim iAdGC 
Dim Conn 
Dim Com 
Dim Rs 
Dim varGCAdsPath
Dim strQuery

   ' Get the Global Catalog
   Set iAdCont = GetObject("GC:")
   For Each iAdGC In iAdCont
      varGCAdsPath = iAdGC.ADsPath

   Set Conn = createobject("ADODB.Connection")
   Set Com = createobject("ADODB.Command")

   ' Open the Connection
   Conn.Provider = "ADsDSOObject"
   Conn.Open "ADs Provider"

   ' Build the query to find the user based on his Alias
   strQuery = "<" & varGCAdsPath & ">;(sAMAccountName=" & strsAMAccountName & ");mailNickName,distinguishedName;subtree"

   Com.ActiveConnection = Conn
   Com.CommandText = strQuery
   Set Rs = Com.Execute

   GetDNFromSam = Rs.Fields("distinguishedName")

   'Clean Up

   Set Rs = Nothing
   Set Com = Nothing
   Set Conn = Nothing
   Set iAdCont = Nothing
   Set iAdGC = Nothing

End Function

Sub SetLimits(strUserDN, vStoreQuota,vOverQuotaLimit,vHardLimit)

Dim strLDAP
Dim objPerson
Dim objMailbox

    strLDAP = "LDAP://" & strUserDN 

    Set objPerson = CreateObject("CDO.Person")
    objPerson.DataSource.Open strLDAP,,AD_MODE_READ_WRITE

    Set objMailbox = objPerson.GetInterface("IMailboxStore")

    'check if the user is mailbox enabled
    If objMailbox.HomeMDB = "" Then
        Wscript.Echo "No Mailbox found."
       'Give Warning at
        Wscript.Echo "Current StoreQuota:" & objMailbox.StoreQuota
        Wscript.Echo "Setting StoreQuota to:" & vStoreQuota & "(KB)"
        objMailbox.StoreQuota = vStoreQuota

       'Prevent Sending at
       Wscript.Echo "Current OverQuotaLimit:" & objMailbox.OverQuotaLimit & "(KB)"
       Wscript.Echo "Setting OverQuotaLimit to:" & vOverQuotaLimit & "(KB)"
       objMailbox.OverQuotaLimit = vOverQuotaLimit

       'Prevent Sending and Recieving at
       Wscript.Echo "Current Hardlimit:" & objMailbox.HardLimit & "(KB)"
       Wscript.Echo "Setting StoreQuota to:" & vHardLimit & "(KB)"
       objMailbox.HardLimit = vHardLimit

       ' Clearing defaults
       Wscript.Echo "Disabling Database Defaults"
       objMailbox.EnableStoreDefaults = false

    End If

    Set objMailbox = Nothing
    Set objPerson = Nothing

End Sub

The list of sAMAccountName's can be provided via a text file(Datafile). The Datafile contains the sAMAccountName's of the users(one on each line). So assuming your Datafile is called "sAMAccounts.txt" and is on the C:\, you would run the script as follows:

C:\>Cscript SetMailboxQuota.vbs C:\sAMAccounts.txt

The account that you are logged on to the computer must have permissions to change the limits on the mailboxes.

Comments (4)

  1. anu2004 says:

    Hi Akash,

    i m getting error lin at line 84.

  2. Akashb says:

    looks like you are getting a error at the line below

    Set Conn = createobject("ADODB.Connection")

    Make sure you MDAC is installed correctly. Use the utility below check if something is wrong.

    Component Checker: Diagnose problems and reconfigure MDAC installations

  3. Donesh Bajaj says:

    Getting error while running this code

    "Error Setting Limits429 ActiveX component can’t create object"

  4. Akashb says:

    Are you running the code on the Server? make sure you have cdoex registered. Looks like the code is failing when it’s trying to create a object of CDO.Person

    Person CoClass

Skip to main content