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

[求助]使用脚本在邮箱中统计未读邮件的数量 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2011-03-13
— 本帖被 YOYO 从 Exchange 脚本中心 移动到本区(2015-06-25) —
  • 脚本环境:PowerShell
  • 适合版本:Exchange 2010
  • 适用平台:
使用此脚本可以在邮箱中统计未读邮件的数量。


#Windows PowerShellCopy Code###########################################################################
#
# NAME: UnReadStats.ps1
#
# AUTHOR: John Grenfell
# EMAIL: john.grenfell@wiltshire.ac.uk
#
# COMMENT:
#
# You have a royalty-free right to use, modify, reproduce, and
# distribute this script file in any way you find useful, provided that
# you agree that the creator, owner above has no warranty, obligations,
# or liability for such use.
#
# VERSION HISTORY:
# 1.1 03.02.2011 - Beta release
#
# Make sure you've downloaded  Microsoft Exchange Web Services (EWS) Managed API 1.1
# http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
#
# Note : keep an eye on the view total counts as we I'm not sure if we should set them higher
###########################################################################


#Who are we looking for? - Note we need full rights to connect to this mailbox
$MailboxName = "john.grenfell@company.com"


#Load the dll
$WebServicesdll = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($WebServicesdll)
$Service = New-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)


#Connect to Exchange as the current user
$WindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$SidBind = "LDAP://<SID=" + $WindowsIdentity.User.Value.ToString() + ">"
$AceUser = [ADSI]$SidBind
$Service.AutodiscoverUrl($AceUser.Mail.ToString())


#Hook up to the mailbox folder
$FolderId = New-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$FolderId)
$SearchForUnRead = New-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead, $false)


#Define my item and folder views
$ItemView = New-object Microsoft.Exchange.WebServices.Data.ItemView(2000)
$FolderView = New-object Microsoft.Exchange.WebServices.Data.FolderView(10000)
$FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep


#Find all folders
$AllFolders = $InboxFolder.FindFolders($FolderView);


#Reset total count
$UnReadEmailTotal = 0


#Loop through all the folders found
ForEach ($Folder in $AllFolders){
    "Currently working on : " + $Folder.DisplayName
    If ($Folder.UnreadCount -gt 0){


        "- Number of Unread Messages : " + $Folder.UnreadCount


        $FindItemsResult = $Folder.FindItems($SearchForUnRead,$ItemView)


        "-- Last Mail From : " + $FindItemsResult.Items[0].From.Name
        "-- Subject : " + $FindItemsResult.Items[0].Subject
        "-- Sent : " + $FindItemsResult.Items[0].DateTimeSent
    }
    $UnReadEmailTotal = $UnReadEmailTotal + $Folder.UnreadCount


}


#Output total unread items
"Total unread email : " + $UnReadEmailTotal
分享到
快速回复
限60 字节
 
上一个 下一个