Office 365 UnRead电子邮件PowerShell管理报告

时间:2017-02-15 04:31:39

标签: powershell email report office365 read-unread

我开发了一个Powershell脚本,为每个用户生成Office 365的UnRead电子邮件报告。

1.-这仅适用于管理员用户帐户 2.-管理员用户在Exchange Server(Applicationimpersonate)中需要以下角色

# work with Office 365 exchange server to retrieve messages
# Check Admin user have Applicationimpersonate Role

Set-ExecutionPolicy RemoteSigned

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session


# load the assembly
[void] [Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll")
$s = New-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)


if ($UserCredential -ne $null) 
{
    $s.Credentials = $UserCredential.GetNetworkCredential()
}
else 
{
    # Credencial for Admin User
    $UserCredential = Get-Credential 
    $s.Credentials = $UserCredential.GetNetworkCredential()
}


# This URL is used for Office 365
# You can use AutodiscoverUrl in case of don't have URL
$s.Url= new-object Uri(“https://outlook.office365.com/EWS/Exchange.asmx”)

# To use AutoDiscovery
# discover the url from your email address
# $TestUrlCallback = {
#  param ([string] $url)
#  if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
# }
#  $s.AutodiscoverUrl($email,$TestUrlCallback)


# get a handle to the inbox, this function get all inbox, you can replace with a address file 
# uncomment this line
# For All Inbox

Write-host "Name , Email , Toatl in Inbox, UnRead in Inbox , Sent "
get-mailbox | foreach-object{
    $mbcomb = "" | select DisplayName,EmailAddress,Inbox_Number_Unread,Inbox_Unread_LastRecieved,Sent_Items_LastSent
    $mbcomb.DisplayName = $_.DisplayName.ToString()
    $mbcomb.EmailAddress = $_.WindowsEmailAddress.ToString()
    $email = $_.WindowsEmailAddress
    $folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $email);
    $s.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$email)
    $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,$folderId)
    $folderIdS = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems, $email);
    $Sent = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,$folderIdS)
    Write-host $mbcomb.DisplayName "," $email "," $inbox.TotalCount "," $inbox.UnreadCount ","  $Sent.TotalCount
}



# For email address in a File 
#function GetUnreadInboxMails([string] $emailAddress) 
#{
#
#    $folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $emailAddress);
#    $s.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$emailAddress)
#    $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,$folderId)
#    $folderIdS = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems, $emailAddress);
#    $Sent = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,$folderIdS)
#    Write-host $emailAddress "," $inbox.UnreadCount "," $Sent.TotalCount "," $inbox.TotalCount 
#}
#$addresses = Import-Csv "c:\temp\email-list.txt"
#$addresses | % {GetUnreadInboxMails($_.MailAddress)}  

0 个答案:

没有答案