This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
CompilerR2.ps1
341 lines (297 loc) · 18.1 KB
/
CompilerR2.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<#
Compiles all the needed powershell files for QA checks into one master script.
#>
Param ([string]$Settings, [switch]$Silent = $false)
Set-StrictMode -Version 2
If ([string]::IsNullOrEmpty($Settings)) { $Settings = 'default-settings.ini' }
[string]$version = ('v3r2.{0}.{1}' -f (Get-Date -Format 'yy'), (Get-Date -Format 'MMdd'))
[string]$date = Get-Date -Format 'yyyy/MM/dd HH:mm'
[string]$path = Split-Path (Get-Variable MyInvocation -ValueOnly).MyCommand.Path
Try { $gh = Get-Host; [int]$ws = $gh.UI.RawUI.WindowSize.Width - 2 } Catch { [int]$ws = 80 }
If ($ws -lt 80) { $ws = 80 }
###################################################################################################
# Required Functions #
###################################################################################################
[string]$F = ([char]9608).ToString()
[string]$T = ([char]9600).ToString()
[string]$B = ([char]9604).ToString()
[string]$M = ([char]9632).ToString()
[string]$L = ([char]9472).ToString()
[string]$TL = ([char]9556).ToString()
[string]$TR = ([char]9559).ToString()
[string]$BL = ([char]9562).ToString()
[string]$V = ([char]9553).ToString()
[string]$H = ([char]9552).ToString()
Function Write-Host2 ([string]$Message, [consolecolor]$ForegroundColor = ($Host.UI.RawUI.ForegroundColor), [switch]$NoNewline = $false)
{
If ($Silent -eq $false) { Write-Host $Message -NoNewline:$NoNewline -ForegroundColor $ForegroundColor }
}
Function Write-Colr
{
Param ([String[]]$Text,[ConsoleColor[]]$Colour,[Switch]$NoNewline=$false)
For ([int]$i = 0; $i -lt $Text.Length; $i++) { Write-Host2 $Text[$i] -Foreground $Colour[$i] -NoNewLine }
If ($NoNewline -eq $false) { Write-Host2 '' }
}
Function Write-Header
{
Param ([string]$Message,[int]$Width); $underline=''.PadLeft($Width-16,$L)
$q=("$TL$H$H$H$H$H$H$H$H$H$H$H$TR ",'','','', "$V $V ",'','','', "$V ","$F$T$F $F$T$F"," $V ",'',
"$V ","$F$B$F $F$T$F"," $V ",'', "$V "," $T "," $V ",'', "$V ",' CHECK '," $V"," $F$F",
"$V ",' '," $V"," $F$F ", "$V ",' ','',"$F$F$B $F$F ", "$BL$H$H$H$H$H$H$H$H",'',''," $T$F$F$T ")
$s=('QA Script Engine','Written by Mike @ My Random Thoughts','support@myrandomthoughts.co.uk','','','',$Message,$version,$underline)
[System.ConsoleColor[]]$c=('White','Gray','Gray','Red','Cyan','Red','Green','Yellow','Yellow');Write-Host2 ''
For ($i=0;$i-lt$q.Length;$i+=4) { Write-Colr ' ',$q[$i],$q[$i+1],$q[$i+2],$q[$i+3],$s[$i/4].PadLeft($Width-19) -Colour Yellow,White,Cyan,White,Green,$c[$i/4] }
Write-Host2 ''
}
Function DivLine { Param ([int]$Width); Return ' '.PadRight($Width, $L) }
Function Load-IniFile
{
Param ([string]$InputFile)
If ($InputFile.ToLower().EndsWith('.ini') -eq $false) { $InputFile += '.ini' }
If ((Test-Path -Path $InputFile) -eq $false)
{
Switch (Split-Path -Path (Split-Path -Path $InputFile -Parent) -Leaf)
{
'i18n' { [string]$errMessage = ' ERROR: Language ' }
'settings' { [string]$errMessage = ' ERROR: Settings ' }
Default { [string]$errMessage = (Split-Path -Path (Split-Path -Path $InputFile -Parent) -Leaf) }
}
Write-Host2 ($errMessage + 'file "{0}" not found.' -f (Split-Path -Path $InputFile -Leaf)) -ForegroundColor Red
Write-Host2 ' '$InputFile -ForegroundColor Red
Write-Host2 ''
Break
}
[string] $comment = ";"
[string] $header = "^\s*(?!$($comment))\s*\[\s*(.*[^\s*])\s*]\s*$"
[string] $item = "^\s*(?!$($comment))\s*([^=]*)\s*=\s*(.*)\s*$"
[hashtable]$ini = @{}
Switch -Regex -File $inputfile {
"$($header)" { $section = ($matches[1] -replace ' ','_'); $ini[$section.Trim()] = @{} }
"$($item)" { $name, $value = $matches[1..2]; If (($name -ne $null) -and ($section -ne $null)) { $ini[$section][$name.Trim()] = $value.Trim() } }
}
Return $ini
}
###################################################################################################
If ($Silent -eq $false) { Clear-Host }
Write-Header -Message 'QA Script Engine Check Compiler' -Width $ws
# Load settings file
Try
{
[hashtable]$iniSettings = (Load-IniFile -InputFile ("$path\settings\$Settings" ))
[hashtable]$lngStrings = (Load-IniFile -InputFile ("$path\i18n\{0}_text.ini" -f ($iniSettings['settings']['language'])))
}
Catch
{
Write-Host2 ' ERROR: There were problems loading the required INI files.' -ForegroundColor Red
Write-Host2 ' Please check the settings file is correct.' -ForegroundColor Red
Write-Host2 ''
Break
}
[string]$shared = "Function newResult { Return ( New-Object -TypeName PSObject -Property @{'server'=''; 'name'=''; 'check'=''; 'datetime'=(Get-Date -Format 'yyyy-MM-dd HH:mm'); 'result'='Unknown'; 'message'=''; 'data'='';} ) }"
[string]$scriptHeader = @"
#Requires -Version 2
<#
QA MASTER SCRIPT
DO NOT EDIT THIS FILE - ALL CHANGES WILL BE LOST
THIS FILE IS AUTO-COMPILED FROM SEVERAL SOURCE FILES
VERSION : $version
COMPILED: $date
#>
"@
$scriptHeader += @'
[CmdletBinding(DefaultParameterSetName = 'HLP')]
Param (
[Parameter(ParameterSetName='QAC', Mandatory=$true, Position=1)][string[]]$ComputerName,
[Parameter(ParameterSetName='QAC', Mandatory=$false )][switch] $SkipHTMLHelp,
[Parameter(ParameterSetName='QAC', Mandatory=$false )][switch] $GenerateCSV,
[Parameter(ParameterSetName='QAC', Mandatory=$false )][switch] $GenerateXML,
[Parameter(ParameterSetName='QAC', Mandatory=$false )][switch] $DoNotPing,
[Parameter(ParameterSetName='HLP', Mandatory=$false )][switch] $Help
)
Set-StrictMode -Version 2
'@
# Get full list of checks...
[object]$qaChecks = Get-ChildItem -Path ($path + '\checks') -Recurse | Where-Object { (-not $_.PSIsContainer) -and ($_.Name).StartsWith('c-') -and ($_.Name).EndsWith('.ps1') | Sort-Object $_.Name }
If ([string]::IsNullOrEmpty($qaChecks) -eq $true)
{
Write-Host2 ' ERROR: No checks found' -ForegroundColor Red
Write-Host2 ' Please make sure you are running this from the correct folder.' -ForegroundColor Red
Write-Host2 ''
Break
}
###################################################################################################
[string]$shortcode = ($iniSettings['settings']['shortcode'] + '_').ToString().Replace(' ', '-')
If ($shortcode -eq '_') { $shortcode = '' }
Write-Host2 ' Removing Previous Check Versions...... ' -NoNewline -ForegroundColor White
[string]$outPath = "$path\QA_$shortcode$version.ps1"
If (Test-Path -Path $outPath) { Try { Remove-Item $outPath -Force } Catch { } }
Write-Host2 'Done' -ForegroundColor Green
###################################################################################################
# CHECKS building #
###################################################################################################
Write-Colr ' Generating New QA Check Script........ ', $qaChecks.Count, ' checks ' -Colour White, Green, White
Write-Colr ' Using Settings File................... ', $Settings.ToUpper() -Colour White, Green
Write-Host2 ' ' -NoNewline; For ($j = 0; $j -lt ($qaChecks.Count + 5); $j++) { Write-Host2 $B -NoNewline -ForegroundColor DarkGray }; Write-Host2 ''
Write-Host2 ' ' -NoNewline
# Start building the QA file
Out-File -FilePath $outPath -InputObject $scriptHeader -Encoding utf8
Out-File -FilePath $outPath -InputObject ('[string] $version = "' + $version + '"') -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ('[string] $settingsFile = "' + $Settings + '"') -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ('[hashtable]$script:lang = @{}' ) -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ('[hashtable]$script:qahelp = @{}' ) -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ('') -Encoding utf8 -Append
# Add the shared variables code
Out-File -FilePath $outPath -InputObject ($shared) -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ('') -Encoding utf8 -Append; Write-Host2 $T -NoNewline -ForegroundColor Cyan
# Get a list of all the checks, adding them into an array
[string]$cList = '[array]$script:qaChecks = ('
[string]$cLine = ''
ForEach ($qa In $qaChecks)
{
[string]$checkName = ($qa.BaseName).Substring(1, 8).Replace('-','')
If (-not $iniSettings["$checkName-skip"])
{
$cCheck = 'c-' + $qa.BaseName.Substring(2); $cLine += "'$cCheck',"
If ($cList.EndsWith('(')) { $space = '' } Else { $space = "`n".PadRight(28) }
If ($cLine.Length -ge 130) { $cList += "$space$cLine"; $cLine='' }
}
}
If ($cLine.Length -gt 10)
{
If ($cList.Substring($cList.Length - 10, 10) -ne $cLine.Substring($cLine.Length - 10, 10))
{
$cList += "$space$cLine"
$cLine=''
}
}
$cList = $cList.Trim(',') + ')'
Out-File -FilePath $outPath -InputObject $cList -Encoding utf8 -Append; Write-Host2 $T -NoNewline -ForegroundColor Cyan
Out-File -FilePath $outPath -InputObject ('') -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject (''.PadLeft(190, '#')) -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ('# QA Check Script Blocks') -Encoding utf8 -Append
[System.Text.StringBuilder]$qaHelp = ''
# Add each check into the script
ForEach ($qa In $qaChecks)
{
Out-File -FilePath $outPath -InputObject "`$c$($qa.Name.Substring(2, 6).Replace('-','')) = {" -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject ($shared) -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject '$script:lang = @{}' -Encoding utf8 -Append
Out-File -FilePath $outPath -InputObject '$script:appSettings = @{}' -Encoding utf8 -Append
[string]$checkName = ($qa.Name).Substring(1, 8).Replace('-','')
If ($iniSettings["$checkName-skip"]) { $checkName += '-skip' }
# Add each checks settings
Try
{
ForEach ($key In ($iniSettings[$checkName].Keys | Sort-Object))
{
[string]$value = $iniSettings[$checkName][$key]
If ($value -eq '') { $value = "''" }
[string]$appSetting = ('$script:appSettings[' + "'{0}'] = {1}" -f $key, $value)
Out-File -FilePath $outPath -InputObject $appSetting -Encoding utf8 -Append
}
}
Catch
{
# Missing INI Section for this check, read from the check script itself
[string]$getContent = ((Get-Content -Path ($qa.FullName) -TotalCount 50) -join "`n")
$regExV = [RegEx]::Match($getContent, "DEFAULT-VALUES:((?:.|\s)+?)(?:(?:[A-Z\- ]+:\n)|(?:#>))")
[string[]]$Values = ($regExV.Groups[1].Value.Trim()).Split("`n")
If (([string]::IsNullOrEmpty($Values) -eq $false) -and ($Values -ne 'None'))
{
ForEach ($EachValue In $Values)
{
[string]$key = ($EachValue -split ' = ')[0].Trim()
[string]$value = ($EachValue -split ' = ')[1].Trim()
If ($value -eq '') { $value = "''" }
[string]$appSetting = ('$script:appSettings[' + "'{0}'] = {1}" -f $key, $value)
Out-File -FilePath $outPath -InputObject $appSetting -Encoding utf8 -Append
}
}
}
# Add language specific strings to each check
Try {
ForEach ($key In ($lngStrings['common'].Keys | Sort-Object))
{
[string]$value = $lngStrings['common'][$key]
If ($value -eq '') { $value = "''" }
[string]$lang = ('$script:lang[' + "'{0}'] = {1}" -f $key, $value)
Out-File -FilePath $outPath -InputObject $lang -Encoding utf8 -Append
}
$checkName = $checkName.TrimEnd('-skip')
ForEach ($key In ($lngStrings[$checkName].Keys | Sort-Object))
{
[string]$value = $lngStrings[$checkName][$key]
If ($value -eq '') { $value = "''" }
[string]$lang = ('$script:lang[' + "'{0}'] = {1}" -f $key, $value)
Out-File -FilePath $outPath -InputObject $lang -Encoding utf8 -Append
}
} Catch { }
# Add the check itself
Out-File -FilePath $outPath -InputObject (Get-Content -Path ($qa.FullName)) -Encoding utf8 -Append
# Generate the help text for from each check (taken from the header information)
# ALSO, add any required additional script functions
[string] $xmlHelp = "<xml>"
[string[]]$keyWords = @('DESCRIPTION', 'REQUIRED-INPUTS', 'PASS', 'WARNING', 'FAIL', 'MANUAL', 'NA', 'APPLIES', 'REQUIRED-FUNCTIONS')
[string] $getContent = ((Get-Content -Path ($qa.FullName)) -join "`n")
ForEach ($keyWord In $KeyWords)
{
# Code from Reddit user "sgtoj"
$regEx = [RegEx]::Match($getContent, "$($keyWord):((?:.|\s)+?)(?:(?:[A-Z\- ]+:)|(?:#>))")
[string[]]$sectionValue = ($regEx.Groups[1].Value.Trim()).Split("`n")
If (([string]::IsNullOrEmpty($sectionValue) -eq $false) -and ($sectionValue -notlike '*None*'))
{
# Add any required additional script functions
If ($keyWord -eq 'REQUIRED-FUNCTIONS') {
ForEach ($function In $sectionValue) {
Out-File -FilePath $outPath -InputObject (Get-Content "$path\engine\$($function.Trim()).ps1") -Encoding utf8 -Append
}
}
Else
{
$keyWord = $keyWord.ToLower()
$xmlHelp += "<$($keyWord.Replace('-', ''))>"
ForEach ($item in $sectionValue) { $xmlHelp += "$($item.Trim())"; If ($keyWord -ne 'DESCRIPTION') { $xmlHelp += "!n" } }
$xmlHelp += "</$($keyWord.Replace('-', ''))>"
}
}
}
$xmlHelp += "</xml>"
$checkName = $checkName.TrimEnd('-skip')
$qaHelp.AppendLine('$script:qahelp[' + "'$checkName']='$xmlHelp'") | Out-Null
# Complete this check
Out-File -FilePath $outPath -InputObject '}' -Encoding utf8 -Append; Write-Host2 $T -NoNewline -ForegroundColor Green
}
Out-File -FilePath $outPath -InputObject (''.PadLeft(190, '#')) -Encoding utf8 -Append
# Write out the EN-GB help file
Out-File -FilePath "$path\i18n\en-gb_help.ps1" -InputObject ($qaHelp.ToString()) -Force -Encoding utf8; Write-Host2 $T -NoNewline -ForegroundColor Cyan
[string]$language = ($iniSettings['settings']['language'])
If (($language -eq '') -or ((Test-Path -Path "$path\i18n\$language.ini") -eq $false)) { $language = 'en-gb' }
Out-File -FilePath $outPath -InputObject (Get-Content ("$path\i18n\$language" + "_help.ps1")) -Encoding utf8 -Append; Write-Host2 $T -NoNewline -ForegroundColor Cyan
Out-File -FilePath $outPath -InputObject (''.PadLeft(190, '#')) -Encoding utf8 -Append
Try
{
ForEach ($key In ($lngStrings['engine'].Keys | Sort-Object))
{
[string]$value = $lngStrings['engine'][$key]
If ($value -eq '') { $value = "''" }
[string]$lang = ('$script:lang[' + "'{0}'] = {1}" -f $key, $value)
Out-File -FilePath $outPath -InputObject $lang -Encoding utf8 -Append
}
}
Catch { }
Out-File -FilePath $outPath -InputObject (''.PadLeft(190, '#')) -Encoding utf8 -Append
[object]$engine = (Get-Content ($path + '\engine\Main-Engine-r2.ps1'))
$engine = $engine.Replace('# COMPILER INSERT', '[string] $reportCompanyName = "' + ($iniSettings['settings']['reportCompanyName']) + '"' + "`n# COMPILER INSERT")
$engine = $engine.Replace('# COMPILER INSERT', '[string] $script:qaOutput = "' + ($iniSettings['settings']['outputLocation'] ) + '"' + "`n# COMPILER INSERT")
$engine = $engine.Replace('# COMPILER INSERT', '[int] $script:ccTasks = ' + ($iniSettings['settings']['concurrent'] ).PadLeft(3) + "`n# COMPILER INSERT")
$engine = $engine.Replace('# COMPILER INSERT', '[int] $script:checkTimeout = ' + ($iniSettings['settings']['timeout'] ).PadLeft(3) + "`n")
Out-File -FilePath $outPath -InputObject $engine -Encoding utf8 -Append; Write-Host2 $T -NoNewline -ForegroundColor Cyan
Write-Host2 ''
###################################################################################################
# FINISH #
###################################################################################################
Write-Host2 (DivLine -Width $ws) -ForegroundColor Yellow
Write-Colr ' Execute ',$(Split-Path -Leaf $outPath),' for command line help' -Colour White, Yellow, White
Remove-Variable version, date, path, outpath -ErrorAction SilentlyContinue
Write-Host2 ''
Write-Host2 ''