This Powershell module provide helpers functions for logging. It can manage a Log file or write to the Event Log.
Init log File.
Initialize-Log [-LogFile] <String> [<CommonParameters>]
Name | Alias | Description | Required? | Pipeline Input | Default Value |
---|---|---|---|---|---|
LogFile | System.String the path to the log file. | true | false | ||
OutOnConsole | System.Boolean Set for the whole module if the Log functions (Write-Log and Event-Log) should be outputted into the Console. | false | false |
BEISPIEL 1
Initialize-Log -LogFile "C:\Logs\Test.log"
Clean up and rotate files
Rotate-Files [-FilesDir] <String> [[-DayOfWeek] <Int32>] [[-DayOfMonth] <Int32>] [[-RotationDaily] <Int32>]
[[-RotationWeekly] <Int32>] [[-RotationMonthly] <Int32>] [<CommonParameters>]
Name | Alias | Description | Required? | Pipeline Input | Default Value |
---|---|---|---|---|---|
FilesDir | true | false | |||
DayOfWeek | Directory log files are written to | false | false | 2 | |
DayOfMonth | The day of the week to store for weekly files (1 to 7 where 1 is Sunday) | false | false | 1 | |
RotationDaily | The day of the month to store for monthly files (Max = 28 since varying last day of month not currently handled) | false | false | 7 | |
RotationWeekly | The number of daily files to keep | false | false | 6 | |
RotationMonthly | The number of weekly files to keep The number of monthly files to keep | false | false | 5 |
BEISPIEL 1
Rotate-Logs -FilesDir "c:\MyFilesDirectory"
Write a new entry in Event Log
Write-EventLog [-Source] <String> [-Message] <String> [[-Level] <String>] [<CommonParameters>]
Name | Alias | Description | Required? | Pipeline Input | Default Value |
---|---|---|---|---|---|
Source | System.String. Required. The Source of the Event. | true | false | ||
Message | System.String. Required. The Message to put into Event log. | true | false | ||
Level | System.String. Optional. The Event Level. Valid Values are "Error", "Warning", "Information", "SuccessAudit" and "FailureAudit". Default value is "Information" | false | false | Information |
BEISPIEL 1
Write-EventLog -Source "ActiveDirectory" -Message "Everything's fine! Let's set back and relax"
Write-EventLog [-LogName] <string> [-Source] <string> [-EventId] <int> [[-EntryType] <EventLogEntryType>] [-Message] <string> [-Category <int16>] [-RawData <byte[]>] [-ComputerName <string>] [<CommonParameters>]
Name | Alias | Description | Required? | Pipeline Input | Default Value |
---|---|---|---|---|---|
Category | Keine | false | false | ||
ComputerName | CN | false | false | ||
EntryType | ET | false | false | ||
EventId | ID, EID | true | false | ||
LogName | LN | true | false | ||
Message | MSG | true | false | ||
RawData | RD | false | false | ||
Source | SRC | true | false |
Write-Log writes a message to a specified log file with the current time stamp.
Name | Alias | Description | Required? | Pipeline Input | Default Value |
---|---|---|---|---|---|
Message | LogContent | System.String The Content to write in Logfile | true | true (ByPropertyName) | |
Level | System.String The Log Level. Valid values are ERROR,WARN and INFO | false | true (ByPropertyName) | Info | |
OutOnConsole | Console | System.Boolean If set to True, the log will also appear in the console. Default set to False | false | true (ByPropertyName) | False |
NoClobber | NoClobber | false | false | False |
- System.Int32
BEISPIEL 1
Write-Log -Message "Log message"
Writes the message to c:\Logs\PowerShellLog.log
BEISPIEL 2
Write-Log -Message "Restarting Server" -Path c:\Logs\Scriptoutput.log
Writes the content to the specified log file and creates the path and file specified.
BEISPIEL 3
Write-Log -Message "Does not exist" -Path c:\Logs\Script.log -Level Error
Writes the message to the specified log file as an error message, and writes the message to the error pipeline.