Last active
May 8, 2020 04:32
-
-
Save hansgafriedzal/975a85e38466ea7f14a5b4dd8be943c9 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 Log | |
{ | |
param ( | |
$Object, | |
$ForegroundColor, | |
$BackgroundColor, | |
[switch] $NoNewLine | |
) | |
Write-Host @PSBoundParameters | |
$pathLog = "$([Environment]::GetFolderPath("Desktop"))\Log\$($timestamp).log" | |
if (test-path $pathLog) {} else {mkdir $pathLog} | |
$s = "$(Get-Date -format "[yyyy/MM/dd HH:mm:ss]")`t$Object" | |
Add-Content "$pathLog\$(gc env:computername)_$(Get-Date -format "yyyyMMdd").log" -value $s | |
} | |
try | |
{ | |
Log "start!" | |
#==========initialization========== | |
$variables = "var" | |
#================================== | |
Log "initializing..." | |
Log "processing $variables..." | |
Log "done!" | |
throw Exception | |
} | |
catch [Exception] | |
{ | |
$template = "{0} : {1}`r`n{2}`r`n" + | |
" + CategoryInfo : {3}`r`n" + | |
" + FullyQualifiedErrorId : {4}`r`n" | |
$values = $_.InvocationInfo.MyCommand.Name, | |
$_.Exception.Message, | |
$_.InvocationInfo.PositionMessage, | |
$_.CategoryInfo.ToString(), | |
$_.FullyQualifiedErrorId | |
Log ($template -f $values) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment