Sulprobil
Search…
PowerShell
Here I present a PowerShell script which I find useful.
Of course, PowerShell is a Microsoft Windows based object orientated script / programming language. It has its own command line interface.
Normally I would prefer a platform independent language like Python. But within a Windows environment you can do almost anything with PowerShell. And sometimes I just do not like to perform all tasks with Excel / VBA. PowerShell can perform tasks in background mode, at a scheduled time, etc.
A first useful function is in my opinion Write-Log. It offers logging functionality.
Please read my Disclaimer
1
<#
2
.SYNOPSIS
3
Write-Log -Message [string] -Severity EVER|INFO|WARN|FATAL
4
5
.DESCRIPTION
6
This script/function enables logging within PowerShell scripts.
7
8
Global variables which control features of this script are:
9
[string]$global:LogFilePath - The text file into which all messages are getting written
10
[string]$global:SubName - The name of the script calling this Log script
11
[int]$global:LogLevel - The log levels:
12
1 logs everything,
13
2 omits INFOs,
14
3 omits INFOs and WARNings,
15
4 only logs severity EVER messages
16
[bool]$global:WriteToHost - $FALSE = Only write into logfile; $TRUE = Also write to Host
17
18
.OUTPUTS
19
Write-Log writes into a logfile with name [string]$global:LogFilePath.
20
if the user has not given this, the standard filename "$PSScriptRoot\Logs\LogFile.txt" is used.
21
The contents of a log file will look like:
22
INFO: BERND-PC\Bernd 2020-02-16 12:19:36 [zip_and_copy.ps1] - Log started
23
INFO: BERND-PC\Bernd 2020-02-16 12:19:36 [zip_and_copy.ps1] - Log finished
24
25
In case the user has set [bool]$global:WriteToHost to $TRUE, WRite-Log also repeats all log
26
messages on screen (Host).
27
28
.PARAMETER Message
29
The message you want to log.
30
31
.PARAMETER Severity
32
The severity of the message. This can be 'INFO', 'WARN', 'FATAL', or 'EVER'.
33
If global variable LogLevel is 1 then messages with all severities will be logged.
34
If LogLevel is 2 then INFO messages will not be logged.
35
If LogLevel is 3 then INFO and WARN messages will not be logged.
36
If LogLevel is 4 then INFO, WARN and FATAL messages will not be logged.
37
38
.EXAMPLE
39
# Initialize global logging variables and make log script known
40
$global:SubName = $MyInvocation.MyCommand.Name
41
$global:LogLevel = 1
42
$global:LogFilePath = "$PSScriptRoot\Logs\LogFile_" + $SubName + "_" +
43
(Get-Date -UFormat "%Y%m%d") + ".txt"
44
$global:WriteToHost = $TRUE
45
. "$PSScriptRoot\Write-Log.ps1"
46
47
.EXAMPLE
48
# How to call
49
Write-Log -Message 'Log started' -Severity INFO
50
If(-Not (Test-path $ArchiveFile)) {Write-Log -Message "$ArchiveFile was not created" -Severity FATAL}
51
52
.NOTES
53
Source: https://berndplumhoff.gitbook.io/sulprobil/powershell
54
Version Date Who What
55
1.0 16-Feb-2020 Bernd Plumhoff Initial Version
56
1.1 16-Jul-2020 Bernd Plumhoff EVER instead of ALWAYS, and optional Write-Host
57
#>
58
59
#Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser -Force
60
#Clear
61
Set-StrictMode -Version Latest
62
63
function Write-Log {
64
[CmdletBinding()]
65
param(
66
[Parameter()]
67
[ValidateNotNullOrEmpty()]
68
[string]$Message,
69
[Parameter()]
70
[ValidateNotNullOrEmpty()]
71
[ValidateSet('INFO','WARN','FATAL','EVER')]
72
[string]$Severity = 'INFO'
73
)
74
switch ($Severity)
75
{
76
'INFO' { If ([int]$global:LogLevel -gt 1) {Exit} }
77
'WARN' { If ([int]$global:LogLevel -gt 2) {Exit} }
78
'FATAL' { If ([int]$global:LogLevel -gt 3) {Exit} }
79
'EVER' { <# Will always be logged! #> }
80
}
81
if (-Not (Test-Path -IsValid $global:LogFilePath)) { $global:LogFilePath = "$PSScriptRoot\Logs\LogFile.txt" }
82
[string]$Output = $Severity + ": " + [System.Security.Principal.WindowsIdentity]::GetCurrent().Name +
83
" " + (Get-Date -UFormat "%Y-%m-%d %T") + " [" + $global:SubName + "] - " + $Message
84
Write-Output $Output | Out-File -append $global:LogFilePath
85
If (Test-Path variable:global:WriteToHost) {
86
If ($global:WriteToHost) { Write-Host $Output }
87
}
88
}
Copied!
Write-Log .txt
4KB
Text
Write-Log .txt (rename to .ps1)
Last modified 10mo ago
Copy link