Trying to set up a PS script that queries O365 Exchange, checks for inbound mail, counts all inbound mail, returns count, and do the same for outbound.
Here's what I have so far, it keeps returning an identical number of inbound vs outbound messages which is 500500.
Not sure how that can be unless it's a preset maximum and I've reached the upper limit.
# initialize session
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
# O365 Inbound/Outbound tracking
$inbound = Get-MailTrafficReport -Direction Inbound | select "MessageCount"
$inbound -replace "[()\s+]"
$i = $inbound
$(for($i=0;$i -le 1000; $i+=1){$i} ) | ForEach-Object -begin {$inbound_sum=0 }-process {$inbound_sum+=$_}
$inbound_sum
# send sum to streaming dataset?
$outbound = Get-MailTrafficReport -Direction Outbound | select "MessageCount"
$outbound -replace "[()\s+]"
$o = $outbound
$(for($o=0;$o -le 1000; $o+=1){$o} ) | ForEach-Object -begin {$outbound_sum=0 }-process {$outbound_sum+=$_}
$outbound_sum
inbound_sum = outbound_sum? What am I missing here?
I tried taking out the $0 -le 1000
in the iterator lines and it just hangs. Despite this I doubt limiting the results to 1000 is summing inbound and outbound to exactly the same number.
500 is the windows administrative account uid, seems like I've hit the administrative limit.
Putting this here as it's too long for a normal comment.
As per the MS docs, AD, Exchange, etc., have default limits set relative to how many records they will return at one time unless you set that to unlimited. See the docs on that topic.
However, why are you not using MS Exchange/O365 built-in message, monitoring, tracing and logging to get this data/statistics?
Monitoring, reporting, and message tracing in Exchange Online
Don't reinvent the wheel (unless it's a learning exercise or you really feel you can do better) if you don't have to when the default solution/service already provides it. Take a look at the scripts below and tweak as needed, if necessary.
Office 365 Mail Traffic Statistics by User
This script connects to Office 365 and retrieves the inbound and outbound mail traffic statistics by recipient for all available dates. This information is then exported to a CSV file which can be opened in Microsoft Excel.
Download: Get-DetailedMessageStats.ps1
Office 365 Exchange Auditing and Reporting - Mailbox Usage, Traffic Reports, etc
Get 160+ O365 Exchange reports on Incoming and Outgoing Mail Traffics, Spam/Malware Emails, Mailbox Forwarding, Mailbox Permissions, Mailbox Auditing, Non-Owner Access, Mailbox Login, Mailbox Size&Usage, Active & Inactive Mailboxes, Distribution Groups with their Membership etc