Office 365 – DirSync delete logs

Last updated on March 27th, 2018 at 09:29 am

Under the hood DirSync generates logs whenever it performs a scheduled task. If you’ve increased the frequency DirSync runs, you may want to run this script to empty out the underlying logs.
The Microsoft Online Services Directory Synchronisation Tool, otherwise known as DirSync is used to synchronise your on premise Active Directory and Exchange organization with Office 365. Under the hood, Forefront Identity Manager is used to manage this process.
Each time the synchronization progress runs – by default every three hours – logs are recorded within Forefront Identity Manager itself and the underlying SQL Server database:

For larger environments, or those where the synchronization schedule has been reduced, the log generation can be more than desired and you may wish to clean things up.
The script below, executed on your DirSync server will allow you to clear down your DirSync logs:

# Get the local DirSync (FIM) server object
$DirSync = Get-WmiObject -Class "MIIS_SERVER" -Namespace "root\MicrosoftIdentityIntegrationServer"
# Clear runs older than $DaysToKeep, by default 2 days
$DirSync.ClearRuns([DateTime]::Today.AddDays(-$DaysToKeep)) | Format-Table ReturnValue

To use the script, save it as a PowerShell file Рfor example Clear-DirSyncRuns.ps1, and execute using parameters as shown below:
.\Clear-DirSyncRuns.ps1 -DaysToKeep 1
Naturally, like any PowerShell script this can be configured as a scheduled task for automatic execution.