I'm working with a PS script that has over 100 instances of writing a log for a given step of the script to a log file, and I've now been tasked with getting all those log outputs pushed into Windows Event Logs. The issue is, I don't want to go through by hand and write individual Write-EventLog lines for each write to the log file, I don't know which log writes are most critical to the script, and trying to write the whole log file directly to the Event Log isn't feasible since the log file wildly exceeds the character limit for Windows Event Logs.
With all that said, how would I change the following log function to stop writing to the current log file when it hits a given character limit, and begin writing to a new file with a number added to the original log file's filename?
Example of logging script:
$vScrLogFile = ((Get-Item $PSCommandPath).FullName).Replace((Get-Item $PSCommandPath).Name,"logs\$((Get-Item $PSCommandPath).Basename).$(get-date -Forma开发者_如何学Pythont "MM.dd.yyyy").log.txt")
Function Log_Info {
# Define parameters
param(
[Parameter(Mandatory=$true, Position=0)][string]$vLogInfo,
[Parameter(Mandatory=$true, Position=1)][int]$vLogFlag = 1,
[Parameter(Mandatory=$false, Position=2)][string]$vLogFile,
[Parameter(Mandatory=$false, Position=3)][string]$vConsoleColor
)
# If flag value = 1 then log to screen only.
# If flag value = 2 then log to file only.
# If flag value = 3 then log to file and screen.
If($vLogFlag -eq 1 -OR $vLogFlag -eq 3){
# Log to screen
if ($vConsoleColor -eq $null -or $vConsoleColor -eq "") {$vConsoleColor = "green"}
Write-Host "$vLogInfo" -ForegroundColor $vConsoleColor
#Write-Host "$vLogInfo`n" -ForegroundColor green
}
If($vLogFlag -eq 2 -OR $vLogFlag -eq 3){
# Log to file
if (-not [string]::IsNullOrWhiteSpace($vLogFile)){
Add-Content $vLogFile $vLogInfo
}
}
}
Log_Info -vLogInfo "This is a test log entry." -vLogFlag 3 -vLogFile $vScrLogFile
This is not my script, but I'd like to keep the end result looking close to this for documentation reasons.
I've tried looking for anything similar to this, but might just be wording it poorly since the closest I got was this guy's question, and he wanted just one rotating log file while I need multiple numbered copies once the file hits the character maximum.
精彩评论