Public Folder Notifications Tweak

Public Folder Notifications Tweak

I showed you how to use Get-PublicFolderItemStatistics Here is a little Public Folder Notifications Tweak that makes it “set it and forget it”. It requires no intervention after it is set. If you are in charge with doing something like maintaining a separate set of contacts for your organization based on a master list of Public folder contacts. You have to constantly check the public folder for changes…

I guess you could ask the department that maintains the public folder to let you know when there have been changes made or you can set up an automated way using PowerShell and Tasks Scheduler.

Prerequisites On Public Folder Notifications Tweak

Please look at my last post on Prerequisistes.

Create a PowerShell Script To Check On the Public Folder

What this script does is allows you to Use Get-PublicFolderItemStatistics for Notifications that are sent by email to the email of your choosing. It looks for any changes that you set in the variable $baseline. If there is a change, the command Get-PublicFolderItemStatistics is piped into an html body that is formatted correctly for sending mail through the MS Graph API.

The contact listing in the public folder is sorted by modification date so you can tell which record was changed.

The Tweak

The Public Folder Notifications Tweak is the Get-PublicFolderItemStatistics command takes the first record’s modification date and compares it to a date that is written to a text file. If it is new than the date in the text file, it sends the report over email. You can look at the report and see the changes by modification date. It then takes the new date and replaces it in the text file for subsequent runs. If the date is not newer it does nothing.

##Declare Parameters##
$clientID = "Your_Client_ID" 
$clientSecret = "Your_Client_Secret" 
$tenantID = "Your_Tenant_ID"

##Run Script##
Connect-ExchangeOnline

$baseline = Get-Content -Path "<Path To>\baseline.txt"
$m = Get-PublicFolderItemStatistics -Identity "\Path\To\Public\Folder\Contacts" | Select Subject,LastModificationTime | Sort-Object -Property LastModificationTime -Descending

if ($m -ge $baseline) {
		
$k = Get-PublicFolderItemStatistics -Identity "\Path\To\Public\Folder\Contacts"  | Select Subject,LastModificationTime | Sort-Object -Property LastModificationTime -Descending
#Connect to GRAPH API

$MailSender = "sender_email"
#Connect to GRAPH API
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

$h = $k | ConvertTo-Html | select -Skip 4

$HTMLBody = @"
$h
"@

$msg = $HTMLBody

#Send Mail    
$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
                    {
                        "message": {
                          "subject": "Change in Public Folder Contacts.",
                          "body": {
                            "contentType": "HTML",
                            "content": "$msg"

                          },
                          "toRecipients": [
                            {
                              "emailAddress": {
                                "address": "email_you_specify"                              }
                            }
                          ]
                        },
                        "saveToSentItems": "false"
                      }
"@

Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend

		$T = Get-Date
		Set-Content -Path "<Path To>\baseline.txt" -Value $T

}else{
		Write-Host No Change!

Public Folder Notifications Tweak for Notifications Scheduled Task

Please look at my last post on how to create the scheduled task.

What a great Public Folder Notifications Tweak! Now that you have the scheduled task set up, when there is a change in the public folder, you will be notified via email, and you can make the necessary changes on your end. Beats having to remember!

Avatar photo

I am an IT professional with over twenty years experience in the field. I have supported thousands of users over the years. The organizations I have worked for range in size from one person to hundreds of people. I have performed support from Help Desk, Network / Cloud Administration, Network Support, Application Support, Implementation and Security.

Pin It on Pinterest