I call the function like so.
log -l "Trying to import Active Directory module"
log -l "Unable to import Active Directory module" -type "ERROR"
Here is the function code
Code: Select all
function Log {
#Create a logging function that writes to console, and colour codes according to whether it's an Error or not
Param($l, $type)
$time = get-date -f HH:mm:ss
if ($type -match "error"){
write-host $time -f Gray -NoNewline ; write-host " ERROR: " -f Red -NoNewline ; write-host $l -f DarkYellow
}
elseif ($type -match "warn"){
write-host $time -f Gray -NoNewline ; write-host " WARNING: " -f Magenta -NoNewline ; write-host $l -f Yellow
}
else {
write-host $time -f Gray -NoNewline ; write-host " INFO: " -f Cyan -NoNewline ; write-host $l -f Green
}
}