Update Write-CMTraceLog.ps1
Applying formatting suggestions from CSPELL and GIT_DIFF linterspull/597/head
parent
77bb54e605
commit
35c4d206ac
|
@ -1,5 +1,5 @@
|
||||||
# Obtain UTC offset (think about moving it to the parent method)
|
# 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));
|
$DateTime.SetVarDate($(Get-Date));
|
||||||
$UtcValue = $DateTime.Value;
|
$UtcValue = $DateTime.Value;
|
||||||
$global:CMTraceLog_UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21);
|
$global:CMTraceLog_UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21);
|
||||||
|
@ -8,7 +8,7 @@
|
||||||
# Set context of process which writes a message
|
# Set context of process which writes a message
|
||||||
$global:CMTraceLog_Context = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name;
|
$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_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_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}`">";
|
[string]$global:logline_part2_template = "<time=`"{0}{1}`" date=`"{2}`" component=`"{3}`" context=`"{4}`" type=`"{5}`" thread=`"{6}`" file=`"{7}`">";
|
||||||
|
@ -31,16 +31,16 @@ Function Write-CMTraceLog
|
||||||
Param(
|
Param(
|
||||||
|
|
||||||
#Path to the log file
|
#Path to the log file
|
||||||
[parameter(Mandatory=$False)]
|
[parameter(Mandatory=$False)]
|
||||||
[String]$Logfile = "$Script:WorkingDir\logs\updates.log",
|
[String]$Logfile = "$Script:WorkingDir\logs\updates.log",
|
||||||
|
|
||||||
#The information to log
|
#The information to log
|
||||||
[parameter(Mandatory=$True)]
|
[parameter(Mandatory=$True)]
|
||||||
$Message,
|
$Message,
|
||||||
|
|
||||||
#The severity (Error, Warning, Verbose, Debug, Information)
|
#The severity (Error, Warning, Verbose, Debug, Information)
|
||||||
[parameter(Mandatory=$True)]
|
[parameter(Mandatory=$True)]
|
||||||
[ValidateSet('Warning','Error','Verbose','Debug', 'Information', IgnoreCase=$True)]
|
[ValidateSet('Warning','Error','Verbose','Debug', 'Information', IgnoreCase=$True)]
|
||||||
[String]$Type,
|
[String]$Type,
|
||||||
|
|
||||||
#Write back to the console or just to the log file. By default it will write back to the host.
|
#Write back to the console or just to the log file. By default it will write back to the host.
|
||||||
|
@ -88,7 +88,7 @@ Function Write-CMTraceLog
|
||||||
}
|
}
|
||||||
else
|
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;
|
[System.Exception]$Exception = $Message;
|
||||||
[String]$ErrorID = 'Custom Error';
|
[String]$ErrorID = 'Custom Error';
|
||||||
[System.Management.Automation.ErrorCategory]$ErrorCategory = [Management.Automation.ErrorCategory]::WriteError;
|
[System.Management.Automation.ErrorCategory]$ErrorCategory = [Management.Automation.ErrorCategory]::WriteError;
|
||||||
|
@ -100,7 +100,7 @@ Function Write-CMTraceLog
|
||||||
$Type.ToUpper(),
|
$Type.ToUpper(),
|
||||||
$Message.exception.message,
|
$Message.exception.message,
|
||||||
$Message.InvocationInfo.MyCommand,
|
$Message.InvocationInfo.MyCommand,
|
||||||
$Message.InvocationInfo.Scriptname,
|
$Message.InvocationInfo.ScriptName,
|
||||||
$Message.InvocationInfo.ScriptLineNumber,
|
$Message.InvocationInfo.ScriptLineNumber,
|
||||||
$Message.InvocationInfo.OffsetInLine,
|
$Message.InvocationInfo.OffsetInLine,
|
||||||
$Message.InvocationInfo.Line
|
$Message.InvocationInfo.Line
|
||||||
|
@ -114,14 +114,14 @@ Function Write-CMTraceLog
|
||||||
|
|
||||||
#region set the 2nd part of logged entry
|
#region set the 2nd part of logged entry
|
||||||
[string]$logline_part2 = [string]::Format(
|
[string]$logline_part2 = [string]::Format(
|
||||||
$global:logline_part2_template,
|
$global:logline_part2_template,
|
||||||
$time,
|
$time,
|
||||||
$global:CMTraceLog_UtcOffset,
|
$global:CMTraceLog_UtcOffset,
|
||||||
$date,
|
$date,
|
||||||
$Component,
|
$Component,
|
||||||
$global:CMTraceLog_Context,
|
$global:CMTraceLog_Context,
|
||||||
$Severity,
|
$Severity,
|
||||||
$ProcessID,
|
$ProcessID,
|
||||||
$Source
|
$Source
|
||||||
);
|
);
|
||||||
#endregion set the 2nd part of logged entry
|
#endregion set the 2nd part of logged entry
|
||||||
|
@ -137,7 +137,8 @@ Function Write-CMTraceLog
|
||||||
#region Warning
|
#region Warning
|
||||||
2{
|
2{
|
||||||
#Write back to the host if $Writebacktohost is true.
|
#Write back to the host if $Writebacktohost is true.
|
||||||
if(($WriteBackToHost)){
|
if(($WriteBackToHost))
|
||||||
|
{
|
||||||
Switch($PSCmdlet.GetVariableValue('WarningPreference')){
|
Switch($PSCmdlet.GetVariableValue('WarningPreference')){
|
||||||
'Continue' {$WarningPreference = 'Continue';Write-Warning -Message "$Message";$WarningPreference=''}
|
'Continue' {$WarningPreference = 'Continue';Write-Warning -Message "$Message";$WarningPreference=''}
|
||||||
'Stop' {$WarningPreference = 'Stop';Write-Warning -Message "$Message";$WarningPreference=''}
|
'Stop' {$WarningPreference = 'Stop';Write-Warning -Message "$Message";$WarningPreference=''}
|
||||||
|
@ -151,10 +152,11 @@ Function Write-CMTraceLog
|
||||||
#region Error
|
#region Error
|
||||||
3{
|
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,
|
#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.
|
#Write back to the host if $Writebacktohost is true.
|
||||||
if(($WriteBackToHost)){
|
if(($WriteBackToHost))
|
||||||
|
{
|
||||||
#Write back to Host
|
#Write back to Host
|
||||||
Switch($PSCmdlet.GetVariableValue('ErrorActionPreference'))
|
Switch($PSCmdlet.GetVariableValue('ErrorActionPreference'))
|
||||||
{
|
{
|
||||||
|
@ -178,22 +180,21 @@ Function Write-CMTraceLog
|
||||||
'Inquire' {$VerbosePreference = 'Inquire'; Write-Verbose -Message "$Message";$VerbosePreference = ''}
|
'Inquire' {$VerbosePreference = 'Inquire'; Write-Verbose -Message "$Message";$VerbosePreference = ''}
|
||||||
'Stop' {$VerbosePreference = 'Stop'; Write-Verbose -Message "$Message";$VerbosePreference = ''}
|
'Stop' {$VerbosePreference = 'Stop'; Write-Verbose -Message "$Message";$VerbosePreference = ''}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion Verbose
|
#endregion Verbose
|
||||||
|
|
||||||
#region Debug
|
#region Debug
|
||||||
5{
|
5{
|
||||||
#Write back to the host if $Writebacktohost is true.
|
#Write back to the host if $Writebacktohost is true.
|
||||||
if(($WriteBackToHost)){
|
if(($WriteBackToHost))
|
||||||
|
{
|
||||||
Switch ($PSCmdlet.GetVariableValue('DebugPreference')){
|
Switch ($PSCmdlet.GetVariableValue('DebugPreference')){
|
||||||
'Continue' {$DebugPreference = 'Continue'; Write-Debug -Message "$Message";$DebugPreference = ''}
|
'Continue' {$DebugPreference = 'Continue'; Write-Debug -Message "$Message";$DebugPreference = ''}
|
||||||
'Inquire' {$DebugPreference = 'Inquire'; Write-Debug -Message "$Message";$DebugPreference = ''}
|
'Inquire' {$DebugPreference = 'Inquire'; Write-Debug -Message "$Message";$DebugPreference = ''}
|
||||||
'Stop' {$DebugPreference = 'Stop'; Write-Debug -Message "$Message";$DebugPreference = ''}
|
'Stop' {$DebugPreference = 'Stop'; Write-Debug -Message "$Message";$DebugPreference = ''}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion Debug
|
#endregion Debug
|
||||||
|
|
||||||
|
@ -285,7 +286,7 @@ The write-cmtracelog function will then print the verbose message.
|
||||||
Change Log
|
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
|
||||||
|
|
||||||
##########
|
##########
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue