Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active March 31, 2024 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/4565e42a56bc8f775f7c5d1b12b86142 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/4565e42a56bc8f775f7c5d1b12b86142 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@AfroThundr3007730
Copy link
Author

Updated version available in my HelperFunctions module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment