Skip to content

Instantly share code, notes, and snippets.

@hansgafriedzal
Last active May 8, 2020 04:32
Show Gist options
  • Save hansgafriedzal/975a85e38466ea7f14a5b4dd8be943c9 to your computer and use it in GitHub Desktop.
Save hansgafriedzal/975a85e38466ea7f14a5b4dd8be943c9 to your computer and use it in GitHub Desktop.
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