Last active
March 31, 2024 18:55
-
-
Save AfroThundr3007730/4565e42a56bc8f775f7c5d1b12b86142 to your computer and use it in GitHub Desktop.
Wrapper to write PowerShell transcripts to the event log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function will extract the content of the last transcript written to a log file, then embed it in a Windows event.
Example usage: