Created
April 16, 2018 21:06
-
-
Save HerbM/8e640ccbbaf747c5753fd8fba59f55c8 to your computer and use it in GitHub Desktop.
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 Get-CurrentLineNumber { | |
$Invocation = Get-Variable MyInvocation -value -ea 0 2>$Null | |
If (!$Invocation) { $Invocation = $MyInvocation } | |
$Invocation.ScriptLineNumber | |
} | |
Function Get-CurrentFileLine { | |
if ($MyInvocation.PSCommandPath) { | |
"$(split-path -leaf $MyInvocation.PSCommandPath):$($MyInvocation.ScriptLineNumber)" | |
} else {"GLOBAL:$(LINE)"} | |
} | |
New-Alias -Name LINE -Value Get-CurrentLineNumber -Description 'Returns the current (caller''s) line number in a script.' -force -scope Global | |
New-Alias -Name __LINE__ -Value Get-CurrentLineNumber -Description 'Returns the current (caller''s) line number in a script.' -force -scope Global | |
New-Alias -Name FILE -Value Get-CurrentFileName -Description 'Returns the name of the current script file.' -force -scope Global | |
New-Alias -Name FLINE -Value Get-CurrentFileLine -Description 'Returns the name of the current script file.' -force -scope Global | |
New-Alias -Name __FILE__ -Value Get-CurrentFileName -Description 'Returns the name of the current script file.' -force -scope Global | |
# Typically I alias Write-Log to Write-Verbose WHEN Function Write-Log isn't defined | |
# so remove the alias when we define Write-Log | |
if (Get-Command Write-Log -alias -ea 0) { remove-item Alias:Write-Log -force -ea 0 } | |
Function Write-Log { | |
param ( | |
[string]$Message, | |
[int]$Severity = 3, ## Default to a high severity. Otherwise, override | |
[string]$File = $LogFilePath # default to caller's $LogFilePath if defined | |
) | |
try { | |
if (!$LogLevel) { $LogLevel = 3 } | |
if ($Severity -lt $LogLevel) { return } | |
write-verbose $Message | |
$line = [pscustomobject]@{ | |
'DateTime' = (Get-Date -f "yyyy-MM-dd-ddd-HH:mm:ss") #### (Get-Date) | |
'Severity' = $Severity | |
'Message' = $Message | |
} | |
if (-not $LogFilePath) { | |
$LogFilePath = "$($MyInvocation.ScriptName)" -replace '(\.ps1)?$', '' | |
$LogFilePath += '-Log.txt' | |
} | |
if ($File) { $LogFilePath = $File } | |
if ($psversiontable.psversion.major -lt 3) { | |
$Entry = "`"$($line.DateTime)`", `"$($line.$Severity)`", `"$($line.$Message)`"" | |
$null = Out-file -enc utf8 -filepath $LogFilePath -input $Entry -append -erroraction Silentlycontinue -force | |
} else { | |
$line | Export-Csv -Path $LogFilePath -Append -NoTypeInformation -erroraction Silentlycontinue -force -enc ASCII | |
} | |
} catch { | |
$ec = ('{0:x}' -f $_.Exception.ErrorCode); $em = $_.Exception.Message; $in = $_.InvocationInfo.PositionMessage | |
$description = "$(FLINE) Catch $in $ec, $em" | |
"Logging: $description" >> $LogFilePath | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment