#https://fimfacts.wordpress.com/2013/01/15/export-and-delete-the-fim-sync-run-history-using-powershell/
#https://social.technet.microsoft.com/Forums/en-US/04e9cf9a-74e5-44ac-b634-f6b5a5793de9/save-and-clear-run-history-in-fim-2010-r2?forum=ilm2
#Above the sites i found almost the same script. I changed it for my purpose. I hope you will give the other persons and me the credits if you use this script.
#https://kuipers.eu/get-fim-mim-history-runs-and-clear-them-if-they-older-then-n-days
$exportDirectory ="C:\FIM_Scripts\Run-Exports\"
$dayDiff = "31"
$dateDelete = Get-Date
#if the dayDiff parameter is passed, subtract it from the current day.
If($dayDiff -ne 0)
{
$dateDelete = $dateDelete.AddDays(-$dayDiff)
}
Else
{
$dateDelete = $dateDelete.AddDays(1)
}
# Rewrite the dateDelete so it can be compared.
$dateDelete = $dateDelete.ToString("yyyy-MM-dd")
#if the exportDirectory parameter is passed, save the run history to that directory
If($exportDirectory -ne “”)
{
Write-Host “Exporting run history, older then days definden in varable $daydiff.”
$lstManagementAgents = @(get-wmiobject -class “MIIS_RunHistory” -namespace “root\MicrosoftIdentityIntegrationServer” -computer “.” | Sort-Object RunEndtime -Descending | where RunEndtime -LE $dateDelete)
# $lstManagementAgent = @(get-wmiobject -class “MIIS_RunHistory” -namespace “root\MicrosoftIdentityIntegrationServer” -computer “.” | Sort-Object RunEndtime -Descending | Select-Object -First 4 | Select-Object -Skip 2)
$runDetails = foreach ($lstManagementAgent in $lstManagementAgents)
{
$lstManagementAgent.RunDetails().ReturnValue
}
# I decided to use the $dateDelete instead of $today. Because i want to export only runs till the delete day
$filePathName = $exportDirectory + "Runs-till-$dateDelete.xml”
$runDetails |Out-File $filePathName
Write-Host “Successfully exported run history to: ” $filePathName
}
#finally, delete the run history:
#Define the date again, because of diffrent Date notation.
$dateDelete = Get-Date
$dateDelete = $dateDelete.AddDays(-$dayDiff)
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
#——————————————————————————————————————–
Trap
{
Write-Host “Error: $($_.Exception.Message)” -foregroundcolor yellow -backgroundcolor black
Exit
}
#——————————————————————————————————————–