切换到宽版
  • 10519阅读
  • 0回复

[求助]使用脚本清除已断开连接的邮箱 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2011-03-10
— 本帖被 YOYO 从 Exchange 脚本中心 移动到本区(2015-06-25) —
  • 脚本环境:Visual Basic
  • 适合版本:Exchange 2003
  • 适用平台:
  • 脚本作用:清除已断开连接的邮箱
使用些脚本可以在目标服务器上枚举所有断开连接的邮箱,列出的每个邮箱,用户和总大小,然后让用户选择删除这些邮箱。

Option Explicit

Dim sComputerName    ' Exchange Server
Dim oWMIService    
Dim oDisMbox        ' Disconnected Mailbox Objects
Dim oIE                ' Internet Explorer object
Dim mBox            ' Single disconnected mailbox
Dim cMailboxes        ' Collection of Display names for disconnected mailboxes
Dim sAnswer            ' Message box response
Dim sWinMgmts        ' Connection string
Dim i                ' Counter variable
Dim intPaddingName    ' Reporting whitespace
Dim intPaddingSize    ' Reporting whitespace
Dim intTotalSize    ' Sum of disconnected mailbox sizes
' --------------------------------------------------------
' STATIC VARIABLE ASSIGNMENTS
' --------------------------------------------------------
CONST WMI_NAME_SPACE = "root/MicrosoftExchangeV2"
CONST WMI_INSTANCE = "Exchange_Mailbox"
CONST NAME_WIDTH = 40
CONST SIZE_WIDTH = 15
CONST QUIT_IE_ON_EXIT = FALSE    ' Auto close IE Window
' --------------------------------------------------------
' MAIN SCRIPT CODE
' --------------------------------------------------------
' Check for server name passed as a command line argument
If WScript.Arguments.Count > 0 Then
    sComputerName = WScript.Arguments(0)
Else
    ExitAndUse()
End If

' Echo to CMD windows
WScript.Echo "Please wait while enumerating disconnected " _
    & "mailboxes on server " & sComputerName & "..."

sWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" _
    & sComputerName & "/" & WMI_NAME_SPACE

Set oWMIService = GetObject(sWinMgmts)
Set oDisMbox = oWMIService.ExecQuery _
    ("Select * from Exchange_Mailbox " _
        & "WHERE DateDiscoveredAbsentInDS " _
        & "IS NOT Null")

' Enter only if there are disconnected mailboxes
If oDisMbox.count > 0 Then
    Set oIE = WScript.CreateObject("InternetExplorer.Application")
    oIE.Navigate "about:blank"  
    oIE.ToolBar = 1
    oIE.StatusBar = 0
    oIE.Width=800
    oIE.Height = 600
    oIE.Left = 0
    oIE.Top = 0

    ' Wait for IE
    Do While (oIE.Busy)
        WScript.Sleep 200
    Loop    

    ' Make IE visable
    oIE.Visible = 1            
    
    ' Zero variables
    i = 0
    intTotalSize = 0
    
    ' Header: Set fixed type font
    cMailboxes = "<pre>"

    ' Create list of disconnected mailbox display names
    For Each mBox In oDisMbox
        i = i + 1
        intTotalSize = intTotalSize + mBox.Size
        intPaddingName = NAME_WIDTH - Len(mBox.MailboxDisplayName)
        intPaddingSize = SIZE_WIDTH - Len(mBox.Size)
        cMailboxes = cMailboxes & mBox.MailboxDisplayName _
            & Space(intPaddingName) & Space(intPaddingSize) _
            & mBox.Size & " KB<BR>"
    Next

    ' Footer: Total mailboxes sizes
    cMailboxes = cMailboxes & Space(NAME_WIDTH) _
        & "-------------------<BR>"
    intTotalSize = Round(intTotalSize / 1024)
    intPaddingSize = SIZE_WIDTH - Len(intTotalSize)
    cMailboxes = cMailboxes & Space(NAME_WIDTH) _
        & Space(intPaddingSize) & intTotalSize & " MB</pre>"
    oIE.Document.Body.InnerHTML = oIE.Document.Body.InnerHTML & vbCRLF _
        & "<H3>Disconnected Mailboxes on " & sComputerName _
        & ": " & oDisMbox.count & "</H3>" & vbCRLF _
        & cMailboxes

    ' Proceed or Quit
    sAnswer = MsgBox("Purge disconnected mailboxes?" , 20, sComputerName)
    If sAnswer = 6 Then ' 6 = Yes
        i = oDisMbox.Count
        For Each mBox In oDisMbox
            i = i - 1
            oIE.Document.Body.InnerHTML = oIE.Document.Body.InnerHTML _
                & "<BR>Purging Mailbox: " & mBox.MailboxDisplayName _
                & " (" & i & " remaining)"
            ' Purge command, comment out for testing
            mBox.Purge
        Next
        oIE.Document.Body.InnerHTML = oIE.Document.Body.InnerHTML & "<BR>Done!"
    Else ' Exit on anything other than Yes
        If QUIT_IE_ON_EXIT Then
            oIE.Quit
        End If
        WScript.Quit
    End If
Else
    WScript.echo "No disconnected mailboxes found!"
End if

' Finish Script
WScript.Echo "Done!"
If QUIT_IE_ON_EXIT Then
    WScript.Sleep 3000
    oIE.Quit
End If

'Object Cleanup
Set oWMIService = Nothing
Set oDisMbox = Nothing
If QUIT_IE_ON_EXIT Then
    Set    oIE = Nothing
End If
WScript.Quit

' --------------------------------------------------------
' SUBS AND FUNCTIONS
' --------------------------------------------------------

Sub ExitAndUse()
    MsgBox "Server Name Required" & vbCRLF _
        & "Example: cscript //nologo PurgeMailboxes.vbs EXCHSVR01",0,"ERROR"
    ' Command line equivalent
    ' WScript.echo vbCRLF & "Server Name Required" & vbCRLF _
    '    & "Example: cscript //nologo purgeMailboxes.vbs EXCH01"
    WScript.Quit
End Sub
分享到
快速回复
限60 字节
 
上一个 下一个