mrhall Posted October 3, 2023 Posted October 3, 2023 Can you share that Powershell script? We've hit that trouble - but I'm told this is impossible to see until it is too late! Maybe we need a different Microsoft licence?
StephenPink Posted October 4, 2023 Posted October 4, 2023 (edited) Similar here. We use Gmail, so I have some Apps Script which regularly checks the number of messages sent from the mailbox during the last 24 hours and posts it into a Sheets file. There's then a chart in that Sheets file which I've embedded into a Sites page for staff to check before sending any emails with ~100+ recipients. [ATTACH=CONFIG]69875[/ATTACH] Oh man that's so much slicker than mine haha, feel like I should've put a little more effort in! Can you share that Powershell script? We've hit that trouble - but I'm told this is impossible to see until it is too late! Maybe we need a different Microsoft licence? Yeah no worries see below, defo not the tidiest or fanciest thing but does the job. Obvs will need to replace appropriate variables: # Script to total current sent items for EduLink mailbox, in the last 24 hours # Requires the Exchange Online Powershell module to be installed first - https://learn.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exchange-online-powershell-module # Import the Exchange Online Powershell module Import-Module ExchangeOnlineManagement # Connect to Exchange Online Powershell Connect-ExchangeOnline -CertificateThumbPrint "THUMBPRINT" -AppID "APPID" -Organization "ORGNAME.onmicrosoft.com" # Get Microsoft Graph access token $AppId = "APPID" $AppSecret = "APPSECRET" $TenantId = "TENANTID" # Construct URI and body needed for authentication $uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" $body = @{ client_id = $AppId scope = "https://graph.microsoft.com/.default" client_secret = $AppSecret grant_type = "client_credentials" } $tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing # Unpack Access Token $token = ($tokenRequest.Content | ConvertFrom-Json).access_token $Headers = @{ 'Content-Type' = "application\json" 'Authorization' = "Bearer $Token" } # Gets the current date/time and works out 24 hours previous $Now = Get-Date $24HoursAgo = (Get-Date $Now).AddHours(-24) Write-Host "PERFORMING MESSAGE TRACE..." -ForegroundColor Cyan $MessageList = @() # Full contents of the trace will go here $Page = 1 $PageSize = 5000 # Max pagesize is 5000. do { Write-Output "Getting page $Page of messages..." $MessagesThisPage = Get-MessageTrace -SenderAddress SENDERADDRESS -StartDate $24HoursAgo -EndDate $Now -PageSize $PageSize -Page $Page Write-Output "There were $($MessagesThisPage.Count) messages on page $Page..." $MessageList += $MessagesThisPage $Page++ } until ($MessagesThisPage.count -lt $PageSize) [int]$MessageCount = $MessageList.Count Write-Output "Message trace returned $MessageCount messages total." [int]$MaximumSend = 10000 Write-Host "SENDING EMAIL REPORT..." -ForegroundColor Cyan # Define some variables for the message #HTML header with styles $HtmlHead=" <br /> BODY{font-family: Tahoma; font-size: 10pt;}<br /> H1{font-size: 22px;}<br /> H2{font-size: 18px; padding-top: 10px;}<br /> H3{font-size: 16px; padding-top: 8px;}<br /> " # Mail.Send can send from any user. Check the application access policy restrictions to make sure this user is included. $MsgFrom = "SENDFROMADDRESS" # Recipients and subject $EmailRecipient = "RECIPIENTADDRESS" $ccRecipient1 = "RECIPIENTADDRESS" $ccRecipient2 = "RECIPIENTADDRESS" $ccRecipient3 = "RECIPIENTADDRESS" $MsgSubject = "SUBJECT" # Content $htmlheader = "eduLink One - Total number of messages sent in the previous 24-hours" $htmlline1 = " Between $(Get-Date $24HoursAgo -Format "dd MMMM yyyy HH:mm:ss") and $(Get-Date $Now -Format "dd MMMM yyyy HH:mm:ss") the eduLink One mailbox has sent a total of $MessageCount messages." $htmlline2 = " This is approximately $(($MessageCount/$MaximumSend).ToString("P")) of the maximum 10,000 messages per 24-hour period." $HtmlBody = "" + $htmlheader + $htmlline1 + $htmlline2 + " " $HtmlMsg = $HtmlHead + $HtmlBody + "" # Create message body and properties and send $MessageParams = @{ "URI" = "https://graph.microsoft.com/v1.0/users/$MsgFrom/sendMail" "Headers" = $Headers "Method" = "POST" "ContentType" = 'application/json' "Body" = (@{ "message" = @{ "subject" = $MsgSubject "body" = @{ "contentType" = 'HTML' "content" = $htmlMsg } "toRecipients" = @( @{ "emailAddress" = @{"address" = $EmailRecipient } } ) "ccRecipients" = @( @{ "emailAddress" = @{"address" = $ccRecipient1 } } , @{ "emailAddress" = @{"address" = $ccRecipient2 } } , @{ "emailAddress" = @{"address" = $ccRecipient3 } } ) } }) | ConvertTo-JSON -Depth 6 } # Send the message Invoke-RestMethod @[u][url="https://www.edugeek.net/member.php?u=1461"]Messa[/url][/u]geparams # Disconnect any Exchange Online Powershell sessions Disconnect-ExchangeOnline -Confirm:$false Write-Host "DONE!" -ForegroundColor Green Hopefully it roughly makes sense and is useful. We are now on A3 but I'm sure it was setup/working whilst still on A1 Plus. Cheers, Stephen Edited October 4, 2023 by StephenPink
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now