Update Write-CMTraceLog.ps1

Applying formatting suggestions from CSPELL and GIT_DIFF linters
pull/597/head
Andrzej Demski 2024-04-01 14:40:37 +02:00 committed by GitHub
parent 77bb54e605
commit 35c4d206ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 25 deletions

View File

@ -1,5 +1,5 @@
# Obtain UTC offset (think about moving it to the parent method)
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime;
$DateTime = New-Object -ComObject 'WbemScripting.SWbemDateTime';
$DateTime.SetVarDate($(Get-Date));
$UtcValue = $DateTime.Value;
$global:CMTraceLog_UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21);
@ -8,7 +8,7 @@
# Set context of process which writes a message
$global:CMTraceLog_Context = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name;
# set string templates for formating later
# set string templates for formatting later
[string]$global:logline_part1_template_nonerror = "<![LOG[{0}: {1}]LOG]!>";
[string]$global:logline_part1_template_error = "<![LOG[{0}: {1}`r`r`nCommand: {2}`nScriptName: {3}`nLine Number: {4}`nColumn Number: {5}`nLine: {6}]LOG]!>";
[string]$global:logline_part2_template = "<time=`"{0}{1}`" date=`"{2}`" component=`"{3}`" context=`"{4}`" type=`"{5}`" thread=`"{6}`" file=`"{7}`">";
@ -88,7 +88,7 @@ Function Write-CMTraceLog
}
else
{
# we do not have an exception, we need to prepare out own cutom error to use later
# we do not have an exception, we need to prepare out own custom error to use later
[System.Exception]$Exception = $Message;
[String]$ErrorID = 'Custom Error';
[System.Management.Automation.ErrorCategory]$ErrorCategory = [Management.Automation.ErrorCategory]::WriteError;
@ -100,7 +100,7 @@ Function Write-CMTraceLog
$Type.ToUpper(),
$Message.exception.message,
$Message.InvocationInfo.MyCommand,
$Message.InvocationInfo.Scriptname,
$Message.InvocationInfo.ScriptName,
$Message.InvocationInfo.ScriptLineNumber,
$Message.InvocationInfo.OffsetInLine,
$Message.InvocationInfo.Line
@ -137,7 +137,8 @@ Function Write-CMTraceLog
#region Warning
2{
#Write back to the host if $Writebacktohost is true.
if(($WriteBackToHost)){
if(($WriteBackToHost))
{
Switch($PSCmdlet.GetVariableValue('WarningPreference')){
'Continue' {$WarningPreference = 'Continue';Write-Warning -Message "$Message";$WarningPreference=''}
'Stop' {$WarningPreference = 'Stop';Write-Warning -Message "$Message";$WarningPreference=''}
@ -151,10 +152,11 @@ Function Write-CMTraceLog
#region Error
3{
#This if statement is to catch the two different types of errors that may come through. A normal terminating exception will have all the information that is needed, if it's a user generated error by using Write-Error,
#then the else statment will setup all the information we would like to log.
#then the else statement will setup all the information we would like to log.
#Write back to the host if $Writebacktohost is true.
if(($WriteBackToHost)){
if(($WriteBackToHost))
{
#Write back to Host
Switch($PSCmdlet.GetVariableValue('ErrorActionPreference'))
{
@ -179,21 +181,20 @@ Function Write-CMTraceLog
'Stop' {$VerbosePreference = 'Stop'; Write-Verbose -Message "$Message";$VerbosePreference = ''}
}
}
}
#endregion Verbose
#region Debug
5{
#Write back to the host if $Writebacktohost is true.
if(($WriteBackToHost)){
if(($WriteBackToHost))
{
Switch ($PSCmdlet.GetVariableValue('DebugPreference')){
'Continue' {$DebugPreference = 'Continue'; Write-Debug -Message "$Message";$DebugPreference = ''}
'Inquire' {$DebugPreference = 'Inquire'; Write-Debug -Message "$Message";$DebugPreference = ''}
'Stop' {$DebugPreference = 'Stop'; Write-Debug -Message "$Message";$DebugPreference = ''}
}
}
}
#endregion Debug
@ -285,7 +286,7 @@ The write-cmtracelog function will then print the verbose message.
Change Log
##########
v1.6 - 2024-04-01 - reorganized com handling triggered by UTC Offset calculation, reuced the paralellism by moving err/non-err strings generation to the front
v1.6 - 2024-04-01 - reorganized com handling triggered by UTC Offset calculation, reduced the parallelism by moving err/non-err strings generation to the front
##########