Last active
May 6, 2025 17:21
-
-
Save dancing-groot/6af20a68e67306762905665db339d30d to your computer and use it in GitHub Desktop.
Function for writing to a log file in plain format, with the option to write the information to the host as well
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
[CmdletBinding()] | |
param() | |
#region FUNCTIONS | |
function New-SimpleLog | |
{ | |
<# | |
.SYNOPSIS | |
Write information to a log file and optionally to the console | |
.LINK | |
https://gist.github.com/dancing-groot/6af20a68e67306762905665db339d30d | |
.NOTES | |
Version: 2025.05.06 | |
Author: @dancing-groot | |
#> | |
[cmdletbinding()] | |
param ( | |
[string]$LogFile = "$($ENV:TEMP)\$((Split-Path -Leaf $script:MyInvocation.MyCommand.Path).Trim('.ps1'))-$(Get-Date -UFormat '%Y.%m.%d')-$(Get-Date -UFormat '%H%M').log", | |
[ValidateSet('Log', 'Console', 'All')] | |
[string]$WriteTo = "Log" | |
) | |
if ($script:PSBoundParameters['Verbose']) { $script:VerbosePreference = 'Continue' } | |
if ($script:PSBoundParameters['Debug']) { $script:DebugPreference = 'Continue' } | |
if ((Test-Path -LiteralPath (Split-Path $LogFile -Parent)) -eq $false) { New-Item -Path (Split-Path $LogFile -Parent) -Type Directory } | |
return @{Path = $LogFile; WriteTo = $WriteTo } | |
} # New-SimpleLog | |
function Write-SimpleLog | |
{ | |
<# | |
.SYNOPSIS | |
Write information to a log file and optionally to the console | |
.LINK | |
https://gist.github.com/dancing-groot/6af20a68e67306762905665db339d30d | |
.NOTES | |
Version: 2025.04.15 | |
Author: @dancing-groot | |
#> | |
[cmdletbinding()] | |
param ( | |
[parameter(Mandatory = $true)] | |
[string]$Path, | |
[parameter(Mandatory = $true)] | |
[string]$Message, | |
[ValidateSet('Warning', 'Error', 'Verbose', 'Debug', 'Information', 'Info')] | |
[string]$Type = "Info", | |
[ValidateSet('Log', 'Console', 'All')] | |
[string]$WriteTo = "Log" | |
) | |
$content = "$(Get-Date -UFormat '%Y.%m.%d %T')" + "`t" + ` | |
$Type + "`t" + ` | |
$Message | |
# Write the line to the log file | |
switch ($true) | |
{ | |
($WriteTo -in @('Console', 'All')) | |
{ | |
switch ($type) | |
{ | |
"Info" { Write-Information $Message -InformationAction Continue; break } | |
"Infomration" { Write-Information $Message -InformationAction Continue; break } | |
"Warning" { Write-Warning $Message -WarningAction Continue; break } | |
"Error" { Write-Host "ERROR: $Message" -ForegroundColor Red; break } | |
"Verbose" { Write-Verbose $Message; break } | |
"Debug" { Write-Debug $Message; break } | |
} | |
} | |
($WriteTo -in @('Log', 'All')) | |
{ | |
switch ($type) | |
{ | |
"Info" { Add-Content -LiteralPath $Path -Value $content; break } | |
"Infomration" { Add-Content -LiteralPath $Path -Value $content; break } | |
"Warning" { Add-Content -LiteralPath $Path -Value $content; break } | |
"Error" { Add-Content -LiteralPath $Path -Value $content; break } | |
"Verbose" { if ($script:PSBoundParameters['Verbose']) { Add-Content -LiteralPath $Path -Value $content; break } } | |
"Debug" { if ($script:PSBoundParameters['Debug']) { Add-Content -LiteralPath $Path -Value $content; break } } | |
} | |
} | |
} | |
} # Write-SimpleLog | |
#endregion FUNCTIONS | |
#region DECLARATION | |
$LogFile = "$PSScriptRoot\MyTestlog.log" | |
$LogParams = New-SimpleLog -LogFile $LogFile -WriteTo All | |
# or use the line below to use the default values (a generated log file name in the user's TEMP folder and write to the log file only) | |
#$LogParams = New-SimpleLog | |
#endregion DECLARATION | |
#region TESTING | |
Write-SimpleLog @LogParams -Message "This is an Information message" | |
Write-SimpleLog @LogParams -Message "This is a Debug message" -Type Debug | |
Write-SimpleLog @LogParams -Message "This is a Verbose message" -Type Verbose | |
Write-SimpleLog @LogParams -Message "This is a Warning message" -Type Warning | |
Write-SimpleLog @LogParams -Message "This is an Error message" -Type Error | |
#region TESTING |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment