Last active
October 29, 2018 05:41
-
-
Save adilio/770ee912231c709558602ee65513264b to your computer and use it in GitHub Desktop.
Simple function to write to a log file
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
<# | |
Write-Log.ps1 | |
20181027 - 4dilio | |
Simple function to write to a log file. | |
You MUST define $Log in your script. | |
#> | |
function Write-Log { | |
[CmdletBinding()] | |
param( | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string]$Message, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[ValidateSet('Information','Warning','Error')] | |
[string]$Severity = 'Information' | |
) | |
$Time = (Get-Date -Format 'MM-dd-yy HH:mm:ss') | |
# Check if $Log is defined and exists; | |
# if not, create it. | |
If ($Log -eq $null) { | |
$Log = "LogOutput.txt" | |
} | |
ElseIf ( -Not (Test-Path -LiteralPath $Log)) { | |
New-Item -ItemType File -Force -Path $Log > $null | |
} | |
# Note: You MUST specify $Log in your script | |
Write-Output "$Time - [$Severity] - $Message" | | |
Out-File $Log -Append | |
# Truncate log file to avoid filling up drive | |
$TruncLog = Get-Content -Tail 1000 $Log | |
$TruncLog | Out-File $Log | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment