-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ps1
241 lines (195 loc) · 8.09 KB
/
setup.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
# List of available profile files in the /configurations sub-folder
$configurationsFolderPath = ".\configurations"
$profileFiles = @(Get-ChildItem -Path $configurationsFolderPath -Filter "*.dsc.yaml" | Select-Object -ExpandProperty Name)
$resourceTypeGroupIdentifier = 2
$resourceNameGroupIdentifier = 3
$resourceOutcomeGroupIdentifier = 4
function IsPowershellRunningWithAdminRights
{
$currentUser = New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())
if (!$currentUser.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{
Write-Warning "Your not running Powershell with Administrator rights"
return $false;
}
return $true;
}
function IsWingetVersionCorrect {
param (
[string]$requiredVersion = "1.7.10661"
)
$wingetVersionOutput = winget -v
$currentVersion = $wingetVersionOutput.TrimStart('v')
$currentVersionObject = [version]$currentVersion
$requiredVersionObject = [version]$requiredVersion
if ($currentVersionObject -lt $requiredVersionObject) {
Write-Warning "WinGet is not at the required version or higher. Required: $requiredVersion, Current: $currentVersion"
return $false
}
return $true
}
function Invoke-ProfileProcess {
param (
[string]
$profilePath,
[ValidateSet("Apply", "Test")]
[string]
$mode
)
Clear-Host
$operationVerb = if ($mode -eq "Apply") { "Applying" } else { "Testing" }
$wingetCommand = if ($mode -eq "Apply") { "configure" } else { "configure test" }
Write-Host "$operationVerb profile: " -NoNewline -ForegroundColor Yellow
Write-Host $($profilePath)
$output = Invoke-Expression "winget $wingetCommand -f `"$profilePath`" --accept-configuration-agreements" 2>&1
Write-ConfigurationOutput -consoleOutput $output -mode $mode
Read-Host "Press Enter to continue..."
}
function Show-MainMenu {
Clear-Host
Write-Host "Available Profiles" -ForegroundColor Yellow
for ($i = 0; $i -lt $profileFiles.Count; $i++) {
Write-Host "$($i + 1). $($profileFiles[$i])"
}
Write-Host "0. Exit"
$choice = Read-Host "Enter the number of the profile you want to test/apply (or '0' to exit)"
return [int]$choice
}
function Show-SubMenu {
param (
[string]
$selectedProfile
)
Clear-Host
Write-Host "Selected Profile: " -NoNewline -ForegroundColor Yellow
Write-Host $($selectedProfile)
Write-Host "1. Run Profile"
Write-Host "2. Test Profile"
Write-Host "3. Return to Main Menu"
Write-Host "0. Exit"
$subChoice = Read-Host "Enter your choice (or '0' to exit)"
return [int]$subChoice
}
function CalculateMaxLengths {
param (
[System.Text.RegularExpressions.MatchCollection]
$lineMatches
)
$maxTypeLength = 0
$maxNameLength = 0
foreach ($lineMatch in $lineMatches) {
$typeLength = $lineMatch.Groups[$resourceTypeGroupIdentifier].Value.Length
$nameLength = $lineMatch.Groups[$resourceNameGroupIdentifier].Value.Length
if ($typeLength -gt $maxTypeLength) { $maxTypeLength = $typeLength }
if ($nameLength -gt $maxNameLength) { $maxNameLength = $nameLength }
}
return @{
'MaxTypeLength' = $maxTypeLength;
'MaxNameLength' = $maxNameLength
}
}
function Write-OutputLine {
param (
[string]$type,
[string]$name,
[string]$status,
[int]$maxTypeLength,
[int]$maxNameLength,
[string]$statusColor
)
$formattedType = $type.PadRight($maxTypeLength)
$formattedName = $name.PadRight($maxNameLength)
Write-Host $formattedType -NoNewline -ForegroundColor Yellow
Write-Host " [$formattedName]" -NoNewline -ForegroundColor Blue
Write-Host " $status" -ForegroundColor $statusColor
}
function Write-ConfigurationOutput {
param (
[Parameter(Mandatory = $true)]
[object]
$consoleOutput,
[Parameter(Mandatory = $true)]
[ValidateSet("Test", "Apply")]
[string]
$mode
)
$consoleOutputString = if ($consoleOutput -is [String]) { $consoleOutput } else { $consoleOutput | Out-String }
$pattern = '(Assert|Apply) :: (\w+)(?: \[(.*?)\])?(?:.*\r?\n)+?.*?(System is not in the described configuration state\.|System is in the described configuration state\.|This configuration unit was not run because an assert failed or was false\.|Configuration successfully applied\.|The configuration unit failed while attempting to apply the desired state\.|The configuration unit was not in the module as expected\.)'
$lineMatches = [regex]::Matches($consoleOutputString, $pattern)
# Calculate max lengths for formatting output
$maxLengths = CalculateMaxLengths $lineMatches
$maxTypeLength = $maxLengths.MaxTypeLength
$maxNameLength = $maxLengths.MaxNameLength
$isOverallSuccess = $true
foreach ($match in $lineMatches) {
$type = $match.Groups[$resourceTypeGroupIdentifier].Value
$name = $match.Groups[$resourceNameGroupIdentifier].Value
$outcome = $match.Groups[$resourceOutcomeGroupIdentifier].Value
# Determine status based on the outcome
$status = switch -Regex ($outcome) {
"System is not in the described configuration state\." { "Not in the described state" }
"System is in the described configuration state\." { "In the described state" }
"This configuration unit was not run because an assert failed or was false\." { "Skipped due to failed assert" }
"Configuration successfully applied\." { "Successfully applied" }
"The configuration unit failed while attempting to apply the desired state\." { "Failed to apply" }
"The configuration unit was not in the module as expected\." { "Unknown configuration type" }
default { "Unknown" }
}
$statusColor = switch ($status) {
"Not in the described state" { "Red" }
"Skipped due to failed assert" { "DarkYellow" }
"Failed to apply" { "Red" }
"Unknown configuration type" { "Red" }
default { "Green" }
}
Write-OutputLine `
-type $type `
-name $name `
-status $status `
-maxTypeLength $maxTypeLength `
-maxNameLength $maxNameLength `
-statusColor $statusColor
if ($status -eq "Not in the described state" -or $status -eq "Failed to apply") {
$isOverallSuccess = $false
}
}
Write-Host "----------------------------------------------------------------------"
if ($isOverallSuccess) {
Write-Host "Overall: System is in the described configuration state." -ForegroundColor Green
} else {
Write-Host "Overall: System is not in the described configuration state." -ForegroundColor Red
}
Write-Host "----------------------------------------------------------------------"
}
$isPowershellRunningWithAdminRights = IsPowershellRunningWithAdminRights
if(!$isPowershellRunningWithAdminRights)
{
break
}
$isWingetVersionCorrect = IsWingetVersionCorrect
if(!$isWingetVersionCorrect)
{
break
}
# Main script execution loop
while ($true) {
$choice = Show-MainMenu
if ($choice -eq 0) { break }
if ($choice -ge 1 -and $choice -le $profileFiles.Count) {
$selectedProfile = $profileFiles[$choice - 1]
$profilePath = Join-Path -Path $configurationsFolderPath -ChildPath $selectedProfile
$subChoiceLoop = $true
while ($subChoiceLoop) {
$subChoice = Show-SubMenu -selectedProfile $selectedProfile
switch ($subChoice) {
1 { Invoke-ProfileProcess -profilePath $profilePath -mode "Apply" }
2 { Invoke-ProfileProcess -profilePath $profilePath -mode "Test" }
3 { $subChoiceLoop = $false } # Set flag to break out of the submenu loop
0 { exit }
default { Write-Host "Invalid choice. Please select a valid option." }
}
}
} else {
Write-Host "Invalid choice. Please select a valid option."
}
}