Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleanup the Azure Active Directory Connect Sync
function Clear-AADCSyncRuns
{
<#
.SYNOPSIS
Cleanup the Azure Active Directory Connect Sync
.DESCRIPTION
Cleanup the Azure Active Directory Connect Sync
.PARAMETER DaysToKeep
Clear runs older than given value. 0 means only today.
.EXAMPLE
PS C:\> Clear-AADCSyncRuns
# Clear runs older then 2 days
.EXAMPLE
PS C:\> Clear-AADCSyncRuns -DaysToKeep 0
# Clear runs older then today.
.NOTES
Works with DirSync, or newer.
#>
[CmdletBinding()]
[OutputType([string])]
param
(
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 1)]
[ValidateNotNullOrEmpty()]
[Alias('Days')]
[int]
$DaysToKeep = 2
)
Process
{
# Get the local DirSync (FIM) server object
$DirSync = (Get-WmiObject -Class 'MIIS_SERVER' -Namespace 'root\MicrosoftIdentityIntegrationServer')
}
End
{
# Clear runs older than X days
$ResultTMP = ($DirSync.ClearRuns([DateTime]::Today.AddDays(- $DaysToKeep)))
# Transform the result
if ($ResultTMP)
{
[string]$Result = $ResultTMP.ReturnValue
}
else
{
[string]$Result = 'Unknown'
}
# Return
Return $Result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.