Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper to write PowerShell transcripts to the event log
function Write-ScriptEvent {
<#
.SYNOPSIS
Wrapper to write PowerShell transcripts to the event log.
#>
Param(
# Transcript file to read from
[Parameter(Mandatory = $true)]
[string]$LogFile,
# Event source to apply
[Parameter(Mandatory = $false)]
[string]$Source = 'PSScript Transcript',
# Event ID to apply
[Parameter(Mandatory = $false)]
[int]$EventID = 1000
)
$split = [regex]::Escape("**********************")
$lastRun = ((Get-Content $LogFile -Raw) -split ($split))[-3]
if ($lastRun.Length -gt 32600) {
$lastRun = $lastRun.Substring(0, 32600) + "`n`n****OUTPUT TRUNCATED****"
}
if (!([System.Diagnostics.EventLog]::SourceExists($Source))) {
New-EventLog -LogName 'Application' -Source $Source
}
Write-EventLog -LogName 'Application' -Source $Source `
-EntryType Information -EventId $EventID -Message $lastRun
}
@AfroThundr3007730

This comment has been minimized.

Copy link
Owner Author

@AfroThundr3007730 AfroThundr3007730 commented Feb 5, 2021

This function will extract the content of the last transcript written to a log file, then embed it in a Windows event.

Example usage:

Start-Transcript C:\your\transcript.log
# Your script here
Stop-Transcript
Write-ScriptEvent C:\your\transcript.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment