MIM2016: How to Use PowerShell to Delete the Run History Based on a Specific Date
FIM ScriptBox Item
Applies to
- MIM 2016
- FIM 2010 R2
- FIM 2010
Summary
The script code below clears the run history earlier than or equal to a specific date.
If no parameter is specified, today is used as a specific date.
Optionally, you can specify the number of days from today.
Script Code
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 |
#--------------------------------------------------------------------------------------------------------------------
Clear-Host $DeleteDay = Get-Date If($args.count -gt 0) { $DayDiff = New-Object System.TimeSpan $args[0], 0, 0, 0, 0 $DeleteDay = $DeleteDay.Subtract($DayDiff) }
Write-Host "Deleting run history earlier than or equal to:" $DeleteDay.toString('MM/dd/yyyy') $lstSrv = @(get-wmiobject -class "MIIS_SERVER" -namespace "root\MicrosoftIdentityIntegrationServer" -computer ".") Write-Host "Result: " $lstSrv[0].ClearRuns($DeleteDay.toString('yyyy-MM-dd')).ReturnValue #-------------------------------------------------------------------------------------------------------------------- Trap { Write-Host "`nError: $($_.Exception.Message)`n" -foregroundcolor white -backgroundcolor darkred Exit } #-------------------------------------------------------------------------------------------------------------------- |
Alternatively, if you prefer to work with a PowerShell module, you can add the following to your module:
Module
01.<#
02..SYNOPSIS
03. Clears the FIM Run History
04..DESCRIPTION
05. Clears the FIM Run History
06..EXAMPLE
07. PS> Clear-FIMRunHistory 5
08. Clears the FIM Run History until 5 days ago
09.#>
10.Function Clear-FIMRunHistory {
11.
12. [CmdletBinding()]
13. param
14. (
15. [Parameter(
16. Mandatory=$True,
17. ValueFromPipeline=$true,
18. ValueFromPipelineByPropertyName=$true)]
19. [int]$DaysToKeep
20. )
21.
22. Begin { }
23.
24. Process {
25.
26. $DeleteDay = Get-Date
27. If($DaysToKeep -gt 0) {
28.
29. $DayDiff = New-Object System.TimeSpan $DaysToKeep, 0, 0, 0, 0
30. $DeleteDay = $DeleteDay.Subtract($DayDiff)
31.
32. Write-Output "Deleting run history earlier than or equal to:" $DeleteDay.toString('MM/dd/yyyy')
33. $lstSrv = @(get-wmiobject -class "MIIS_SERVER" -namespace "root\MicrosoftIdentityIntegrationServer" -computer ".")
34. Write-Output "Result: " $lstSrv[0].ClearRuns($DeleteDay.toString('yyyy-MM-dd')).ReturnValue
35.
36. }
37.
38. Trap {
39. Write-Output "`nError: $($_.Exception.Message)`n"
40. Exit
41. }
42.
43. }
44.
45. End { }
46.
47.}
Note
To provide feedback about this script, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the FIM ScriptBox.