-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFunction-Logging-Extended-Sam-Boutros.ps1
112 lines (95 loc) · 3.95 KB
/
Function-Logging-Extended-Sam-Boutros.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
102
103
104
105
106
107
108
109
110
111
112
$LogFile = "$((Get-Location).Path)\PowerShellScriptExecuted-$(Get-Date -Format 'dd-MMMM-yyyy-hh-mm-ss-tt').txt"
function Write-Log {
<#
.SYNOPSIS
Function to log input string to file and display it to screen
.DESCRIPTION
Function to log input string to file and display it to screen. Log entries in the log file are time stamped. Function allows for displaying text to screen in different colors.
.PARAMETER String
The string to be displayed to the screen and saved to the log file
.PARAMETER Color
The color in which to display the input string on the screen
Default is White
Valid options are
Black
Blue
Cyan
DarkBlue
DarkCyan
DarkGray
DarkGreen
DarkMagenta
DarkRed
DarkYellow
Gray
Green
Magenta
Red
White
Yellow
.PARAMETER LogFile
Path to the file where the input string should be saved.
Example: c:\log.txt
If absent, the input string will be displayed to the screen only and not saved to log file
.EXAMPLE
Write-Log -String "Hello World" -Color Yellow -LogFile c:\log.txt
This example displays the "Hello World" string to the console in yellow, and adds it as a new line to the file c:\log.txt
If c:\log.txt does not exist it will be created.
Log entries in the log file are time stamped. Sample output:
2014.08.06 06:52:17 AM: Hello World
.EXAMPLE
Write-Log "$((Get-Location).Path)" Cyan
This example displays current path in Cyan, and does not log the displayed text to log file.
.EXAMPLE
"$((Get-Process | select -First 1).name) process ID is $((Get-Process | select -First 1).id)" | Write-Log -color DarkYellow
Sample output of this example:
"MDM process ID is 4492" in dark yellow
.EXAMPLE
Write-Log 'Found',(Get-ChildItem -Path .\ -File).Count,'files in folder',(Get-Item .\).FullName Green,Yellow,Green,Cyan .\mylog.txt
Sample output will look like:
Found 520 files in folder D:\Sandbox - and will have the listed foreground colors
.LINK
https://superwidgets.wordpress.com/2014/12/01/powershell-script-function-to-display-text-to-the-console-in-several-colors-and-save-it-to-log-with-timedate-stamp/
.NOTES
Function by Sam Boutros
v1.0 - 08/06/2014
v1.1 - 12/01/2014 - added multi-color display in the same line
v1.2 - 8 August 2016 - updated date time stamp format, protect against bad LogFile name
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Low')]
Param(
[Parameter(Mandatory=$true,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true,
Position=0)]
[String[]]$String,
[Parameter(Mandatory=$false,
Position=1)]
[ValidateSet("Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow")]
[String[]]$Color = "Green",
[Parameter(Mandatory=$false,
Position=2)]
[String]$LogFile,
[Parameter(Mandatory=$false,
Position=3)]
[Switch]$NoNewLine
)
$LegalFileNameCharSet = "^[" + [Regex]::Escape("A-Za-z0-9^&'@{}[],$=!-#()%.+~_") + "]+$"
if ($String.Count -gt 1) {
$i=0
foreach ($item in $String) {
if ($Color[$i]) { $col = $Color[$i] } else { $col = "White" }
Write-Host "$item " -ForegroundColor $col -NoNewline
$i++
}
if (-not ($NoNewLine)) { Write-Host " " }
} else {
if ($NoNewLine) { Write-Host $String -ForegroundColor $Color[0] -NoNewline }
else { Write-Host $String -ForegroundColor $Color[0] }
}
if ($LogFile.Length -gt 2 -and !($LogFile -match $LegalFileNameCharSet)) {
"$(Get-Date -format 'dd MMMM yyyy hh:mm:ss tt'): $($String -join " ")" | Out-File -Filepath $Logfile -Append
} else {
Write-debug "Log: Missing -LogFile parameter or bad LogFile name. Will not save input string to log file.."
}
}