Get Fim / Mim History runs and clear them if they older then “n” Days

#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
    }
    #——————————————————————————————————————–

 

Get Fim / MIM MIIS_Runhistory last 9

$GetNamespace = Get-WmiObject -Class “MIIS_RunHistory” -Namespace root\MicrosoftIdentityIntegrationServer | Select-Object -First 9

$MaAgentError = @()
foreach($Namespace in $GetNamespace) {
    if($Namespace.RunStatus -eq "success"){
       $MaAgentOK = "Everything allright"
    }
    else {
       foreach($Namespace in $GetNamespace) {
        $MaAgentError += $Namespace.MaName+" "+$Namespace.RunProfile.ToString()+" "+$Namespace.RunStatus
       }
       $MaAgentError
       break
     }
}
if($MaAgentError.Count -eq 0) {
    $MaAgentOK
}

 

Get Fim / Mim MIIS_RunHistory

$UserForestMAName = "AD Source"
$ResourceForestMAName = "AD Destination"
$ResourceForestMADestination2Name = "AD Destination2"

$GetNamespace = Get-WmiObject -Class “MIIS_RunHistory” -Namespace root\MicrosoftIdentityIntegrationServer

# 00 Destination2 Delta Import 
$RunProfileName00 = $GetNamespace[0].RunProfile.ToString()
$RunStatusInfo00 = $GetNamespace[0].RunStatus
$ResourceForestMATradingName+” $RunProfileName00 “+$RunStatusInfo00

# 01 Destination2 Export 
$RunProfileName01 = $GetNamespace[1].RunProfile.ToString()
$RunStatusInfo01 = $GetNamespace[1].RunStatus
$ResourceForestMATradingName+” $RunProfileName01 “+$RunStatusInfo01

# 02 Destination2 Delta Sync 
$RunProfileName02 = $GetNamespace[2].RunProfile.ToString()
$RunStatusInfo02 = $GetNamespace[2].RunStatus
$ResourceForestMATradingName+” $RunProfileName02 “+$RunStatusInfo02

# 03 Destination Delta Import
$RunProfileName03 = $GetNamespace[3].RunProfile.ToString()
$RunStatusInfo03 = $GetNamespace[3].RunStatus
$ResourceForestMAName+” $RunProfileName03 “+$RunStatusInfo03

# 04 Destination Export
$RunProfileName04 = $GetNamespace[4].RunProfile.ToString()
$RunStatusInfo04 = $GetNamespace[4].RunStatus
$ResourceForestMAName+” $RunProfileName04 “+$RunStatusInfo04

# 05 Destination Delta Sync
$RunProfileName05 = $GetNamespace[5].RunProfile.ToString()
$RunStatusInfo05 = $GetNamespace[5].RunStatus
$ResourceForestMAName+” $RunProfileName05 “+$RunStatusInfo05

# 06 Source Delta Sync
$RunProfileName06 = $GetNamespace[6].RunProfile.ToString()
$RunStatusInfo06 = $GetNamespace[6].RunStatus
$UserForestMAName+” $RunProfileName06 “+$RunStatusInfo06

# 07 source Delta Import
$RunProfileName07 = $GetNamespace[7].RunProfile.ToString()
$RunStatusInfo07 = $GetNamespace[7].RunStatus
$ResourceForestMAName+” $RunProfileName07 “+$RunStatusInfo07

# 08 Source Delta Import
$RunProfileName08 = $GetNamespace[8].RunProfile.ToString()
$RunStatusInfo08 = $GetNamespace[8].RunStatus
$UserForestMAName+” $RunProfileName08 “+$RunStatusInfo08

# some variables and files
$jetzt = get-date -uformat "%Y-%m-%d %H:%M:%S"

# Script will send an email at the end. Needed Parameters
$myServer = "$env:computername.$env:userdnsdomain" # Name of the Maschine
$emailFrom = "$myServer <mail adres of the server>" # Servername und Verteilerliste
$emailBetreff = (get-date -uformat %Y-%m-%d)+" Some Run Fimprofiles no Succes."

$emailTo = "mail@domain.come" # testing

$emailServer = "your mail releay server here" # INFO: server that runs script must be allowed in FW, on mailrelay and in McAfee Virus Console outgoing connection to port 25 must be allowed!

# create email
$emailBody = "`r`n $ResourceForestMADestination2Name $RunProfileName00 $RunStatusInfo00`
                 `r`n $ResourceForestMADestination2Name $RunProfileName01 $RunStatusInfo00`
                 `r`n $ResourceForestMADestination2Name $RunProfileName02 $RunStatusInfo00`
                 `r`n $ResourceForestMAName $RunProfileName03 $RunStatusInfo00`
                 `r`n $ResourceForestMAName $RunProfileName04 $RunStatusInfo00`
                 `r`n $ResourceForestMAName $RunProfileName05 $RunStatusInfo00`
                 `r`n $UserForestMAName $RunProfileName06 $RunStatusInfo00`
                 `r`n $ResourceForestMAName $RunProfileName07 $RunStatusInfo00`
                 `r`n $UserForestMAName $RunProfileName08 $RunStatusInfo00`
                  `r`n"

 
If (`
$RunStatusInfo00 -ne "success"`
 -or $RunStatusInfo01 -ne "success"`
 -or $RunStatusInfo02 -ne "success"`
 -or $RunStatusInfo03 -ne "success"`
 -or $RunStatusInfo04 -ne "success"`
 -or $RunStatusInfo05 -ne "success"`
 -or $RunStatusInfo06 -ne "success"`
 -or $RunStatusInfo07 -ne "success"`
  -or $RunStatusInfo08 -ne "success"`
) {Send-MailMessage -From $emailFrom -To $emailTo -Subject $emailBetreff -SmtpServer $emailServer -Body $emailBody}