Script Repository


Export logging information to CSV

January 04, 2018
1701

The following example exports log records that occurred during the last 30 days to a CSV file.

Note: The script uses the $Context variable available on the server side only. This means that the script can be executed only by Business Rules, Custom Commands, and Scheduled Tasks. For example, to export log records on a regular basis, you can create a Scheduled Task configured for the Domain-DNS object type. To add the script to a Scheduled Task, use the Run a program or PowerShell script action.

Parameters:

  • $numDays - specifies the number of days to get log records for;
  • $csvFilePath - specifies the path to the CSV file that will be created.
Edit Remove
PowerShell
$numDays = 30 # set to 0 to output all records
$csvFilePath = "\\server\share\report.csv"

# Bind to the service log
$path = $Context.GetWellKnownContainerPath("ServiceLog")
$serviceLog = $Context.BindToObject($path)

# Get the General Log
$generalLog = $serviceLog.GeneralLog
if ($numDays -ne 0)
{
   $generalLog.StartDateTime = (Get-Date).AddDays(-$numDays)
   $generalLog.EndDateTime = Get-Date
}

# Get the log records
$log = $generalLog.Log
$records = $log.GetPage(0)

# Save log records to the report
$report = New-Object "System.Collections.ArrayList"
for ($i = 0; $i -lt $records.Count; $i++)
{
    $record = $records.GetObject($i)
    switch ($record.State)
    {
        "OPERATION_STATE_SUSPENDED"
        {
            $state = "Suspended"
        }
        "OPERATION_STATE_COMPLETED"
        {
            $state = "Completed"
        }
        "OPERATION_STATE_FAILED_NO_CONTINUE"
        {
            $state = "Failed"
        }
        "OPERATION_STATE_FAILED_CAN_CONTINUE"
        {
            $state = "Failed"
        }
    }
    $reportRecord = New-Object PSObject -Property @{
        "Start Time" = $record.StartTime
        "Completion Time" = $record.CompletionTime
        "Initiator Name" = $record.Initiator.Name
        "Object Name" = $record.TargetObjectName
        "Object Type" = $record.TargetObjectType
        "State" = $state
        "Description" = $record.Description
    }
    $report.Add($reportRecord)
}

$report | Export-Csv -Path $csvFilePath -NoTypeInformation


Comments ( 0 )
No results found.
Leave a comment