-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonLibrary.ps1
More file actions
196 lines (161 loc) · 5.31 KB
/
CommonLibrary.ps1
File metadata and controls
196 lines (161 loc) · 5.31 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Common Library and variables.
$version = "1.2.0"
$bootstrapFolder = "$env:temp\system-bootstrap"
$trueValues = @{
'Y' = $true
'YES' = $true
'TRUE' = $true
1 = $true
}
function Set-EnvironmentVariable($name, $value) {
[System.Environment]::SetEnvironmentVariable($name, $value, "User")
[System.Environment]::SetEnvironmentVariable($name, $value, "Process")
}
function Test-ValueIsTrue($Value) {
if ($null -eq $Value) {
return $false
}
if ($trueValues.ContainsKey($($Value).ToUpper())) {
return $true
} else {
return $false
}
}
if ($null -eq $env:SYSTEM_LOGGING_LEVEL) {
Set-EnvironmentVariable 'SYSTEM_LOGGING_LEVEL' "INFO"
}
if ($null -eq $env:SYSTEM_SHOW_LOG_PREAMBLE) {
Set-EnvironmentVariable 'SYSTEM_SHOW_LOG_PREAMBLE' "FALSE"
}
if ($null -eq $env:SYSTEM_SCRIPTS_ROOT) {
Set-EnvironmentVariable 'SYSTEM_SCRIPTS_ROOT' "$($env:USERPROFILE)\Scripts"
}
function Get-LoggingVerbose {
if ($env:SYSTEM_LOGGING_LEVEL -eq 'VERBOSE') {
return $true
} else {
return $false
}
}
# Backup logging commands
function wi($message) {
if (Test-ValueIsTrue $env:SYSTEM_SHOW_LOG_PREAMBLE) {
Write-Host "[$(Get-Date -Format 'yyyyMMdd-hhmmss')][INF] $message"
} else {
Write-Host "$message"
}
}
function wv($message) {
if (Test-ValueIsTrue $env:SYSTEM_SHOW_LOG_PREAMBLE) {
Write-Host "[$(Get-Date -Format 'yyyyMMdd-hhmmss')][VRB] $message"
} else {
Write-Host "[VRB] $message"
}
}
function ww($message) {
if (Test-ValueIsTrue $env:SYSTEM_SHOW_LOG_PREAMBLE) {
Write-Host "[$(Get-Date -Format 'yyyyMMdd-hhmmss')][WRN] $message"
} else {
Write-Host "[WRN] $message"
}
}
function Write-HeadingBlock($Message) {
if (Test-ValueIsTrue $env:SYSTEM_SHOW_LOG_PREAMBLE) {
Write-Host "[$(Get-Date -Format 'yyyyMMdd-hhmmss')][INF] "
Write-Host "[$(Get-Date -Format 'yyyyMMdd-hhmmss')][INF] --- [$Message] ---"
} else {
Write-Host ""
Write-Host "--- [$Message] ---"
}
}
function Write-StepResult {
[CmdletBinding()]
param(
$StepName,
$Status,
[switch] $InitialStatus,
[switch] $FinalStatus
)
$ESCAPE = $([char]27)
$NORMAL = "$ESCAPE[0m"
$WHITE_FOREGROUND = "$ESCAPE[37m"
$GREEN_FOREGROUND = "$ESCAPE[32m"
$RED_BACKGROUND = "$ESCAPE[41m"
if ($StepName.length -gt 30) {
$StepName = $StepName.Substring(0, 30)
}
if ($Status.length -gt 40) {
$Status = $Status.Substring(0, 40)
}
$dots = $("." * (80 - $StepName.length - $Status.length) )
if ($Status -eq 'Failed') {
Write-Host "`r$($StepName)$($dots)$WHITE_FOREGROUND$RED_BACKGROUND$($Status)$NORMAL"
return
}
if ($InitialStatus) {
Write-Host "$($StepName)$($dots)$GREEN_FOREGROUND$($Status)$NORMAL" -NoNewline
return
}
if ($FinalStatus) {
Write-Host "`r$($StepName)$($dots)$GREEN_FOREGROUND$($Status)$NORMAL"
return
}
Write-Host "`r$($StepName)$($dots)$GREEN_FOREGROUND$($Status)$NORMAL" -NoNewline
}
function Start-StepExecution($steps) {
foreach ($step in $steps) {
Write-StepResult -StepName $step.name -Status 'Running' -InitialStatus
if (-not ($step.PSObject.Properties.Name -contains "VerboseOutput")) {
$step | Add-Member -MemberType NoteProperty -Name VerboseOutput -Value ""
}
if (-not ($step.PSObject.Properties.Name -contains "ErrorsOutput")) {
$step | Add-Member -MemberType NoteProperty -Name ErrorsOutput -Value ""
}
if (-not ($step.PSObject.Properties.Name -contains "OutputVariable")) {
$step | Add-Member -MemberType NoteProperty -Name OutputVariable -Value ""
}
$step.OutputVariable = "SYSTEM_STEP_$($step.id.ToUpper())"
$status = Invoke-Command -ScriptBlock $step.script
if ($status[0]) {
$step.passed = $true
} else {
$step.passed = $false
}
$step.details = $status[2]
Set-EnvironmentVariable -name $step.OutputVariable -value $($step.details | ConvertTo-Json -Compress -Depth 10)
Write-StepResult -StepName $step.name -Status $status[1] -FinalStatus
if ($step.passed -eq $false) {
break
}
}
if (Get-LoggingVerbose) {
foreach ($step in $steps) {
$verboseOutput = ""
if ($step.passed -and $null -ne $step.details) {
$detailsOutput = Invoke-Command -ScriptBlock $step.detailsAction -ArgumentList $step.details
$verboseOutput += "$($detailsOutput)`n"
}
if ($step.passed -and $null -eq $step.details) {
$verboseOutput += "$($step.name) - NA`n"
}
$step.VerboseOutput = $verboseOutput
}
}
foreach ($step in $steps) {
$errorsOutput = ""
if (!$step.passed) {
$errorOutput = Invoke-Command -ScriptBlock $step.errorAction -ArgumentList $step.details
$errorsOutput += "$($errorOutput)`n"
}
$step.ErrorsOutput = $errorsOutput
}
$failedSteps = 0
foreach ($step in $steps) {
if (!$step.passed) {
$failedSteps++
}
}
return @{
FailedSteps = $failedSteps
}
}