设为首页 收藏本站
查看: 1375|回复: 0

[经验分享] HOWTO: Programmatically Get the Size of Mailboxes on Exchange ServerPSS ID Numbe

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-11 11:32:07 | 显示全部楼层 |阅读模式
  Knowledge Base  
  HOWTO: Programmatically Get the Size of Mailboxes on Exchange ServerPSS ID Number: Q320071
  Article Last Modified on 08-14-2002
  
--------------------------------------------------------------------------------
The information in this article applies to:
  Microsoft Exchange 2000 Server
Microsoft Exchange Server 5.5
  --------------------------------------------------------------------------------
  
Summary
This article describes how to programmatically retrieve information about the size of mailboxes on a server that is running Exchange Server by using Microsoft Visual C, Microsoft Visual C++, Microsoft Visual Basic, and Microsoft Visual Basic Scripting Edition (VBScript).
  
  More Information
  Extended MAPI Approach
Programming Languages Available: Visual C, Visual C++
Exchange Server Versions: Exchange Server 5.5 and Exchange 2000
  Extended MAPI provides the IExchangeManageStore interface to obtain management information from an Exchange information store. This interface provides the GetMailboxTable function, which returns an IMAPITable interface that contains information about the mailboxes on a particular server. Note that this function requires administrative access to the server that is running Exchange Server.
  This table includes a column that provides information about the size of each mailbox. The PR_MESSAGE_SIZE property contains the size (in bytes) of the mailbox.
  For additional information, visit the following Microsoft Developer Network (MSDN) Web site:
IExchangeManageStore::GetMailboxTable
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchserv/html/intrface_24it.asp
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
Q200160 HOWTO: Loop Through Mailboxes on Exchange Using GetMailboxTable
CDO 1.21 Approach
Programming Languages Available: VBScript, Visual Basic, Visual C, Visual C++
Exchange Server Versions: Exchange Server 5.5 and Exchange 2000
  You can use Collaboration Data Objects (CDO) 1.21 to check the same property on a user mailbox store through the CDO InfoStore object. In this approach, your application must log on to a mailbox before the application can check this property. This means that your application must have access to all mailboxes on the server. This approach also requires that you generate a list of mailboxes on the server and then loop through them in code.
  The following sample is a simple VBScript code sample that can check this property on a mailbox. To use this sample, paste the following code in a new text file, and then name the file Mailboxsize.vbs:
'This script logs on to a server that is running Exchange Server and
'displays the current number of bytes that are used in the user's
'mailbox and the number of messages.
  ' USAGE: cscript MailboxSize.vbs SERVERNAME MAILBOXNAME
  ' This requires that CDO 1.21 is installed on the computer.
' This script is provided AS IS. It is intended as a SAMPLE only.
' Microsoft offers no warranty or support for this script.  
' Use at your own risk.
  ' Get command line arguments.
Dim obArgs
Dim cArgs
  Set obArgs = WScript.Arguments
cArgs = obArgs.Count
  Main
  Sub Main()
   Dim oSession
   Dim oInfoStores
   Dim oInfoStore
   Dim StorageUsed
   Dim NumMessages
   Dim strProfileInfo
   Dim sMsg
  On Error Resume Next
  If cArgs <> 2 Then
      WScript.Echo &quot;Usage: cscript MailboxSize.vbs SERVERNAME MAILBOXNAME&quot;
      Exit Sub
   End If
  'Create Session object.
   Set oSession = CreateObject(&quot;MAPI.Session&quot;)
   if Err.Number <> 0 Then
      sMsg = &quot;Error creating MAPI.Session.&quot;
      sMsg = sMsg & &quot;Make sure CDO 1.21 is installed. &quot;
      sMsg = sMsg & Err.Number & &quot; &quot; & Err.Description
      WScript.Echo sMsg
      Exit Sub
   End If
   
   strProfileInfo = obArgs.Item(0) & vbLf & obArgs.Item(1)
  'Log on.
   oSession.Logon , , False, True, , True, strProfileInfo
   if Err.Number <> 0 Then
      sMsg = &quot;Error logging on: &quot;
      sMsg = sMsg & Err.Number & &quot; &quot; & Err.Description
      WScript.Echo sMsg
      WScript.Echo &quot;Server: &quot; & obArgs.Item(0)
      WScript.Echo &quot;Mailbox: &quot; & obArgs.Item(1)
      Set oSession = Nothing
      Exit Sub
   End If
  'Grab the information stores.
   Set oInfoStores = oSession.InfoStores
   if Err.Number <> 0 Then
  sMsg = &quot;Error retrieving InfoStores Collection: &quot;
      sMsg = sMsg & Err.Number & &quot; &quot; & Err.Description
      WScript.Echo sMsg
      WScript.Echo &quot;Server: &quot; & obArgs.Item(0)
      WScript.Echo &quot;Mailbox: &quot; & obArgs.Item(1)
      Set oInfoStores = Nothing
      Set oSession = Nothing
      Exit Sub
   End If
   
   'Loop through information stores to find the user's mailbox.
   For Each oInfoStore In oInfoStores
      If InStr(1, oInfoStore.Name, &quot;Mailbox - &quot;, 1) <> 0 Then
         '&HE080003 = PR_MESSAGE_SIZE
         StorageUsed = oInfoStore.Fields(&HE080003)
         if Err.Number <> 0 Then
            sMsg = &quot;Error retrieving PR_MESSAGE_SIZE: &quot;
            sMsg = sMsg & Err.Number & &quot; &quot; & Err.Description
            WScript.Echo sMsg
            WScript.Echo &quot;Server: &quot; & obArgs.Item(0)
            WScript.Echo &quot;Mailbox: &quot; & obArgs.Item(1)
            Set oInfoStore = Nothing
            Set oInfoStores = Nothing
            Set oSession = Nothing
            Exit Sub
         End If
         
         '&H33020003 = PR_CONTENT_COUNT
         NumMessages = oInfoStore.Fields(&H36020003)
  if Err.Number <> 0 Then
  sMsg = &quot;Error Retrieving PR_CONTENT_COUNT: &quot;
            sMsg = sMsg & Err.Number & &quot; &quot; & Err.Description
            WScript.Echo sMsg
            WScript.Echo &quot;Server: &quot; & obArgs.Item(0)
            WScript.Echo &quot;Mailbox: &quot; & obArgs.Item(1)
            Set oInfoStore = Nothing
            Set oInfoStores = Nothing
            Set oSession = Nothing
            Exit Sub
         End If
  sMsg = &quot;Storage Used in &quot; & oInfoStore.Name
         sMsg = sMsg & &quot; (bytes): &quot; & StorageUsed
         WScript.Echo sMsg
         WScript.Echo &quot;Number of Messages: &quot; & NumMessages
      End If
   Next
  ' Log off.
   oSession.Logoff
  ' Clean up memory.
   Set oInfoStore = Nothing
   Set oInfoStores = Nothing
   Set oSession = Nothing
End Sub
ActiveX Data Objects (ADO) Approach
Programming Languages Available: VBScript, Visual Basic, Visual C, Visual C++
Exchange Server Versions: Exchange 2000 Server
  In Exchange 2000, you can access the Exchange information store through ActiveX Data Objects (ADO). No property that is stored on the mailbox contains the total size. However, each folder in a user mailbox has its size stored in a field that is named http://schemas.microsoft.com/mapi/proptag/x0e080003. This corresponds to the MAPI property, PR_MESSAGE_SIZE.
  By adding the size of all of the folders in a user mailbox, the total size of the mailbox can be computed. To do this, you use a SHALLOW traversal to recursively traverse the folders in the mailbox. Although you can use a DEEP traversal, this approach is avoided because of the issues that DEEP traversal raises. For additional information about the issues with a DEEP traversal, click the article number below to view the article in the Microsoft Knowledge Base:
Q216076 XADM: Accessing Information Store Folders May Become Slow
In this approach, you must run your code on the server that is running Exchange 2000. OLE DB Provider for Exchange (EXOLEDB) cannot access mailboxes remotely.
  The following sample is a simple VBScript code sample that can retrieve the size of a mailbox. To use this sample, paste the following code in a new text file, and then name the file Mailboxsize.vbs:
'This script logs on to a server that is running Exchange 2000 and
'displays the current number of bytes that are used in the user's
'mailbox.
  ' USAGE: cscript MailboxSize.vbs DOMAINNAME MAILBOXNAME
  ' You must run this code on the computer that is running Exchange 2000.
' This script is provided AS IS. It is intended as a SAMPLE only.
' Microsoft offers no warranty or support for this script.  
' Use at your own risk.
  ' Get command line arguments
  Dim obArgs
Dim cArgs
Dim iSize
  Set obArgs = WScript.Arguments
cArgs = obArgs.Count
  Main
  Sub Main()
   Dim sConnString
  On Error Resume Next
  If cArgs <> 2 Then
      WScript.Echo &quot;Usage: cscript MailboxSize.vbs DOMAINNAME MAILBOXNAME&quot;
      Exit Sub
   End If
  ' Set up connection string to mailbox.
   sConnString = &quot;file://./backofficestorage/&quot; & obArgs.Item(0)
   sConnString = sConnString & &quot;/mbx/&quot; & obArgs.Item(1) & &quot;/NON_IPM_SUBTREE&quot;
   WScript.Echo sConnString
  iSize = 0
  RecurseFolder(sConnString)
  WScript.Echo &quot;Mailbox Size: &quot; & iSize
End Sub
  Public Sub RecurseFolder(sConnString)
   Dim oConn
   Dim oRecSet
   Dim sSQL
  ' Set up SQL SELECT statement.
   sSQL = &quot;SELECT &quot;&quot;http://schemas.microsoft.com/mapi/proptag/x0e080003&quot;&quot;, &quot;
   sSQL = sSQL & &quot;&quot;&quot;DAV:href&quot;&quot;, &quot;
   sSQL = sSQL & &quot;&quot;&quot;DAV:hassubs&quot;&quot; &quot;
   sSQL = sSQL & &quot;FROM SCOPE ('SHALLOW TRAVERSAL OF &quot;&quot;&quot; & sConnString
   sSQL = sSQL & &quot;&quot;&quot;') WHERE &quot;&quot;DAV:isfolder&quot;&quot; = true&quot;
   WScript.Echo sSQL
  ' Create Connection object.
   Set oConn = CreateObject(&quot;ADODB.Connection&quot;)
   if Err.Number <> 0 then
      WScript.Echo &quot;Error creating ADO Connection object: &quot; & Err.Number & &quot; &quot; & Err.Description
   end if
  ' Create RecordSet object.
   Set oRecSet = CreateObject(&quot;ADODB.Recordset&quot;)
   if Err.Number <> 0 then
      WScript.Echo &quot;Error creating ADO RecordSet object: &quot; & Err.Number & &quot; &quot; & Err.Description
      Set oConn = Nothing
      Exit Sub
   end if
  ' Set provider to EXOLEDB.
   oConn.Provider = &quot;Exoledb.DataSource&quot;
  ' Open connection to folder.
   oConn.Open sConnString
   if Err.Number <> 0 then
      WScript.Echo &quot;Error opening connection: &quot; & Err.Number & &quot; &quot; & 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 &quot;Error opening recordset: &quot; & Err.Number & &quot; &quot; & 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 &quot;Error moving to first record: &quot; & Err.Number & &quot; &quot; & 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(&quot;http://schemas.microsoft.com/mapi/proptag/x0e080003&quot;)      
  ' If the folder has subfolders, recursively call RecurseFolder to process them.
      If oRecSet.Fields.Item(&quot;DAV:hassubs&quot;) = True then
         RecurseFolder oRecSet.Fields.Item(&quot;DAV:href&quot;)
      End If
      ' Move to next record.
      oRecSet.MoveNext
      if Err.Number <> 0 then
         WScript.Echo &quot;Error moving to next record: &quot; & Err.Number & &quot; &quot; & 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 &quot;Error closing recordset: &quot; & Err.Number & &quot; &quot; & Err.Description
      Set oRecSet = Nothing
      Set oConn = Nothing
      Exit Sub
   end if
  oConn.Close
   if Err.Number <> 0 then
      WScript.Echo &quot;Error closing connection: &quot; & Err.Number & &quot; &quot; & Err.Description
      Set oRecSet = Nothing
      Set oConn = Nothing
      Exit Sub
   end if
  ' Clean up memory.
   Set oRecSet = Nothing
   Set oConn = Nothing
End Sub
For additional information about this topic and a Visual C++ sample that retrieves the size of mailboxes, click the article number below to view the article in the Microsoft Knowledge Base:
Q291368 HOWTO: Determine the Size of Exchange 2000 Server Mailbox with ADO in C++
WebDAV Approach
Languages: VBScript, Visual Basic, Visual C
Exchange Server Versions: Exchange 2000 Server
  You can also use WebDAV similarly to the ADO approach. The property and the algorithm are the same in that you can add the values of http://schemas.microsoft.com/mapi/proptag/x0e080003 from all of the subfolders in a mailbox. One advantage of using WebDAV instead of ADO is that the code does not have to run on the computer that is running Exchange 2000 Server.
  To use this sample, paste the following code in a new text file, and then name the file Mailboxsize.vbs:
'This script logs on to a server that is running Exchange Server and
'displays the current number of bytes that are used in the user's
'mailbox.
  ' USAGE: cscript MailboxSize.vbs SERVERNAME MAILBOXNAME USERNAME PASSWORD
  ' This script is provided AS IS. It is intended as a SAMPLE only.
' Microsoft offers no warranty or support for this script.
' Use at your own risk.
  ' Get command line arguments.
  Dim obArgs
Dim cArgs
Dim iSum
  Set obArgs = WScript.Arguments
cArgs = obArgs.Count
  Main
  Sub Main()
   Dim sUrl
   Dim sMsg
  On Error Resume Next
  iSum = 0
  ' Check argument count.
   If cArgs <> 4 Then
      sMsg = &quot;Usage: cscript MailboxSize.vbs &quot;
      sMsg = sMsg & &quot;SERVERNAME MAILBOXNAME USERNAME PASSWORD&quot;
      WScript.Echo sMsg
      Exit Sub
   End If
  sUrl = &quot;http://&quot; & obArgs.Item(0) & &quot;/exchange/&quot; & obArgs.Item(1) & &quot;/NON_IPM_SUBTREE&quot;
   wscript.echo sUrl
  RecurseFolder(sUrl)
  WScript.Echo &quot;Mailbox Size: &quot; & iSum
End Sub
  Public Sub RecurseFolder(sUrl)
   Dim oXMLHttp
   Dim oXMLDoc
   Dim oXMLSizeNodes
   Dim oXMLHREFNodes
   Dim oXMLHasSubsNodes
   Dim sQuery
  Set oXMLHttp = CreateObject(&quot;Microsoft.xmlhttp&quot;)
   If Err.Number <> 0 Then
      WScript.Echo &quot;Error Creating XML object&quot;
      WScript.Echo Err.Number & &quot;: &quot; & Err.Description
      Set oXMLHttp = Nothing
   End If
  ' Open DAV connection.
   oXMLHttp.open &quot;SEARCH&quot;, sUrl, False, obArgs.Item(2), obArgs.Item(3)
   If Err.Number <> 0 Then
      WScript.Echo &quot;Error opening DAV connection&quot;
      WScript.Echo Err.Number & &quot;: &quot; & Err.Description
      Set oXMLHttp = Nothing
   End If
  ' Set up query.
   sQuery = &quot;<?xml version=&quot;&quot;1.0&quot;&quot;?>&quot;
   sQuery = sQuery & &quot;<g:searchrequest xmlns:g=&quot;&quot;DAV:&quot;&quot;>&quot;
   sQuery = sQuery & &quot;<g:sql>SELECT &quot;&quot;http://schemas.microsoft.com/&quot;
   sQuery = sQuery & &quot;mapi/proptag/x0e080003&quot;&quot;, &quot;&quot;DAV:hassubs&quot;&quot; FROM SCOPE &quot;
   sQuery = sQuery & &quot;('SHALLOW TRAVERSAL OF &quot;&quot;&quot; & sUrl & &quot;&quot;&quot;') &quot;
   sQuery = sQuery & &quot;WHERE &quot;&quot;DAV:isfolder&quot;&quot; = true&quot;
   sQuery = sQuery & &quot;</g:sql>&quot;
   sQuery = sQuery & &quot;</g:searchrequest>&quot;
  ' Set request headers.
   oXMLHttp.setRequestHeader &quot;Content-Type&quot;, &quot;text/xml&quot;
   oXMLHttp.setRequestHeader &quot;Translate&quot;, &quot;f&quot;
   oXMLHttp.setRequestHeader &quot;Depth&quot;, &quot;0&quot;
   oXMLHttp.setRequestHeader &quot;Content-Length&quot;, &quot;&quot; & Len(sQuery)
  ' Send request.
   oXMLHttp.send sQuery
   If Err.Number <> 0 Then
      WScript.Echo &quot;Error Sending Query&quot;
      WScript.Echo Err.Number & &quot;: &quot; & Err.Description
      Set oXMLHttp = Nothing
   End If
  ' Load XML.
   Set oXMLDoc = oXMLHttp.responseXML
  ' Get the XML nodes that contain the individual sizes.
   Set oXMLSizeNodes = oXMLDoc.getElementsByTagName(&quot;d:x0e080003&quot;)
  ' Get the XML nodes that contain the individual HREFs.
   Set oXMLHREFNodes = oXMLDoc.getElementsByTagName(&quot;a:href&quot;)
  ' Get the XML nodes that contain the individual HasSubs.
   Set oXMLHasSubsNodes = oXMLDoc.getElementsByTagName(&quot;a:hassubs&quot;)
  ' Loop through the nodes, and then add all of the sizes.
   For i = 0 to oXMLSizeNodes.length - 1
      WScript.Echo oXMLHREFNodes.Item(i).nodeTypedValue
      WScript.Echo &quot;Size: &quot; & oXMLSizeNodes.Item(i).nodeTypedValue
      iSum = iSum + oXMLSizeNodes.Item(i).nodeTypedValue
  ' If the folder has subfolders, call your recursive function to
      ' process subfolders.
      If oXMLHasSubsNodes.Item(i).nodeTypedValue = True Then
         RecurseFolder oXMLHREFNodes.Item(i).nodeTypedValue
      End If
   Next
  ' Clean up.
   Set oXMLSizeNodes = Nothing
   Set oXMLDoc = Nothing
   Set oXMLHttp = Nothing
End Sub
  Additional query words:
  Keywords:
Issue Type: kbhowto
Technology: kbExchangeSearch kbExchange550 kbZNotKeyword2 kbExchange2000Search kbExchange2000Serv
  
  --------------------------------------------------------------------------------
  Send feedback to Microsoft
  ? 2002-2003 Microsoft Corporation. All rights reserved.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-112277-1-1.html 上篇帖子: WCF初体验(C#操作Exchange) 下篇帖子: Exchange 2007 Server Shutdown Script
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表