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

[求助]使用脚本从Exchange共享邮箱中删除邮件 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2011-03-13
— 本帖被 YOYO 从 Exchange 脚本中心 移动到本区(2015-06-25) —
  • 脚本环境:Visual Basic
  • 适合版本:Exchange 2003
  • 适用平台:
  • 脚本作用:从Exchange共享邮箱中删除邮件
使用此脚本可以从Exchange共享邮件中删除邮件。

' for the basis for this, see http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug06/hey0803.mspx
' this will read all emails from any mailbox that is shared in Outlook by a user
' Currently this runs as a scheduled job on my workstation since he has it shared
' you may receive a prompt for permission to run against Outlook (at least once), allow it to run for 5 minutes or so depending on how
' much you need to delete
' we need the error skip code because Outlook sometimes has bad data and we want to keep going
On Error Resume Next
Dim oShell
Set oShell = CreateObject("Wscript.Shell")
' we need to make sure it runs as cscript because we produce a log in a command box
'forceUseCScript
Sub forceUseCScript()

    If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
        oShell.Popup "Launched using wscript. Relaunching...",3,"WSCRIPT"
        oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),0,False
        WScript.Quit 0
    End If

End Sub

Set objOutlook = CreateObject("Outlook.Application")
'Set objOutlook   = Application)

Set objNamespace = objOutlook.GetNamespace("MAPI")
' put the name in quotes just as it appears in Outlook - THIS WILL CHANGE BASED ON YOUR MAIL BOX NAME!!!!!!
'********************************************************************************************
Set objMailbox   = objNamespace.Folders("Mailbox - IT Operations-2")
'********************************************************************************************
'Set objMailbox   = objNamespace.Folders("Inbox")
Set objFolder    = objMailbox.Folders("Inbox")

Set colItems     = objFolder.Items
' get the total number of emails in case we need it later
mycount          = objFolder.Items.Count
'Wscript.Echo ("Processing " & mycount & " items.")
'Wscript.Echo ("If you get an Outlook permissions prompt, say Yes for 5 minutes.")

For Each objItem in colItems
    ' these are some of the optional data fields we can look at:
    '    if  not isnull(objItem.Subject) then Wscript.Echo "Subject: " & objItem.Subject
    '    if  not isnull(objItem.body) then Wscript.Echo "Body: " & objItem.body
    '    if  not isnull(objItem.sent) then Wscript.Echo "Sent: " & objItem.sent
    '    if  not isnull(objItem.SenderEmailAddress) then Wscript.Echo "Email Address: " & objItem.SenderEmailAddress
    '    if  not isnull(objItem.Size) then Wscript.Echo "Size: " & objItem.Size
    'on error resume next
    '    if  isNull(objItem.ReceivedTime) then
    '               wscript.echo "Null value found"
    '    else
    '        Wscript.Echo  objItem.ReceivedTime
    '        end if
    '    if  not isnull(objItem.Class ) then Wscript.Echo "Class: " & objItem.Class
    '    if  not isnull(objItem.UnRead) then Wscript.Echo "UnRead (True/False): " & objItem.UnRead  
    '    if  not isnull(objItem.SenderName) then Wscript.Echo "Sender Name: " & objItem.SenderName
    

    ' save the received date
    strRcdTime = objItem.ReceivedTime
    'msgbox (objItem.ReceivedTime)
    ' split delimited by space to get rid if time element
    wrk               = Split(strRcdTime, " ")
    ' split delimited by "/"  for date element
    arrTime           = Split(wrk(0), "/")
    ' get year, month , and day
    strYear           = arrTime(2)
    strDay            = arrTime(1)
    strMonth          = arrTime(0)
    ' assemble the recived date in MM/DD/YYYY format
    emailReceivedDate = strMonth & "/" & strDay & "/" & StrYear

    ' assign today’s date (Date) to a variable named emailEndingDate
    emailEndingDate = Date

    ' use "d" because we are interested in the difference in terms of days

    intDays = DateDiff("d", emailReceivedDate, emailEndingDate)
    'Wscript.Echo ("Received: " & emailReceivedDate & " Cutoff date is: " & emailEndingDate & " difference is: " & intdays & " Subject: " & objItem.Subject)
'********************************************************************************************
' in this case I have hardcoded "7" for the number days - THIS MAY CHANGE IN YOUR IMPLEMENTATION
    If intDays > 7 Then
'********************************************************************************************
        'Wscript.Echo "Deleting: " & objItem.Subject & " received at " & objItem.ReceivedTime & " which is " & intDays & " days old."
        objItem.delete
    End If

Next
分享到
快速回复
限60 字节
 
上一个 下一个