-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunction-logging-example.ps1
101 lines (82 loc) · 3.6 KB
/
function-logging-example.ps1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#----------------------------------------------------------[Declarations]----------------------------------------------------------
# function Write-Log
[string] $strLogfilePath = "C:\Temp"
[string] $strLogfileDate = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
[string] $strLogfileNamePrefix = "Log_"
[string] $strLogfileName = $($strLogfileNamePrefix + $strLogfileDate + ".log")
[string] $strLogfile = $strLogfilePath + "\" + $strLogfileName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
function Write-Log {
[CmdletBinding()]
param (
[Parameter(
Mandatory=$true,
Position=0)]
[ValidateNotNullOrEmpty()]
[string] $LogText = "",
[Parameter(
Mandatory=$true,
Position=1)]
[ValidateNotNullOrEmpty()]
[ValidateSet('Info','Success','Warning','Error')]
[string] $LogStatus= "Info",
[Parameter(Mandatory=$false)]
[switch] $Absatz,
[Parameter(Mandatory=$false)]
[switch] $EventLog
)
[string] $strLogdate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
[string] $strTextColor = "White"
[string] $strLogFileAbsatz = ""
[string] $strLogFileHeader = ""
if ( -not (Test-Path $strLogfilePath) ) {
Write-Host "Der angegebene Pfad $strLogfilePath existiert nicht!" -ForegroundColor Red
exit
}
# Add a header to logfile, if the logfile not exist
If ( -not (Test-Path $strLogfile) ) {
$strLogFileHeader = "$("#" * 120)`n"
$strLogFileHeader += "{0,-21} {1,0}" -f "# Skript:", "$($MyInvocation.ScriptName)`n"
$strLogFileHeader += "{0,-21} {1,0}" -f "# Startzeit:", "$(Get-Date -Format "dd.MM.yyyy HH:mm:ss")`n"
$strLogFileHeader += "{0,-21} {1,0}" -f "# Startzeit:", "$(Get-Date -Format "dd.MM.yyyy HH:mm:ss")`n"
$strLogFileHeader += "{0,-21} {1,0}" -f "# Ausführendes Konto:", "$([Security.Principal.WindowsIdentity]::GetCurrent().Name)`n"
$strLogFileHeader += "{0,-21} {1,0}" -f "# Computername:", "$env:COMPUTERNAME`n"
$strLogFileHeader += "$("#" * 120)`n"
Write-Host $strLogFileHeader
Add-Content -Path $strLogfile -Value $strLogFileHeader -Encoding UTF8
}
switch($LogStatus) {
Info {
$strTextColor = "White"
}
Success {
$strTextColor = "Green"
}
Warning {
$strTextColor = "Yellow"
}
Error {
$strTextColor = "Red"
}
}
# Add an Absatz if the parameter is True
if($Absatz) {
[string] $strLogFileAbsatz = "`r`n"
}
#Format the text output
$LogText = "{0,-20} - {1,-7} - {2,0}" -f "$strLogdate", "$LogStatus", "$LogText $strLogFileAbsatz"
# Write output to powershell console
Write-Host $LogText -ForegroundColor $strTextColor
# Write output to logfile
Add-Content -Path $strLogfile -Value $LogText -Encoding UTF8
# Add Logfile to local Eventlog of the operating system
if($EventLog) {
Write-EventLog -LogName 'Windows PowerShell' -Source "Powershell" -EventId 0 -Category 0 -EntryType $LogStatus -Message $LogText
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Write-Log -LogText "Das ist ein Test." -LogStatus Info
Write-Log -LogText "Das ist ein Test." -LogStatus Success
Write-Log -LogText "Das ist ein Test." -LogStatus Warning
Write-Log -LogText "Das ist ein Test." -LogStatus Error -Absatz
Write-Log -LogText "Das ist ein Test." -LogStatus Error -EventLog