forked from rekanized/PSLoggingFunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSLoggingFunctions.psm1
56 lines (54 loc) · 2.06 KB
/
PSLoggingFunctions.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function Write-Log {
param(
[string]$Message,
[string]$Path = ".\Logs\log$(get-date -format "yyyyMMdd").txt",
[ValidateSet("INFO","WARNING","ERROR","DELETE","UPDATE","CREATE","ADD")]
[string]$Type = "INFO",
[Parameter(mandatory)]
[ValidateSet("True","False")]
[string]$Active,
[switch]$LineBreak
)
if ($Active -eq $true) {
$DateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
$LogMessage = "$($DateTime) - USER: $env:USERNAME - $Type - $Message"
if (Test-Path -Path ".\Logs"){
if ($LineBreak){
Add-Content -Path $Path -Value " "
return ""
}
Add-Content -Path $Path -Value $LogMessage
}
else {
Write-Host "Creating new 'Logs' folder within: $($pwd.path)" -ForegroundColor Yellow
New-Item -Path . -Name Logs -ItemType Directory -Confirm:$false | Out-Null
Add-Content -Path $Path -Value $LogMessage
}
}
}
function Invoke-TryCatchLog { #Error, Warning and Logging function (Send any command through the scriptblock and it will bounce correct information to you and your logfile!
param(
[parameter(mandatory)]
$InfoLog,
[parameter(mandatory)]
$ScriptBlock,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile,
[ValidateSet("INFO","WARNING","ERROR","DELETE","UPDATE","CREATE","ADD")]
$LogType = "INFO"
)
try {
Write-Log -Type $LogType -Message "$InfoLog" -Active $LogToFile
$Return = Invoke-Command $ScriptBlock
}
catch {
Write-Log -Type WARNING -Message "You ran into an error when trying to: $InfoLog" -Active $LogToFile
Write-Log -Type ERROR -Message "$($_.Exception.Message) - $($_)" -Active $LogToFile
Write-Host "You ran into an error when trying to: $InfoLog"
Write-Host "$($_.Exception.Message) - $($_)" -ForegroundColor Red
return $null
}
return $Return
}
Export-ModuleMember -Function * -Alias *