[Reference] Script for Reading Run History to CSV

[Reference] Script for Reading Run History to CSV

I have received requests on dumping the Run History information, prior to clearing the Run History. In a project that I have been working on, I put together this script to pull the Run History and dump it down to a CSV file. If you are looking to save your run history prior to clearing the run history, this may just work for you.

Please remember that this was designed for a project I was working on. You may need to modify it to fit your business requirements and/or needs.

Run History Script

  • OUTPUT (2 Files):
    • RUNHISTORY_CSV.CSV: Dumps a good portion of the attributes to the CSV file
    • RUNHISTORY_ALL.TXT: All of it entered into the TXT file as XML

POWERSHELL SCRIPT FOR DUMPING RUN HISTORY TO CSV

 

 

$Global:GetRunHistoryColumnName=""

$Global:GetRunHistoryColumnData=""

$Global:GetRunStepNumber=0

 

function buildrunhistorydata( [System.Object]$xmlobject, [string]$columnname, [int]$i )

{

        if($xmlobject -eq $null){

                if( $Global:GetRunHistoryColumnData -eq "" ){ $Global:GetRunHistoryColumnData = $getdata }

                else { $Global:GetRunHistoryColumnData = $Global:GetRunHistoryColumnData+"`t"+$getdata }

            }

            else{

                if( $Global:GetRunHistoryColumnName -eq "" ){ $Global:GetRunHistoryColumnName = $xmlobject[$columnname].Name }

                else {

                    if( -not $Global:GetRunHistoryColumnName.Contains($columnname) ) {

                    if(( -not $xmlobject[$columnname].Name -eq "" ) -or ( -not $xmlobject.Name -eq "" )){

                    $NewColumnName = if( $xmlobject[$columnname].Name -eq $null ){$xmlobject.Name} else{$xmlobject[$columnname].Name}

                    $Global:GetRunHistoryColumnName = $Global:GetRunHistoryColumnName.Trim()+"`t"+$NewColumnName }}}

 

                switch ($i)

                {

                    0 { $getdata = $xmlobject[$columnname].InnerText }

                    1 { $getdata = $xmlobject[$columnname].InnerXML }

                    2 { $getdata = $xmlobject[$columnname].OuterXML }

                    3 { $getdata = $xmlobject[$columnname].type }

                    4 { $getdata = $xmlobject[$columnname].Value }

                    5 { $getdata = $xmlobject.Value }

                }

 

                if( $columnname.ToLower() -eq "call-stack" )

                {

                    $getdata = $getdata.Replace(" ","")

                    $getdata = $getdata.Replace("`r`n","")

                }

 

                if( $Global:GetRunHistoryColumnData -eq "" ){ $Global:GetRunHistoryColumnData = $getdata }

                else { $Global:GetRunHistoryColumnData = $Global:GetRunHistoryColumnData+"`t"+$getdata }

        }

}

 

$OutFileHistory = $env:TEMP+"\RUNHISTORY_CSV.csv"

$OutFileCompleteRunHistory = $env:TEMP+"\RUNHISTORY_ALL.txt"

$RunStartDate=(Get-Date (Get-Date).AddDays(-20) -Format d)

$GetRunStartTime="RunStartTime >'"+$RunStartDate+"'"

$GetRunHistoryNotSuccess = Get-WmiObject -class "MIIS_RunHistory" -namespace root\MicrosoftIdentityintegrationServer -Filter $GetRunStartTime

 

if( $GetRunHistoryNotSuccess -ne $null ){

 

$sRunHistoryString=""

$GetRunHistoryData=""

$RunHistoryNames=""

 

 

foreach( $RHERR in $GetRunHistoryNotSuccess ){

    

    [xml]$gRunHistory = $RHERR.RunDetails().ReturnValue

    $gRunHistoryRunDetails = $gRunHistory.DocumentElement["run-details"]

 

    buildrunhistorydata $gRunHistoryRunDetails "ma-name" 0

    buildrunhistorydata $gRunHistoryRunDetails "ma-id" 0

    buildrunhistorydata $gRunHistoryRunDetails "run-profile-name" 0

    buildrunhistorydata $gRunHistoryRunDetails "security-id" 0

 

        #step-details ( could have multiple steps, so we will need to loop through )

        $GetRunStepDetails= $gRunHistoryRunDetails["step-details"]

        

            #step-details information

            $Global:GetRunStepNumber = $GetRunStepDetails.Attributes.Item(0).Value

            

            for( [int]$iCounter=0; $iCounter -lt $GetRunStepNumber; $iCounter++ ){

 

                if( $iCounter -gt 0 ) { $GetRunStepDetails = $GetRunStepDetails.NextSibling }

                

                #step-details

                buildrunhistorydata $GetRunStepDetails.Attributes.Item(0) $GetRunStepDetails.Attributes.Item(0).Name 5

                buildrunhistorydata $GetRunStepDetails "step-result" 0

                buildrunhistorydata $GetRunStepDetails "start-date" 2

                buildrunhistorydata $GetRunStepDetails "end-date" 2

                    

                #MA Connection Information

                $GetMAConnection=$GetRunStepDetails["ma-connection"]

                buildrunhistorydata $GetMAConnection "connection-result" 0

                buildrunhistorydata $GetMAConnection "server" 0

                    

                #Step-Description

                $GetRunStepDescription= $GetRunStepDetails["step-description"]

                buildrunhistorydata $GetRunStepDescription "partition" 0

                buildrunhistorydata $GetRunStepDescription "step-type" 3

                    

                #Custom Data

                $GetCustomData = $GetRunStepDescription["custom-data"]

                buildrunhistorydata $GetCustomData.FirstChild "batch-size" 0

                buildrunhistorydata $GetCustomData.FirstChild "page-size" 0

                buildrunhistorydata $GetCustomData.FirstChild "time-limit" 0

                    

                #inbound-flow-counters

                buildrunhistorydata $GetRunStepDetails "inbound-flow-counters" 1

                buildrunhistorydata $GetRunStepDetails "export-counters" 1

                    

                #Synchronization Errors

                $GetSynchronizationErrors=$GetRunStepDetails["synchronization-errors"]

                if( -not $GetSynchronizationErrors.IsEmpty ){

                    buildrunhistorydata $GetSynchronizationErrors.FirstChild "error-type" 0

                    buildrunhistorydata $GetSynchronizationErrors.FirstChild "algorithm-step" 0

                    $GetSyncErrorInfo = $GetSynchronizationErrors.FirstChild["extension-error-info"]

                    if( $GetSyncErrorInfo -ne $null ){ buildrunhistorydata $GetSyncErrorInfo "call-stack" 0 }

                     if( -not $RunHistoryNames.Contains("call-stack") ) { $RunHistoryNames = $GetRunHistoryColumnName } }

                    

                if( $GetRunStepNumber -gt 1 ) { $GetRunHistoryColumnData = $GetRunHistoryColumnData+"`n`t`t`t" }

            }

        

        if($sRunHistoryString -eq ""){$sRunHistoryString = $GetRunHistoryColumnData }else{ $sRunHistoryString = $sRunHistoryString+"`r"+$GetRunHistoryColumnData }    

        $GetRunHistoryColumnData=""

        if($RunHistoryNames -eq ""){ $RunHistoryNames = $GetRunHistoryColumnName }else { $RunHistoryNames = $RunHistoryNames }

        $RHERR.RunDetails() | Out-File -Append $OutFileCompleteRunHistory

    }

    

    if($GetRunHistoryData -eq "") { $RunHistoryNames | Out-File -Append $OutFileHistory }

    $GetRunHistoryData = $sRunHistoryString

    $GetRunHistoryData | Out-File -Append $OutFileHistory

    $sRunHistoryString = ""

    $RunHistoryNames = ""

}

 

 

 

Additional Information

  1. How to clear the run history
  2. How to script for clearing the run history
  3. How to use PowerShell to delete the Run History based on a specific Data
Leave a Comment
  • Please add 4 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 3. Comment: Replaced RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 2. Comment: Modified title casing, added tags

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Richard Mueller edited Revision 2. Comment: Modified title casing, added tags

  • Richard Mueller edited Revision 3. Comment: Replaced RGB values with color names in HTML to restore colors

Page 1 of 1 (2 items)