-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-WVDConfigurations.ps1
More file actions
352 lines (303 loc) · 13.5 KB
/
Copy pathSet-WVDConfigurations.ps1
File metadata and controls
352 lines (303 loc) · 13.5 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
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
342
343
344
345
346
347
348
349
350
351
352
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, HelpMessage = "Specify the configuration file name (with or without .json extension)")]
[ValidateNotNullOrEmpty()]
[string]$ConfigurationFile,
[Parameter(Mandatory = $true, HelpMessage = "Specify the configuration folder name")]
[ValidateNotNullOrEmpty()]
[ValidatePattern("^[a-zA-Z0-9_-]+$")]
[string]$ConfigFolderName,
[Parameter(HelpMessage = "Apply all optimizations without prompting")]
[switch]$ApplyAll,
[Parameter(HelpMessage = "Skip all optimizations without prompting")]
[switch]$SkipAll,
[Parameter(HelpMessage = "Create backup of original file before modifications")]
[switch]$CreateBackup
)
<#
.SYNOPSIS
Configures WVD optimization settings for a specific configuration file.
.DESCRIPTION
This function allows interactive configuration of WVD optimization settings
by reading a JSON configuration file and prompting the user to enable or
disable each optimization item. Supports both flat array structures (like
Services.json, AppxPackages.json) and nested structures (like LanManWorkstation.json).
.PARAMETER ConfigurationFile
The name of the configuration file to modify (with or without .json extension).
Valid files: AppxPackages, AutoLoggers, DefaultUserSettings, EdgeSettings,
LanManWorkstation, PolicyRegSettings, ScheduledTasks, Services.
.PARAMETER ConfigFolderName
The name of the configuration folder containing the files to modify.
.PARAMETER ApplyAll
If specified, applies all optimizations without prompting the user.
.PARAMETER SkipAll
If specified, skips all optimizations without prompting the user.
.PARAMETER CreateBackup
If specified, creates a backup of the original file before making changes.
.EXAMPLE
Set-WVDConfigurations -ConfigurationFile "AppxPackages" -ConfigFolderName "Production"
Interactively configure AppxPackages optimizations for the Production folder.
.EXAMPLE
Set-WVDConfigurations -ConfigurationFile "Services.json" -ConfigFolderName "Test1" -ApplyAll
Apply all Services optimizations for the Test1 folder without prompting.
.NOTES
Author: Your Name
Version: 2.0
Requires: PowerShell 5.1 or higher
#>
Function Set-WVDConfigurations
{
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string]$ConfigurationFile,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidatePattern("^[a-zA-Z0-9_-]+$")]
[string]$ConfigFolderName,
[Parameter()]
[switch]$ApplyAll,
[Parameter()]
[switch]$SkipAll,
[Parameter()]
[switch]$CreateBackup
)
begin
{
# Define configuration file property mappings
$ConfigMappings = @{
'AppxPackages' = @{ PropName = 'AppxPackage'; Description = 'Windows App Package'; IsNested = $false }
'AutoLoggers' = @{ PropName = 'KeyName'; Description = 'Auto Logger'; IsNested = $false }
'DefaultUserSettings' = @{ PropName = 'KeyName'; Description = 'User Setting'; IsNested = $false }
'EdgeSettings' = @{ PropName = 'RegItemValueName'; Description = 'Edge Setting'; IsNested = $false }
'LanManWorkstation' = @{ PropName = 'Name'; Description = 'Network Setting'; IsNested = $true; NestedProperty = 'Keys' }
'PolicyRegSettings' = @{ PropName = 'RegItemValueName'; Description = 'Policy Setting'; IsNested = $false }
'ScheduledTasks' = @{ PropName = 'ScheduledTask'; Description = 'Scheduled Task'; IsNested = $false }
'Services' = @{ PropName = 'Name'; Description = 'Windows Service'; IsNested = $false }
}
# Validate conflicting parameters
if ($ApplyAll -and $SkipAll)
{
throw "Cannot specify both -ApplyAll and -SkipAll parameters simultaneously."
}
}
process
{
try
{
# Normalize configuration file name
$ConfigFileBaseName = [System.IO.Path]::GetFileNameWithoutExtension($ConfigurationFile)
if (-not $ConfigurationFile.EndsWith('.json'))
{
$ConfigurationFile = "$ConfigFileBaseName.json"
}
# Validate configuration file type (case-insensitive lookup)
$ConfigMapping = $null
foreach ($Key in $ConfigMappings.Keys)
{
if ($Key -eq $ConfigFileBaseName -or $Key.ToLower() -eq $ConfigFileBaseName.ToLower())
{
$ConfigMapping = $ConfigMappings[$Key]
break
}
}
if (-not $ConfigMapping)
{
$ValidFiles = $ConfigMappings.Keys -join ', '
throw "Invalid configuration file '$ConfigFileBaseName'. Valid files are: $ValidFiles"
}
# Build target file path
$ConfigurationsRoot = Join-Path -Path $PSScriptRoot -ChildPath "Configurations"
$ConfigFolder = Join-Path -Path $ConfigurationsRoot -ChildPath $ConfigFolderName
$TargetFile = Join-Path -Path $ConfigFolder -ChildPath $ConfigurationFile
# Validate paths exist
if (-not (Test-Path -Path $ConfigFolder -PathType Container))
{
throw "Configuration folder not found: $ConfigFolder"
}
if (-not (Test-Path -Path $TargetFile -PathType Leaf))
{
throw "Configuration file not found: $TargetFile"
}
# Create backup if requested
if ($CreateBackup)
{
$BackupFile = "$TargetFile.backup.$(Get-Date -Format 'yyyyMMdd-HHmmss')"
Write-Verbose "Creating backup: $BackupFile"
Copy-Item -Path $TargetFile -Destination $BackupFile -Force
Write-Host "Backup created: $BackupFile" -ForegroundColor Green
}
# Read and parse configuration file
Write-Verbose "Reading configuration file: $TargetFile"
$Content = Get-Content -Path $TargetFile -Raw | ConvertFrom-Json
if (-not $Content -or $Content.Count -eq 0)
{
Write-Warning "Configuration file is empty or invalid: $TargetFile"
return $false
}
$PropName = $ConfigMapping.PropName
$Description = $ConfigMapping.Description
$IsNested = $ConfigMapping.IsNested
$NestedProperty = $ConfigMapping.NestedProperty
$ModifiedCount = 0
# Get the actual configuration items based on structure
if ($IsNested -and $NestedProperty)
{
# Handle nested structure (like LanManWorkstation)
$ConfigItems = @()
foreach ($Section in $Content)
{
if ($Section.$NestedProperty)
{
$ConfigItems += $Section.$NestedProperty
}
}
}
else
{
# Handle flat structure (like Services, AppxPackages, etc.)
$ConfigItems = $Content
}
$TotalCount = $ConfigItems.Count
Write-Host "`nConfiguring $Description optimizations for '$ConfigFolderName'" -ForegroundColor Cyan
Write-Host "Total items: $TotalCount" -ForegroundColor Yellow
if ($TotalCount -eq 0)
{
Write-Warning "No configuration items found in file: $TargetFile"
return $false
}
# Process each configuration item
foreach ($Config in $ConfigItems)
{
$Name = if ($PropName) { $Config.$PropName } else { "Unknown" }
$CurrentState = $Config.OptimizationState
if (-not $Name)
{
Write-Warning "Skipping item with missing name property '$PropName'"
continue
}
$NewState = $CurrentState
if ($ApplyAll)
{
$NewState = 'Apply'
}
elseif ($SkipAll)
{
$NewState = 'Skip'
}
else
{
# Interactive mode
$ItemDescription = if ($Config.Description) { " - $($Config.Description)" } else { "" }
# For nested items, show additional context
$ContextInfo = ""
if ($IsNested)
{
if ($Config.PropertyValue)
{
$ContextInfo = " (Value: $($Config.PropertyValue))"
}
if ($Config.PropertyType)
{
$ContextInfo += " [Type: $($Config.PropertyType)]"
}
}
Write-Host "`n$Description`: " -NoNewline -ForegroundColor White
Write-Host "$Name" -ForegroundColor Yellow
if ($ContextInfo)
{
Write-Host " Details:$ContextInfo" -ForegroundColor Cyan
}
if ($ItemDescription)
{
Write-Host " Description:$ItemDescription" -ForegroundColor Gray
}
Write-Host " Current state: " -NoNewline -ForegroundColor Gray
$StateColor = if ($CurrentState -eq 'Apply') { 'Green' } else { 'Red' }
Write-Host "$CurrentState" -ForegroundColor $StateColor
do
{
$Response = Read-Host " Action? [A]pply, [S]kip, [K]eep current, [Q]uit, [H]elp"
switch ($Response.ToUpper())
{
'A' { $NewState = 'Apply'; break }
'S' { $NewState = 'Skip'; break }
'K' { $NewState = $CurrentState; break }
'Q'
{
Write-Host "Configuration cancelled by user." -ForegroundColor Yellow
return $false
}
'H'
{
Write-Host "`n Available options:" -ForegroundColor Cyan
Write-Host " A - Apply this optimization" -ForegroundColor Green
Write-Host " S - Skip this optimization" -ForegroundColor Red
Write-Host " K - Keep current setting ($CurrentState)" -ForegroundColor Gray
Write-Host " Q - Quit without saving changes" -ForegroundColor Yellow
Write-Host " H - Show this help" -ForegroundColor Cyan
continue
}
default
{
Write-Host " Invalid option. Press 'H' for help." -ForegroundColor Red
continue
}
}
break
} while ($true)
}
# Update the configuration if changed
if ($NewState -ne $CurrentState)
{
$Config.OptimizationState = $NewState
$ModifiedCount++
Write-Verbose "Changed '$Name' from '$CurrentState' to '$NewState'"
}
}
# Save changes if any modifications were made
if ($PSCmdlet.ShouldProcess($TargetFile, "Save configuration changes"))
{
if ($ModifiedCount -gt 0)
{
Write-Verbose "Saving $ModifiedCount changes to: $TargetFile"
$Content | ConvertTo-Json -Depth 10 | Set-Content -Path $TargetFile -Encoding UTF8
Write-Host "`nConfiguration saved successfully!" -ForegroundColor Green
Write-Host "Modified $ModifiedCount out of $TotalCount items." -ForegroundColor Yellow
}
else
{
Write-Host "`nNo changes were made." -ForegroundColor Yellow
}
}
return $true
}
catch
{
Write-Error "Failed to configure WVD settings: $($_.Exception.Message)"
return $false
}
}
}
# Main execution
if (-not $PSBoundParameters.ContainsKey('WhatIf') -and -not $PSBoundParameters.ContainsKey('Confirm'))
{
$params = @{
ConfigurationFile = $ConfigurationFile
ConfigFolderName = $ConfigFolderName
}
if ($ApplyAll) { $params.ApplyAll = $true }
if ($SkipAll) { $params.SkipAll = $true }
if ($CreateBackup) { $params.CreateBackup = $true }
$result = Set-WVDConfigurations @params
if ($result)
{
Write-Host "`nConfiguration completed successfully!" -ForegroundColor Green
}
else
{
Write-Host "`nConfiguration failed or was cancelled." -ForegroundColor Red
exit 1
}
}