-
Notifications
You must be signed in to change notification settings - Fork 0
/
Out-LogFile.ps1
101 lines (73 loc) · 2.88 KB
/
Out-LogFile.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
<#
.SYNOPSIS
This function provides the logging functionality of the script.
.DESCRIPTION
Logging
.PARAMETER string
The string to be written to the log file.
.PARAMETER isError
Boolean value to signify exception / log it / terminate script.
.OUTPUTS
Logs all activities and backs up all original data to the log folder directory.
.EXAMPLE
Out-LogFile -string "MESSAGE" -isError BOOLEAN
#>
Function Out-LogFile
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
$String,
[Parameter(Mandatory = $false)]
[boolean]$isError=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$isAudit=$FALSE
)
# Get the current date
[string]$date = Get-Date -Format G
# Build output string
#In this case since I abuse the function to write data to screen and record it in log file
#If the input is not a string type do not time it just throw it to the log.
if ($string.gettype().name -eq "String")
{
[string]$logstring = ( "[" + $date + "] - " + $string)
}
else
{
$logString = $String
}
# Write everything to our log file and the screen
$logstring | Out-File -FilePath $global:LogFile -Append
#Write to the screen the information passed to the log.
if ($string.gettype().name -eq "String")
{
Write-Host $logString
}
else
{
write-host $logString | select-object -expandProperty *
}
#If the output to the log is terminating exception - throw the same string.
if ($isError -eq $TRUE)
{
#Ok - so here's the deal.
#By default error action is continue. IN all my function calls I use STOP for the most part.
#In this case if we hit this error code - one of two things happen.
#If the call is from another function that is not in a do while - the error is logged and we continue with exiting.
#If the call is from a function in a do while - write-error rethrows the exception. The exception is caught by the caller where a retry occurs.
#This is how we end up logging an error then looping back around.
write-error $logString
#Now if we're not in a do while we end up here -> go ahead and create the status file this was not a retryable operation and is a hard failure.
if ($global:ThreadNumber -gt 0)
{
out-statusFile -threadNumber $global:ThreadNumber
}
disable-allPowerShellSessions
if ($isAudit -eq $FALSE)
{
Start-ArchiveFiles -isSuccess:$FALSE -logFolderPath $logFolderPath
}
exit
}
}