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:
$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.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.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 :)!