forked from mmessano/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLoggingTemplate.ps1
154 lines (138 loc) · 4.78 KB
/
LoggingTemplate.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#*******************************************************************
# Global Variables
#*******************************************************************
$Script:Version = '1.0.9.1126'
$Script:LogSeparator = '*******************************************************************************'
$Script:LogFile = ""
#*******************************************************************
# Functions
#*******************************************************************
function Get-ScriptName(){
#
# .SYNOPSIS
# Extracts the script name
# .DESCRIPTION
# Extracts the script file name without extention
# .NOTES
# Author: Axel Kara, [email protected]
#
$tmp = $MyInvocation.ScriptName.Substring($MyInvocation.ScriptName.LastIndexOf('\') + 1)
$tmp.Substring(0,$tmp.Length - 4)
}
function Write-Log($Msg, [System.Boolean]$LogTime=$true){
#
# .SYNOPSIS
# Creates a log entry
# .DESCRIPTION
# By default a time stamp will be logged too. This can be
# disabled with the -LogTime $false parameter
# .NOTES
# Author: Axel Kara, [email protected]
# .EXAMPLE
# Write-Log -Msg 'Log entry created successfull.' [-LogTime $false]
#
if($LogTime){
$date = Get-Date -format dd.MM.yyyy
$time = Get-Date -format HH:mm:ss
Add-Content -Path $LogFile -Value ($date + " " + $time + " " + $Msg)
}
else{
Add-Content -Path $LogFile -Value $Msg
}
}
function Initialize-LogFile($File, [System.Boolean]$reset=$false){
#
# .SYNOPSIS
# Initializes the log file
# .DESCRIPTION
# Creates the log file header
# Creates the folder structure on local drives if necessary
# Resets existing log if used with -reset $true
# .NOTES
# Author: Axel Kara, [email protected]
# .EXAMPLE
# Initialize-LogFile -File 'C:\Logging\events.log' [-reset $true]
#
try{
#Check if file exists
if(Test-Path -Path $File){
#Check if file should be reset
if($reset){
Clear-Content $File -ErrorAction SilentlyContinue
}
}
else{
#Check if file is a local file
if($File.Substring(1,1) -eq ':'){
#Check if drive exists
$driveInfo = [System.IO.DriveInfo]($File)
if($driveInfo.IsReady -eq $false){
Write-Log -Msg ($driveInfo.Name + " not ready.")
}
#Create folder structure if necessary
$Dir = [System.IO.Path]::GetDirectoryName($File)
if(([System.IO.Directory]::Exists($Dir)) -eq $false){
$objDir = [System.IO.Directory]::CreateDirectory($Dir)
Write-Log -Msg ($Dir + " created.")
}
}
}
#Write header
Write-Log -LogTime $false -Msg $LogSeparator
Write-Log -LogTime $false -Msg (((Get-ScriptName).PadRight($LogSeparator.Length - (" Version " + $Version).Length," ")) + " Version " + $Version)
Write-Log -LogTime $false -Msg $LogSeparator
}
catch{
Write-Log -Msg $_
}
}
function Read-Arguments($Values = $args) {
#
# .SYNOPSIS
# Reads named script arguments
# .DESCRIPTION
# Reads named script arguments separated by '=' and tagged with'-' character
# .NOTES
# Author: Axel Kara, [email protected]
#
foreach($value in $Values){
#Change the character that separates the arguments here
$arrTmp = $value.Split("=")
switch ($arrTmp[0].ToLower()) {
-log {
$Script:LogFile = $arrTmp[1]
}
}
}
}
#*******************************************************************
# Main Script
#*******************************************************************
if($args.Count -ne 0){
#Read script arguments
Read-Arguments
if($LogFile.StartsWith("\\")){
Write-Host "UNC"
}
elseif($LogFile.Substring(1,1) -eq ":"){
Write-Host "Local"
}
else{
$LogFile = [System.IO.Path]::Combine((Get-Location), $LogFile)
}
if($LogFile.EndsWith(".log") -eq $false){
$LogFile += ".log"
}
}
if($LogFile -eq ""){
#Set log file
$LogFile = [System.IO.Path]::Combine((Get-Location), (Get-ScriptName) + ".log")
}
#Write log header
Initialize-LogFile -File $LogFile -reset $false
#///////////////////////////////////
#/// Enter your script code here ///
#///////////////////////////////////
#Write log footer
Write-Log -LogTime $false -Msg $LogSeparator
Write-Log -LogTime $false -Msg ''