Log to a file
function Add-LogEntry {
Param(
[string]$Message,
[Logs]$Type
)
enum Logs {
Error
Debug
Info
}
$Log = "$LTSvc\enable_bitlocker.txt"
$timestamp = (Get-Date).ToString("MM/dd/yyyy HH:mm:ss")
$errordetails = (Get-Error).ErrorDetails
$line_number = Get-LineNumber
switch ($Type) {
([Logs]::Debug) {Add-Content $Log "Line: $line_number : $timestamp DEBUG: $message"; break }
([Logs]::Error) {Add-Content $Log "Line: $line_number : $timestamp ERROR: $message ERROR DETAILS: $errordetails"; break }
([Logs]::Info) {Add-Content $Log "Line: $line_number : $timestamp INFO: $message"; break}
(default) {Add-Content $Log "$timestamp []: $message"}
}
}
Log to the console
# Outputs logs to the console
function Out-ConsoleLog {
param(
[Logs]$Type,
[string]$Message,
[string]$Recommendations
)
enum Logs {
Info
Success
Failure
}
$console_logs = @{
Info = "Line: $($MyInvocation.ScriptLineNumber) : $TimeStamp : $message"
Success = "Line: $($MyInvocation.ScriptLineNumber) : $TimeStamp : $message"
Failure = "FAILURE: Script Halted at Line: $($MyInvocation.ScriptLineNumber)"
RecommendedAction = $Recommendations
}
switch ($Type) {
([Logs]::Info) {Write-Host $console_logs.Info ; break}
([Logs]::Success) {Write-Host $console_logs.Success ; break }
([Logs]::Failure) {throw "$($console_logs.Failure) $Recommendations"; break }
}
}
}
# Examples
# Out-ConsoleLog -Type Failure -Message "Test" -Recommendation "Try again"
# Out-ConsoleLog -Type Success -Message "Test"
Get Line Numbers
Call this function in the logging function above
function Get-LineNumber {
$callstack = Get-PSCallStack
return $callstack[$callstack.count -2].Position.StartLineNumber
}