Utilizing EWS and AQS to examine variety of Unread in X days with Graph It appears which you can by no means have too some ways of studying the variety of Unread messages in a Inbox so right here is one other technique. The next script makes use of two AQS queries to work out first the variety of messages within the Inbox from a prescribed interval eg the final 30 days after which the variety of these which are then unread. It then works out the proportion of unread utilizing of the 2 values after which creates a graph to show within the Console utilizing Alt-ASCII characters which produces a fairly nifty output eg
It additionally produces a CSV output eg
AQS queries will work in Change 2010 and 2013 so this script ought to work okay in these environments in addition to ExchangeOnline.
I’ve created two variations of this script, the primary model simply stories on one person so that you run it with the e-mail of the customers you wish to report on and the variety of days to question again. eg
.AQSUsr.ps1 [email protected] 30
the second model stories on the entire customers in a CSV file and makes use of EWS Impersonation to entry the Mailboxes. To make use of the script it’s worthwhile to have configured EWS Impersonation and feed the script a CSV file of the customers you wish to report on eg the CSV file ought to seem like
smtpaddress
[email protected]
[email protected]
and also you run it like
.AQSAllUsr.ps1 ./customers.csv 30
I’ve put a download of each scripts right here the code seem like
- ## Get the Mailbox to Entry from the 1st commandline argument
- $csvFileName = $args[0]
- $DaysBack = $args[1]
- ## Load Managed API dll
- Add-Kind -Path “C:Program FilesMicrosoftExchangeWeb Services2.0Microsoft.Exchange.WebServices.dll”
- ## Set Change Model
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Change2010_SP2
- ## Create Change Service Object
- $service = New-Object Microsoft.Change.WebServices.Knowledge.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two choices are availible Option1 to use explict credentials or Possibility 2 use the Default (logged On) credentials
- #Credentials Possibility 1 utilizing UPN for the home windows Account
- $psCred = Get-Credential
- $creds = New-Object System.Internet.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Possibility 2
- #service.UseDefaultCredentials = $true
- ## Select to ignore any SSL Warning points induced by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation atmosphere
- $Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Supplier.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$False
- $Params.ReferencedAssemblies.Add(“System.DLL”) | Out-Null
- $TASource[email protected]‘
- namespace Native.ToolkitExtensions.Internet.CertificatePolicy{
- public class TrustAll : System.Internet.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Internet.ServicePoint sp,
- System.Safety.Cryptography.X509Certificates.X509Certificates cert,
- System.Internet.WebRequest req, int downside) {
- return true;
- }
- }
- }
- ‘@
- $TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an occasion of the TrustAll and connect it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance(“Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll”)
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## finish code from http://poshcode.org/624
- ## Set the URL of the CAS (Consumer Entry Server) to use two choices are availbe to use Autodiscover to discover the CAS URL or Hardcode the CAS to use
- #CAS URL Possibility 1 Autodiscover
- #$service.AutodiscoverUrl($MailboxName,{$true})
- #”Using CAS Server : ” + $Service.url
- #CAS URL Possibility 2 Hardcoded
- #$uri=[system.URI] “https://casservername/ews/exchange.asmx”
- #$service.Url = $uri
- ## Optionally available part for Change Impersonation
- $Script:rptCollection = @()
- operate Course of-Mailbox{
- param (
- $SmtpAddress = “$( throw ‘SMTPAddress is a mandatory Parameter’ )”
- )
- course of{
- Write-Host (“Processing Mailbox : “ + $SmtpAddress)
- # Bind to the Inbox Folder
- $folderid= new-object Microsoft.Change.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$SmtpAddress)
- $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- $Vary = [system.DateTime]::Now.AddDays(-$DaysBack).ToString(“MM/dd/yyyy”) + “..” + [system.DateTime]::Now.AddDays(1).ToString(“MM/dd/yyyy”)
- $AQSString1 = “System.Message.DateReceived:” + $Vary
- $AQSString2 = “(“ + $AQSString1 + “) AND (isread:false)”
- $ivItemView = New-Object Microsoft.Change.WebServices.Knowledge.ItemView(1)
- $allMail = $Inbox.FindItems($AQSString1,$ivItemView)
- $ivItemView = New-Object Microsoft.Change.WebServices.Knowledge.ItemView(1)
- $unread = $Inbox.FindItems($AQSString2,$ivItemView)
- $rptObj = “” | Choose MailboxName,TotalCount,Unread,PercentUnread,PercentGraph
- #Write-Host (“All Mail ” + $allMail.TotalCount)
- #Write-Host (“Unread ” + $unread.TotalCount)
- $rptObj.MailboxName = $SmtpAddress
- $rptObj.TotalCount = $allMail.TotalCount
- $rptObj.Unread = $unread.TotalCount
- $PercentUnread = 0
- if($unread.TotalCount -gt 0){
- $PercentUnread = [Math]::spherical((($unread.TotalCount/$allMail.TotalCount) * 100))
- }
- $rptObj.PercentUnread = $PercentUnread
- #Write-Host (“Percent Unread ” + $PercentUnread)
- $ureadGraph = “”
- for($intval=0;$intval -lt 100;$intval+=4){
- if($PercentUnread -gt $intval){
- $ureadGraph += “▓”
- }
- else{
- $ureadGraph += “░”
- }
- }
- #Write-Host $ureadGraph
- $rptObj.PercentGraph = $ureadGraph
- $rptObj | fl
- $Script:rptCollection +=$rptObj
- }
- }
- Import-Csv -Path $csvFileName | ForEach-Object{
- if($service.url -eq $null){
- $service.AutodiscoverUrl($_.SmtpAddress,{$true})
- “Using CAS Server : “ + $Service.url
- }
- Attempt{
- $service.ImpersonatedUserId = new-object Microsoft.Change.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $_.SmtpAddress)
- Course of-Mailbox -SmtpAddress $_.SmtpAddress
- }
- catch{
- Write-host (“Error processing Mailbox : “ + $_.SmtpAddress + $_.Exception.Message.ToString())
- $Error.Clear()
- }
- }
- $Script:rptCollection | ft
- $Script:rptCollection | Export-Csv -NoTypeInformation -Path c:tempInboxUnreadReport.csv -Encoding UTF8