-
Notifications
You must be signed in to change notification settings - Fork 1
/
Initialize-LogDirectory.ps1
61 lines (53 loc) · 1.53 KB
/
Initialize-LogDirectory.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
function Write-Log {
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet('Information','Warning','Error')]
[string]$Severity = 'Information',
[string]$logFile
)
If (!($PSBoundParameters.LogFile)) {
$logFile = "$env:Temp\LogFile.log"
}
[pscustomobject]@{
Time = (Get-Date -f g)
Message = $Message
Severity = $Severity
} | Export-Csv -Path $logFile -Append -NoTypeInformation
}
function Initialize-LogDirectory {
[CmdletBinding()]
param (
[string]$rootDir = "C:\ITresources",
[string]$logDir = "logs"
)
$fullLogDir = "$rootDir\$logDir"
If (!(Test-Path -LiteralPath $fullLogDir)) {
try {
New-Item -Path $fullLogDir -ItemType Directory -Force -ErrorAction Stop
}
catch {
Write-Log -Severity Error -Message "Couldn't create directory"
Write-Log -Severity Error -Message $_.Exception.Message
exit 1001
}
}
<#
.SYNOPSIS
This script checks and creates a log directory
.PARAMETER rootDir
The root directory
.PARAMETER logDir
The name of the log dir (inside the rootDir)
.EXAMPLE
Initialize-LogDirectory -rootDir "C:\ITresources" -logDir "logs"
.NOTES
Author: Damon Breeden
Github: https://github.com/damonbreeden
#>
}
Initialize-LogDirectory