@@ -93,9 +93,12 @@ function global:Write-Log {
9393 )
9494
9595 begin {
96- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] BoundParameters: $($MyInvocation.BoundParameters | Out-String)" -Tags 'VertigoRay\PSWriteLog','Write-Log'
97- if ($IncludeInvocationHeader ) {
98- Write-InvocationHeader
96+ Microsoft.PowerShell.Utility\Write-Debug (' [Write-Log] BoundParameters: {0}' -f $ ($MyInvocation.BoundParameters | Out-String ))
97+
98+ if ($env: PSWriteLogDisableLogging ) {
99+ # If logging is not currently disabled, get out now!
100+ Microsoft.PowerShell.Utility\Write-Debug (' [Write-Log] env:PSWriteLogDisableLogging: {0}' -f $env: PSWriteLogDisableLogging )
101+ return $null
99102 }
100103
101104 # Get the name of this function
@@ -119,11 +122,11 @@ function global:Write-Log {
119122
120123 [System.Collections.ArrayList ] $legacyMessage = @ ()
121124
122- $legacyMessage.Add (" [ $ ( & $logDate ) $ ( & $logTime ) ] " ) | Out-Null
125+ $legacyMessage.Add (( ' [{0}] ' -f ( Get-Date - Format ' O ' )) ) | Out-Null
123126 if ($Source ) {
124127 $legacyMessage.Add (" [${Source} ]" ) | Out-Null
125128 }
126- $legacyMessage.Add (" [${Component} ]" ) | Out-Null
129+ # $legacyMessage.Add("[${Component}]") | Out-Null
127130 $legacyMessage.Add (" [${Severity} ]" ) | Out-Null
128131 $legacyMessage.Add (($lMessage.Trim () | Out-String )) | Out-Null
129132
@@ -136,7 +139,7 @@ function global:Write-Log {
136139 [string ]
137140 $lMessage
138141 )
139- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Source (sb): ${Source}" -Tags 'VertigoRay\PSWriteLog','Write-Log'
142+ Microsoft.PowerShell.Utility\Write-Debug " [Write-Log] Source (sb): ${Source} "
140143 $severityMap = @ { # Vaguely based on POSH stream numbers
141144 Debug = 5
142145 Error = 3
@@ -162,17 +165,19 @@ function global:Write-Log {
162165 ))
163166 }
164167
165- [scriptblock ] $getLogLine = {
168+ [scriptblock ] $logLine = {
166169 param (
167170 [string ]
168171 $sMsg
169172 )
170173 # # Choose which log type to write to file
171- if ($LogType -ieq ' CMTrace' ) {
172- return & $cmTraceLogString - lMessage ($sMsg | Out-String ).Trim() - lSource $Source
174+ $line = if ($LogType -ieq ' CMTrace' ) {
175+ & $cmTraceLogString - lMessage ($sMsg | Out-String ).Trim() - lSource $Source
173176 } else {
174- return & $legacyLogString - lMessage ($sMsg | Out-String ).Trim() - lSource $Source
177+ & $legacyLogString - lMessage ($sMsg | Out-String ).Trim() - lSource $Source
175178 }
179+
180+ $line | Out-File - FilePath $FilePath.FullName - Append - NoClobber - Force - Encoding ' UTF8' - ErrorAction ' Stop'
176181 }
177182
178183 # Create the directory where the log file will be saved
@@ -182,28 +187,26 @@ function global:Write-Log {
182187 }
183188
184189 process {
185- # Exit function if it is a debug message and 'LogDebugMessage' option is not $true, or if the log directory was not successfully created in 'Begin' block.
186- if (($DebugMessage -and -not $LogDebugMessage )) { Return }
190+ if ($IncludeInvocationHeader ) {
191+ & $logLine - sMsg (" {1}`n {0}`n {1}" -f (Get-InvocationHeader ), (' #' * 40 ))
192+ }
187193
188194 foreach ($msg in $Message ) {
189- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Source: $Source" -Tags 'VertigoRay\PSWriteLog','Write-Log'
190- # Write the log entry to the log file if logging is not currently disabled
191- if (-not $DisableLogging ) {
192- try {
193- & $getLogLine - sMsg $msg | Out-File - FilePath $FilePath.FullName - Append - NoClobber - Force - Encoding ' UTF8' - ErrorAction ' Stop'
194- } catch {
195- if (-not $ContinueOnError ) {
196- throw (' [{0} {1}] [{2}] [{3}] :: Failed to write message [{4}] to the log file [{5}].{6}{7}' -f @ (
197- & $logDate
198- & $logTime
199- $CmdletName
200- $Component
201- $Msg
202- $FilePath.FullName
203- " `n "
204- Resolve-Error | Out-String
205- ))
206- }
195+ Microsoft.PowerShell.Utility\Write-Debug (' [Write-Log] Source: {0}' -f $Source )
196+ try {
197+ & $logLine - sMsg $msg
198+ } catch {
199+ if (-not $ContinueOnError ) {
200+ throw (' [{0} {1}] [{2}] [{3}] :: Failed to write message [{4}] to the log file [{5}].{6}{7}' -f @ (
201+ & $logDate
202+ & $logTime
203+ $CmdletName
204+ $Component
205+ $Msg
206+ $FilePath.FullName
207+ " `n "
208+ Resolve-Error | Out-String
209+ ))
207210 }
208211 }
209212 }
@@ -214,29 +217,29 @@ function global:Write-Log {
214217 if ($MaxLogFileSizeMB ) {
215218 try {
216219 [decimal ] $LogFileSizeMB = $FilePath.Length / 1 MB
217- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] LogFileSizeMB: $LogFileSizeMB / $MaxLogFileSizeMB" -Tags 'VertigoRay\PSWriteLog','Write-Log'
220+ Microsoft.PowerShell.Utility\Write-Debug " [Write-Log] LogFileSizeMB: $LogFileSizeMB / $MaxLogFileSizeMB "
218221 if ($LogFileSizeMB -gt $MaxLogFileSizeMB ) {
219- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Log File Needs to be archived ..." -Tags 'VertigoRay\PSWriteLog','Write-Log'
222+ Microsoft.PowerShell.Utility\Write-Debug " [Write-Log] Log File Needs to be archived ..."
220223 # Change the file extension to "lo_"
221224 [string ] $archivedOutLogFile = [IO.Path ]::ChangeExtension($FilePath.FullName , ' lo_' )
222225
223226 # Log message about archiving the log file
224227 if ((Get-PSCallStack )[1 ].Command -ne ' Write-Log' ) {
225228 # Prevent Write-Log from looping more than once.
226- & $getLogLine - sMsg " Maximum log file size [${MaxLogFileSizeMB} MB] reached. Rename log file to: ${archivedOutLogFile} " | Out-File - FilePath $FilePath .FullName - Append - NoClobber - Force - Encoding ' UTF8 ' - ErrorAction ' Stop '
229+ & $logLine - sMsg " Maximum log file size [${MaxLogFileSizeMB} MB] reached. Rename log file to: ${archivedOutLogFile} "
227230 }
228231
229232 # Archive existing log file from <filename>.log to <filename>.lo_. Overwrites any existing <filename>.lo_ file. This is the same method SCCM uses for log files.
230233 Move-Item - Path $FilePath.FullName - Destination $archivedOutLogFile - Force - ErrorAction ' Stop'
231234
232235 # Start new log file and Log message about archiving the old log file
233- & $getLogLine - sMsg " Maximum log file size [${MaxLogFileSizeMB} MB] reached. Previous log file was renamed to: ${archivedOutLogFile} " | Out-File - FilePath $FilePath .FullName - Append - NoClobber - Force - Encoding ' UTF8 ' - ErrorAction ' Stop '
236+ & $logLine - sMsg " Maximum log file size [${MaxLogFileSizeMB} MB] reached. Previous log file was renamed to: ${archivedOutLogFile} "
234237 } else {
235- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Log File does not need to be archived." -Tags 'VertigoRay\PSWriteLog','Write-Log'
238+ Microsoft.PowerShell.Utility\Write-Debug " [Write-Log] Log File does not need to be archived."
236239 }
237240 } catch {
238241 # If renaming of file fails, script will continue writing to log file even if size goes over the max file size
239- # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] Archive Error: ${_}" -Tags 'VertigoRay\PSWriteLog','Write-Log'
242+ Microsoft.PowerShell.Utility\Write-Debug " [Write-Log] Archive Error: ${_} "
240243 }
241244 }
242245 }
0 commit comments