Export and delete the FIM Sync run history using powershell

I know, this sounds rather old: Searching the web for a powershell script that deletes the run history has plenty of hits. Then again, I haven’t really found a script that does exactly what I want, so I combined a few scripts I found on the web.

The script below has 2 parameters:

  • dayDiff; If this parameter is passed, the number supplied will be subtracted from the current date. The script will only delete history records that are older than the result. If no value is given, all history will be deleted.
  • exportDirectory; If you pass a valid directory here, the script will export the full run history to that directory. Note: the directory should end with ‘/’. If no directory is supplied, the run history will not be exported.

Here’s the script:

PARAM([int]$dayDiff, [string]$exportDirectory)

$dateDelete = Get-Date

#if the dayDiff parameter is passed, subtract it from the current day.
If($dayDiff -ne 0)
$dateDelete = $dateDelete.AddDays(-$dayDiff)
$dateDelete = $dateDelete.AddDays(1)

#if the exportDirectory parameter is passed, save the run history to that directory
If($exportDirectory -ne “”)
Write-Host “Exporting full run history.”

$lstManagementAgent = @(get-wmiobject -class “MIIS_ManagementAgent” -namespace “root\MicrosoftIdentityIntegrationServer” -computer “.”)
$runDetails = $lstManagementAgent[0].RunDetails().ReturnValue

$doc = New-Object System.Xml.XmlDocument
$dateNow = Get-Date -format “yyyyMMddHHmm”

$filePathName = $exportDirectory + $dateNow + “.xml”

Write-Host “Successfully exported run history to: ” $filePathName

#finally, delete the run history:
Write-Host “Deleting run history earlier than:” $dateDelete.toString(‘MM/dd/yyyy’)

$lstSrv = @(get-wmiobject -class “MIIS_SERVER” -namespace “root\MicrosoftIdentityIntegrationServer” -computer “.”)
Write-Host “Result: ” $lstSrv[0].ClearRuns($dateDelete.toString(‘yyyy-MM-dd’)).ReturnValue

Write-Host “Error: $($_.Exception.Message)” -foregroundcolor white -backgroundcolor darkred

Here’s the source for the original script that clears the run history (without exporting it first).

There’s still room for improvement: it might be better if the script would export just the records that it is about to delete, but it’s a great start to come forward to the average wishes of a FIM administrator :)!


This entry was posted in Uncategorized and tagged . Bookmark the permalink.

2 Responses to Export and delete the FIM Sync run history using powershell

  1. David Cassady says:

    Not sure if you’re aware of it yet since this is an old post, but the lines below only return the last run of whatever MA $lstManagementAgent[0] is. Since you connected to the “MIIS_ManagementAgent” class, $lstManagementAgent is an array of your MAs. You need to change the “MIIS_ManagementAgent” class to “MIIS_RunHistory”.

    $lstManagementAgent = @(get-wmiobject -class “MIIS_ManagementAgent” -namespace “root\MicrosoftIdentityIntegrationServer” -computer “.”)
    $runDetails = $lstManagementAgent[0].RunDetails().ReturnValue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s