Projects/OSDCloudGUI/MainWindow.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# PoSHPF - Version 1.2
# Grab all resources (MahApps, etc), all XAML files, and any potential static resources
$Global:resources = Get-ChildItem -Path "$PSScriptRoot\Resources\*.dll" -ErrorAction SilentlyContinue
$Global:XAML = Get-ChildItem -Path "$PSScriptRoot\*.xaml" | Where-Object {$_.Name -ne 'App.xaml'} -ErrorAction SilentlyContinue #Changed path and exclude App.xaml
$Global:MediaResources = Get-ChildItem -Path "$PSScriptRoot\Media" -ErrorAction SilentlyContinue

# This class allows the synchronized hashtable to be available across threads,
# but also passes a couple of methods along with it to do GUI things via the
# object's dispatcher.
class SyncClass 
{
    #Hashtable containing all forms/windows and controls - automatically created when newing up
    [hashtable]$SyncHash = [hashtable]::Synchronized(@{}) 
    
    # method to close the window - pass window name
    [void]CloseWindow($windowName){ 
        $this.SyncHash.$windowName.Dispatcher.Invoke([action]{$this.SyncHash.$windowName.Close()},"Normal") 
    }
    
    # method to update GUI - pass object name, property and value
    [void]UpdateElement($object,$property,$value){ 
        $this.SyncHash.$object.Dispatcher.Invoke([action]{ $this.SyncHash.$object.$property = $value },"Normal") 
    } 
}
$Global:SyncClass = [SyncClass]::new() # create a new instance of this SyncClass to use.

###################
## Import Resources
###################
# Load WPF Assembly
Add-Type -assemblyName PresentationFramework

# Load Resources
foreach($dll in $resources) { [System.Reflection.Assembly]::LoadFrom("$($dll.FullName)") | out-null }

##############
## Import XAML
##############
$xp = '[^a-zA-Z_0-9]' # All characters that are not a-Z, 0-9, or _
$vx = @()             # An array of XAML files loaded

foreach($x in $XAML) { 
    # Items from XAML that are known to cause issues
    # when PowerShell parses them.
    $xamlToRemove = @(
        'mc:Ignorable="d"',
        "x:Class=`"(.*?)`"",
        "xmlns:local=`"(.*?)`""
    )

    $xaml = Get-Content $x.FullName # Load XAML
    $xaml = $xaml -replace "x:N",'N' # Rename x:Name to just Name (for consumption in variables later)
    foreach($xtr in $xamlToRemove){ $xaml = $xaml -replace $xtr } # Remove items from $xamlToRemove
    
    # Create a new variable to store the XAML as XML
    New-Variable -Name "xaml$(($x.BaseName) -replace $xp, '_')" -Value ($xaml -as [xml]) -Force
    
    # Add XAML to list of XAML documents processed
    $vx += "$(($x.BaseName) -replace $xp, '_')"
}
#######################
## Add Media Resources
#######################
$imageFileTypes = @(".jpg",".bmp",".gif",".tif",".png") # Supported image filetypes
$avFileTypes = @(".mp3",".wav",".wmv") # Supported audio/visual filetypes
$xp = '[^a-zA-Z_0-9]' # All characters that are not a-Z, 0-9, or _
if($MediaResources.Count -gt 0){
    ## Okay... the following code is just silly. I know
    ## but hear me out. Adding the nodes to the elements
    ## directly caused big issues - mainly surrounding the
    ## "x:" namespace identifiers. This is a hacky fix but
    ## it does the trick.
    foreach($v in $vx)
    {
        $xml = ((Get-Variable -Name "xaml$($v)").Value) # Load the XML

        # add the resources needed for strings
        $xml.DocumentElement.SetAttribute("xmlns:sys","clr-namespace:System;assembly=System")

        # if the document doesn't already have a "Window.Resources" create it
        if($null -eq ($xml.DocumentElement.'Window.Resources')){ 
            $fragment = "<Window.Resources>" 
            $fragment += "<ResourceDictionary>"
        }
        
        # Add each StaticResource with the key of the base name and source to the full name
        foreach($sr in $MediaResources)
        {
            $srname = "$($sr.BaseName -replace $xp, '_')$($sr.Extension.Substring(1).ToUpper())" #convert name to basename + Uppercase Extension
            if($sr.Extension -in $imageFileTypes){ $fragment += "<BitmapImage x:Key=`"$srname`" UriSource=`"$($sr.FullName)`" />" }
            if($sr.Extension -in $avFileTypes){ 
                $uri = [System.Uri]::new($sr.FullName)
                $fragment += "<sys:Uri x:Key=`"$srname`">$uri</sys:Uri>" 
            }    
        }

        # if the document doesn't already have a "Window.Resources" close it
        if($null -eq ($xml.DocumentElement.'Window.Resources'))
        {
            $fragment += "</ResourceDictionary>"
            $fragment += "</Window.Resources>"
            $xml.DocumentElement.InnerXml = $fragment + $xml.DocumentElement.InnerXml
        }
        # otherwise just add the fragment to the existing resource dictionary
        else
        {
            $xml.DocumentElement.'Window.Resources'.ResourceDictionary.InnerXml += $fragment
        }

        # Reset the value of the variable
        (Get-Variable -Name "xaml$($v)").Value = $xml
    }
}
#################
## Create "Forms"
#################
$forms = @()
foreach($x in $vx)
{
    $Reader = (New-Object System.Xml.XmlNodeReader ((Get-Variable -Name "xaml$($x)").Value)) #load the xaml we created earlier into XmlNodeReader
    New-Variable -Name "form$($x)" -Value ([Windows.Markup.XamlReader]::Load($Reader)) -Force #load the xaml into XamlReader
    $forms += "form$($x)" #add the form name to our array
    $SyncClass.SyncHash.Add("form$($x)", (Get-Variable -Name "form$($x)").Value) #add the form object to our synched hashtable
}
#################################
## Create Controls (Buttons, etc)
#################################
$controls = @()
$xp = '[^a-zA-Z_0-9]' # All characters that are not a-Z, 0-9, or _
foreach($x in $vx)
{
    $xaml = (Get-Variable -Name "xaml$($x)").Value #load the xaml we created earlier
    $xaml.SelectNodes("//*[@Name]") | %{ #find all nodes with a "Name" attribute
        $cname = "form$($x)Control$(($_.Name -replace $xp, '_'))"
        Set-Variable -Name "$cname" -Value $SyncClass.SyncHash."form$($x)".FindName($_.Name) #create a variale to hold the control/object
        $controls += (Get-Variable -Name "form$($x)Control$($_.Name)").Name #add the control name to our array
        $SyncClass.SyncHash.Add($cname, $SyncClass.SyncHash."form$($x)".FindName($_.Name)) #add the control directly to the hashtable
    }
}
############################
## FORMS AND CONTROLS OUTPUT
############################
<# Write-Host -ForegroundColor Cyan "The following forms were created:"
$forms | %{ Write-Host -ForegroundColor Yellow " `$$_"} #output all forms to screen
if($controls.Count -gt 0){
    Write-Host ""
    Write-Host -ForegroundColor Cyan "The following controls were created:"
    $controls | %{ Write-Host -ForegroundColor Yellow " `$$_"} #output all named controls to screen
} #>

#######################
## DISABLE A/V AUTOPLAY
#######################
foreach($x in $vx)
{
    $carray = @()
    $fts = $syncClass.SyncHash."form$($x)"
    foreach($c in $fts.Content.Children)
    {
        if($c.GetType().Name -eq "MediaElement") #find all controls with the type MediaElement
        {
            $c.LoadedBehavior = "Manual" #Don't autoplay
            $c.UnloadedBehavior = "Stop" #When the window closes, stop the music
            $carray += $c #add the control to an array
        }
    }
    if($carray.Count -gt 0)
    {
        New-Variable -Name "form$($x)PoSHPFCleanupAudio" -Value $carray -Force # Store the controls in an array to be accessed later
        $syncClass.SyncHash."form$($x)".Add_Closed({
            foreach($c in (Get-Variable "form$($x)PoSHPFCleanupAudio").Value)
            {
                $c.Source = $null #stops any currently playing media
            }
        })
    }
}

#####################
## RUNSPACE FUNCTIONS
#####################
## Yo dawg... Runspace to clean up Runspaces
## Thank you Boe Prox / Stephen Owen
#region RSCleanup
$Script:JobCleanup = [hashtable]::Synchronized(@{}) 
$Script:Jobs = [system.collections.arraylist]::Synchronized((New-Object System.Collections.ArrayList)) #hashtable to store all these runspaces
$jobCleanup.Flag = $True #cleanup jobs
$newRunspace =[runspacefactory]::CreateRunspace() #create a new runspace for this job to cleanup jobs to live
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("jobCleanup",$jobCleanup) #pass the jobCleanup variable to the runspace
$newRunspace.SessionStateProxy.SetVariable("jobs",$jobs) #pass the jobs variable to the runspace
$jobCleanup.PowerShell = [PowerShell]::Create().AddScript({
    #Routine to handle completed runspaces
    Do {    
        Foreach($runspace in $jobs) {            
            If ($runspace.Runspace.isCompleted) {                         #if runspace is complete
                [void]$runspace.powershell.EndInvoke($runspace.Runspace)  #then end the script
                $runspace.powershell.dispose()                            #dispose of the memory
                $runspace.Runspace = $null                                #additional garbage collection
                $runspace.powershell = $null                              #additional garbage collection
            } 
        }
        #Clean out unused runspace jobs
        $temphash = $jobs.clone()
        $temphash | Where {
            $_.runspace -eq $Null
        } | ForEach {
            $jobs.remove($_)
        }        
        Start-Sleep -Seconds 1 #lets not kill the processor here
    } while ($jobCleanup.Flag)
})
$jobCleanup.PowerShell.Runspace = $newRunspace
$jobCleanup.Thread = $jobCleanup.PowerShell.BeginInvoke() 
#endregion RSCleanup

#This function creates a new runspace for a script block to execute
#so that you can do your long running tasks not in the UI thread.
#Also the SyncClass is passed to this runspace so you can do UI
#updates from this thread as well.
function Start-BackgroundScriptBlock($scriptBlock){
    $newRunspace =[runspacefactory]::CreateRunspace()
    $newRunspace.ApartmentState = "STA"
    $newRunspace.ThreadOptions = "ReuseThread"          
    $newRunspace.Open()
    $newRunspace.SessionStateProxy.SetVariable("SyncClass",$SyncClass) 
    $PowerShell = [PowerShell]::Create().AddScript($scriptBlock)
    $PowerShell.Runspace = $newRunspace
    $PowerShell.BeginInvoke()

    #Add it to the job list so that we can make sure it is cleaned up
<# [void]$Jobs.Add(
        [pscustomobject]@{
            PowerShell = $PowerShell
            Runspace = $PowerShell.BeginInvoke()
        }
    ) #>

}
#================================================
# Window Functions
# Minimize Command and PowerShell Windows
#================================================
$Script:showWindowAsync = Add-Type -MemberDefinition @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@
 -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
function Hide-CmdWindow() {
    $CMDProcess = Get-Process -Name cmd -ErrorAction Ignore
    foreach ($Item in $CMDProcess) {
        $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $Item.id).MainWindowHandle, 2)
    }
}
function Hide-PowershellWindow() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 2)
}
function Show-PowershellWindow() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 10)
}
#================================================
# Initialize
#================================================
$localOSDCloudParams = (Get-Command Start-OSDCloud).Parameters

<# $localOSDCloudParams["OSBuild"].Attributes.ValidValues | ForEach-Object {
    $formMainWindowControlOSBuildCombobox.Items.Add($_) | Out-Null
} #>


$localOSDCloudParams["OSEdition"].Attributes.ValidValues | ForEach-Object {
    $formMainWindowControlOSEditionCombobox.Items.Add($_) | Out-Null
}

$localOSDCloudParams["OSLicense"].Attributes.ValidValues | ForEach-Object {
    $formMainWindowControlOSLicenseCombobox.Items.Add($_) | Out-Null
}

$localOSDCloudParams["OSLanguage"].Attributes.ValidValues | ForEach-Object {
    $formMainWindowControlOSLanguageCombobox.Items.Add($_) | Out-Null
}

$formMainWindowControlCSManufacturerTextbox.Text = Get-MyComputerManufacturer -Brief
$formMainWindowControlCSProductTextbox.Text = Get-MyComputerProduct
$formMainWindowControlCSModelTextbox.Text = Get-MyComputerModel -Brief
#================================================
# SetDefaultWin
#================================================
function SetDefaultWin10 {
    $formMainWindowControlOSBuildCombobox.Items.Clear()
    $localOSDCloudParams["OSBuild"].Attributes.ValidValues | ForEach-Object {
        $formMainWindowControlOSBuildCombobox.Items.Add($_) | Out-Null
    }
    
    $formMainWindowControlOperatingSystemCombobox.SelectedIndex = 0 #Windows 10
    $formMainWindowControlOSBuildCombobox.SelectedIndex = 0      #21H2
    $formMainWindowControlOSLanguageCombobox.SelectedIndex = 7   #en-us
    $formMainWindowControlOSEditionCombobox.SelectedIndex = 5    #Enterprise
    $formMainWindowControlOSLicenseCombobox.SelectedIndex = 1    #Volume

    $formMainWindowControlAutopilotJsonCombobox.SelectedIndex = 1    #OOBE
    $formMainWindowControlImageIndexTextbox.Text = 6             #Enterprise

    $formMainWindowControlOSBuildCombobox.IsEnabled = $true
    $formMainWindowControlOSEditionCombobox.IsEnabled = $true
    $formMainWindowControlOSLanguageCombobox.IsEnabled = $true
    $formMainWindowControlOSLicenseCombobox.IsEnabled = $false
    $formMainWindowControlImageIndexTextbox.IsEnabled = $false
    $formMainWindowControlCSModelTextbox.IsEnabled = $false
    $formMainWindowControlAutopilotJsonCombobox.IsEnabled = $true

    $formMainWindowControlImageNameCombobox.Items.Clear()
    $formMainWindowControlImageNameCombobox.Visibility = "Collapsed"
    
    $formMainWindowControlOSBuildCombobox.Visibility = "Visible"
    $formMainWindowControlOSEditionCombobox.Visibility = "Visible"
    $formMainWindowControlOSLanguageCombobox.Visibility = "Visible"
    $formMainWindowControlOSLicenseCombobox.Visibility = "Visible"
}
function SetDefaultWin11 {
    $formMainWindowControlOperatingSystemCombobox.SelectedIndex = 1 #Windows 11

    $formMainWindowControlOSBuildCombobox.Items.Clear()
    $formMainWindowControlOSBuildCombobox.Items.Add("21H2") | Out-Null
    
    $formMainWindowControlOSBuildCombobox.SelectedIndex = 0      #21H2
    $formMainWindowControlOSLanguageCombobox.SelectedIndex = 7   #en-us
    $formMainWindowControlOSEditionCombobox.SelectedIndex = 5    #Enterprise
    $formMainWindowControlOSLicenseCombobox.SelectedIndex = 1    #Volume

    $formMainWindowControlAutopilotJsonCombobox.SelectedIndex = 1    #OOBE
    $formMainWindowControlImageIndexTextbox.Text = 6             #Enterprise

    $formMainWindowControlOSBuildCombobox.IsEnabled = $true
    $formMainWindowControlOSEditionCombobox.IsEnabled = $true
    $formMainWindowControlOSLanguageCombobox.IsEnabled = $true
    $formMainWindowControlOSLicenseCombobox.IsEnabled = $false
    $formMainWindowControlImageIndexTextbox.IsEnabled = $false
    $formMainWindowControlCSModelTextbox.IsEnabled = $false
    $formMainWindowControlAutopilotJsonCombobox.IsEnabled = $true

    $formMainWindowControlImageNameCombobox.Items.Clear()
    $formMainWindowControlImageNameCombobox.Visibility = "Collapsed"
    
    $formMainWindowControlOSBuildCombobox.Visibility = "Visible"
    $formMainWindowControlOSEditionCombobox.Visibility = "Visible"
    $formMainWindowControlOSLanguageCombobox.Visibility = "Visible"
    $formMainWindowControlOSLicenseCombobox.Visibility = "Visible"
}
SetDefaultWin10
#================================================
# CustomImage
#================================================
[array]$OSDCloudOSIso = @()
[array]$OSDCloudOSIso = Find-OSDCloudFile -Name '*.iso' -Path '\OSDCloud\OS\' | Where-Object {$_.Length -gt 3GB}

foreach ($Item in $OSDCloudOSIso) {
    if ((Get-DiskImage -ImagePath $Item.FullName).Attached) {
        #ISO is already mounted
    }
    else {
        Write-Host "Mounting OSDCloud OS ISO $($Item.FullName)" -ForegroundColor Cyan
        $Results = Mount-DiskImage -ImagePath $Item.FullName
        $Results | Select-Object -Property Attached,DevicePath,ImagePath,Number,Size | Format-List
    }
}

$CustomImageChildItem = @()
[array]$CustomImageChildItem = Find-OSDCloudFile -Name '*.wim' -Path '\OSDCloud\OS\'
[array]$CustomImageChildItem += Find-OSDCloudFile -Name 'install.wim' -Path '\Sources\'
$CustomImageChildItem = $CustomImageChildItem | Sort-Object -Property Length -Unique | Sort-Object FullName | Where-Object {$_.Length -gt 3GB}
        
if ($CustomImageChildItem) {
    $CustomImageChildItem | ForEach-Object {
        $formMainWindowControlOperatingSystemCombobox.Items.Add($_) | Out-Null
    }
}
#================================================
# AutopilotJsonCombobox
#================================================
$formMainWindowControlAutopilotJsonCombobox.IsEnabled = $false
$AutopilotJsonChildItem = @()
[array]$AutopilotJsonChildItem = Find-OSDCloudFile -Name "*.json" -Path '\OSDCloud\Autopilot\Profiles\' | Sort-Object FullName
[array]$AutopilotJsonChildItem += Find-OSDCloudFile -Name "*.json" -Path '\OSDCloud\Config\AutopilotJSON\' | Sort-Object FullName
$AutopilotJsonChildItem = $AutopilotJsonChildItem | Where-Object {$_.FullName -notlike "C*"}
if ($AutopilotJsonChildItem) {
    $formMainWindowControlAutopilotJsonCombobox.Items.Add('') | Out-Null
    $formMainWindowControlAutopilotJsonCombobox.IsEnabled = $true
    $AutopilotJsonChildItem | ForEach-Object {
        $formMainWindowControlAutopilotJsonCombobox.Items.Add($_) | Out-Null
    }
    $formMainWindowControlAutopilotJsonCombobox.SelectedIndex = 1
}
else {
    $formMainWindowControlAutopilotJsonLabel.Visibility = "Collapsed" 
    $formMainWindowControlAutopilotJsonCombobox.Visibility = "Collapsed"  
}
#================================================
# OOBEDeployCombobox
#================================================
$formMainWindowControlOOBEDeployCombobox.IsEnabled = $false
$OOBEDeployJsonChildItem = Find-OSDCloudFile -Name "*.json" -Path '\OSDCloud\Config\OOBEDeploy\' | Sort-Object FullName
$OOBEDeployJsonChildItem = $OOBEDeployJsonChildItem | Where-Object {$_.FullName -notlike "C*"}
if ($OOBEDeployJsonChildItem) {
    $formMainWindowControlOOBEDeployCombobox.Items.Add('') | Out-Null
    $formMainWindowControlOOBEDeployCombobox.IsEnabled = $true
    $OOBEDeployJsonChildItem | ForEach-Object {
        $formMainWindowControlOOBEDeployCombobox.Items.Add($_) | Out-Null
    }
    $formMainWindowControlOOBEDeployCombobox.SelectedIndex = 1
}
else {
    $formMainWindowControlOOBEDeployLabel.Visibility = "Collapsed"  
    $formMainWindowControlOOBEDeployCombobox.Visibility = "Collapsed"  
}
#================================================
# AutopilotOOBECombobox
#================================================
$formMainWindowControlAutopilotOOBECombobox.IsEnabled = $false
$AutopilotOOBEJsonChildItem = Find-OSDCloudFile -Name "*.json" -Path '\OSDCloud\Config\AutopilotOOBE\' | Sort-Object FullName
$AutopilotOOBEJsonChildItem = $AutopilotOOBEJsonChildItem | Where-Object {$_.FullName -notlike "C*"}
if ($AutopilotOOBEJsonChildItem) {
    $formMainWindowControlAutopilotOOBECombobox.Items.Add('') | Out-Null
    $formMainWindowControlAutopilotOOBECombobox.IsEnabled = $true
    $AutopilotOOBEJsonChildItem | ForEach-Object {
        $formMainWindowControlAutopilotOOBECombobox.Items.Add($_) | Out-Null
    }
    $formMainWindowControlAutopilotOOBECombobox.SelectedIndex = 1
}
else {
    $formMainWindowControlAutopilotOOBELabel.Visibility = "Collapsed"  
    $formMainWindowControlAutopilotOOBECombobox.Visibility = "Collapsed"  
}
#================================================
# OSEditionCombobox
#================================================
$formMainWindowControlOSEditionCombobox.add_SelectionChanged({
    #Home
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 0) {
        $formMainWindowControlImageIndexTextbox.Text = 4
        $formMainWindowControlImageIndexLabel.IsEnabled = $false
        $formMainWindowControlImageIndexTextbox.IsEnabled = $false   #Disable
        $formMainWindowControlOSLicenseCombobox.SelectedIndex = 0    #Retail
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $false   #Disable
    }
    #Home N
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 1) {
        $formMainWindowControlImageIndexTextbox.Text = 5
        $formMainWindowControlImageIndexTextbox.IsEnabled = $false   #Disable
        $formMainWindowControlOSLicenseCombobox.SelectedIndex = 0    #Retail
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $false   #Disable
    }
    #Home Single Language
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 2) {
        $formMainWindowControlImageIndexTextbox.Text = 6
        $formMainWindowControlImageIndexTextbox.IsEnabled = $false   #Disable
        $formMainWindowControlOSLicenseCombobox.SelectedIndex = 0    #Retail
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $false   #Disable
    }
    #Education
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 3) {
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $true
        if ($formMainWindowControlOSLicenseCombobox.SelectedIndex -eq 0) {
            $formMainWindowControlImageIndexTextbox.Text = 7
        }
        else {
            $formMainWindowControlImageIndexTextbox.Text = 4
        }
    }
    #Education N
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 4) {
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $true
        if ($formMainWindowControlOSLicenseCombobox.SelectedIndex -eq 0) {
            $formMainWindowControlImageIndexTextbox.Text = 8
        }
        else {
            $formMainWindowControlImageIndexTextbox.Text = 5
        }
    }
    #Enterprise
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 5) {
        $formMainWindowControlOSLicenseCombobox.SelectedIndex = 1
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $false
        $formMainWindowControlImageIndexTextbox.Text = 6
    }
    #Enterprise N
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 6) {
        $formMainWindowControlOSLicenseCombobox.SelectedIndex = 1
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $false
        $formMainWindowControlImageIndexTextbox.Text = 7
    }
    #Pro
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 7) {
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $true
        if ($formMainWindowControlOSLicenseCombobox.SelectedIndex -eq 0) {
            $formMainWindowControlImageIndexTextbox.Text = 9
        }
        else {
            $formMainWindowControlImageIndexTextbox.Text = 8
        }
    }
    #Pro N
    if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 8) {
        $formMainWindowControlOSLicenseCombobox.IsEnabled = $true
        if ($formMainWindowControlOSLicenseCombobox.SelectedIndex -eq 0) {
            $formMainWindowControlImageIndexTextbox.Text = 10
        }
        else {
            $formMainWindowControlImageIndexTextbox.Text = 9
        }
    }
})
#================================================
# OSLicenseCombobox
#================================================
$formMainWindowControlOSLicenseCombobox.add_SelectionChanged({
    if ($formMainWindowControlOSLicenseCombobox.SelectedIndex -eq 0) {
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 3) {$formMainWindowControlImageIndexTextbox.Text = 7}
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 4) {$formMainWindowControlImageIndexTextbox.Text = 8}
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 7) {$formMainWindowControlImageIndexTextbox.Text = 9}
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 8) {$formMainWindowControlImageIndexTextbox.Text = 10}
    }
    if ($formMainWindowControlOSLicenseCombobox.SelectedIndex -eq 1) {
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 3) {$formMainWindowControlImageIndexTextbox.Text = 4}
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 4) {$formMainWindowControlImageIndexTextbox.Text = 5}
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 7) {$formMainWindowControlImageIndexTextbox.Text = 8}
        if ($formMainWindowControlOSEditionCombobox.SelectedIndex -eq 8) {$formMainWindowControlImageIndexTextbox.Text = 9}
    }
})
#================================================
# OperatingSystemCombobox
#================================================
$formMainWindowControlOperatingSystemCombobox.add_SelectionChanged({
    if ($formMainWindowControlOperatingSystemCombobox.SelectedIndex -eq 0) {
        SetDefaultWin10
    }
    elseif ($formMainWindowControlOperatingSystemCombobox.SelectedIndex -eq 1) {
        SetDefaultWin11
    }
    else {
        $formMainWindowControlOSBuildCombobox.Visibility = "Collapsed"
        $formMainWindowControlOSEditionCombobox.Visibility = "Collapsed"
        $formMainWindowControlOSLanguageCombobox.Visibility = "Collapsed"
        $formMainWindowControlOSLicenseCombobox.Visibility = "Collapsed"
        $formMainWindowControlImageIndexTextbox.IsEnabled = $false
        $formMainWindowControlImageIndexTextbox.Text = 1

        $formMainWindowControlImageNameCombobox.Visibility = "Visible"
        $formMainWindowControlImageNameCombobox.Items.Clear()
        $formMainWindowControlImageNameCombobox.IsEnabled = $true
        $GetWindowsImageOptions = Get-WindowsImage -ImagePath $formMainWindowControlOperatingSystemCombobox.SelectedValue
        $GetWindowsImageOptions | ForEach-Object {
            $formMainWindowControlImageNameCombobox.Items.Add($_.ImageName) | Out-Null
        }
        $formMainWindowControlImageNameCombobox.SelectedIndex = 0
    }
})
$formMainWindowControlImageNameCombobox.add_SelectionChanged({
    $formMainWindowControlImageIndexTextbox.Text = $formMainWindowControlImageNameCombobox.SelectedIndex + 1
    if ($formMainWindowControlImageIndexTextbox.Text -eq 0) {$formMainWindowControlImageIndexTextbox.Text = 1}
})
#================================================
# StartButton
#================================================
$formMainWindowControlStartButton.add_Click({
    $formMainWindow.Close()
    Show-PowershellWindow
    #================================================
    # Variables
    #================================================
    if ($formMainWindowControlOperatingSystemCombobox.SelectedIndex -eq 0) {
        $OSVersion = 'Windows 10'
    }
    elseif ($formMainWindowControlOperatingSystemCombobox.SelectedIndex -eq 1) {
        $OSVersion = 'Windows 11'
    }
    else {
        $OSVersion = $null
    }
    $OSBuild = $formMainWindowControlOSBuildCombobox.SelectedItem
    $OSEdition = $formMainWindowControlOSEditionCombobox.SelectedItem
    $OSLanguage = $formMainWindowControlOSLanguageCombobox.SelectedItem
    $OSLicense = $formMainWindowControlOSLicenseCombobox.SelectedItem
    $OSImageIndex = $formMainWindowControlImageIndexTextbox.Text
    #================================================
    # AutopilotJson
    #================================================
    $AutopilotJsonName = $formMainWindowControlAutopilotJsonCombobox.SelectedValue
    if ($AutopilotJsonName) {
        $AutopilotJsonItem = $AutopilotJsonChildItem | Where-Object {$_.FullName -eq "$AutopilotJsonName"}
    }
    else {
        $SkipAutopilot = $true
        $AutopilotJsonName = $null
        $AutopilotJsonItem = $null
    }
    if ($AutopilotJsonItem) {
        $AutopilotJsonObject = Get-Content -Raw $AutopilotJsonItem.FullName | ConvertFrom-Json
        $SkipAutopilot = $false
    }
    else {
        $SkipAutopilot = $true
        $AutopilotJsonObject = $null
    }
    #================================================
    # OOBEDeployJson
    #================================================
    $OOBEDeployJsonName = $formMainWindowControlOOBEDeployCombobox.SelectedValue
    if ($OOBEDeployJsonName) {
        $OOBEDeployJsonItem = $OOBEDeployJsonChildItem | Where-Object {$_.FullName -eq "$OOBEDeployJsonName"}
    }
    else {
        $SkipOOBEDeploy = $true
        $OOBEDeployJsonName = $null
        $OOBEDeployJsonItem = $null
    }
    if ($OOBEDeployJsonItem) {
        $OOBEDeployJsonObject = Get-Content -Raw $OOBEDeployJsonItem.FullName | ConvertFrom-Json
        $SkipOOBEDeploy = $false
    }
    else {
        $SkipOOBEDeploy = $true
        $OOBEDeployJsonObject = $null
    }
    #================================================
    # AutopilotOOBEJson
    #================================================
    $AutopilotOOBEJsonName = $formMainWindowControlAutopilotOOBECombobox.SelectedValue
    if ($AutopilotOOBEJsonName) {
        $AutopilotOOBEJsonItem = $AutopilotOOBEJsonChildItem | Where-Object {$_.FullName -eq "$AutopilotOOBEJsonName"}
    }
    else {
        $SkipAutopilotOOBE = $true
        $AutopilotOOBEJsonName = $null
        $AutopilotOOBEJsonItem = $null
    }
    if ($AutopilotOOBEJsonItem) {
        $AutopilotOOBEJsonObject = Get-Content -Raw $AutopilotOOBEJsonItem.FullName | ConvertFrom-Json
        $SkipAutopilotOOBE = $false
    }
    else {
        $SkipAutopilotOOBE = $true
        $AutopilotOOBEJsonObject = $null
    }
    #================================================
    # ImageFile
    #================================================
    if ($formMainWindowControlOperatingSystemCombobox.SelectedIndex -ge 2) {
        $ImageFileFullName = $formMainWindowControlOperatingSystemCombobox.SelectedValue
        if ($ImageFileFullName) {
            $ImageFileItem = $CustomImageChildItem | Where-Object {$_.FullName -eq "$ImageFileFullName"}
            $ImageFileName = Split-Path -Path $ImageFileItem.FullName -Leaf
            $OSVersion = $null
            $OSBuild = $null
            $OSEdition = $null
            $OSLanguage = $null
            $OSLicense = $null
        }
        else {
            $ImageFileItem = $null
            $ImageFileFullName = $null
            $ImageFileName = $null
        }
    }

    #================================================
    # Global Variables
    #================================================
    $Global:StartOSDCloudGUI = $null
    $Global:StartOSDCloudGUI = [ordered]@{
        AutopilotJsonChildItem      = $AutopilotJsonChildItem
        AutopilotJsonItem           = $AutopilotJsonItem
        AutopilotJsonName           = $AutopilotJsonName
        AutopilotJsonObject         = $AutopilotJsonObject
        AutopilotOOBEJsonChildItem  = $AutopilotOOBEJsonChildItem
        AutopilotOOBEJsonItem       = $AutopilotOOBEJsonItem
        AutopilotOOBEJsonName       = $AutopilotOOBEJsonName
        AutopilotOOBEJsonObject     = $AutopilotOOBEJsonObject
        ImageFileFullName           = $ImageFileFullName
        ImageFileItem               = $ImageFileItem
        ImageFileName               = $ImageFileName
        Manufacturer                = $formMainWindowControlCSManufacturerTextbox.Text
        OOBEDeployJsonChildItem     = $OOBEDeployJsonChildItem
        OOBEDeployJsonItem          = $OOBEDeployJsonItem
        OOBEDeployJsonName          = $OOBEDeployJsonName
        OOBEDeployJsonObject        = $OOBEDeployJsonObject
        OSBuild                     = $OSBuild
        OSEdition                   = $OSEdition
        OSImageIndex                = $OSImageIndex
        OSLanguage                  = $OSLanguage
        OSLicense                   = $OSLicense
        OSVersion                   = $OSVersion
        Product                     = $formMainWindowControlCSProductTextbox.Text
        Restart                     = $formMainWindowControlRestartCheckbox.IsChecked
        SkipAutopilot               = $SkipAutopilot
        SkipAutopilotOOBE           = $SkipAutopilotOOBE
        SkipODT                     = $true
        SkipOOBEDeploy              = $SkipOOBEDeploy
        ZTI                         = $formMainWindowControlZTICheckbox.IsChecked
    }
    #$Global:StartOSDCloudGUI | Out-Host
    if ($formMainWindowControlScreenshotCheckbox.IsChecked) {
        $Params = @{
            Screenshot = $true
        }
        Start-OSDCloud @Params
    }
    else {
        Start-OSDCloud 
    }
})
#================================================
# Customizations
#================================================
[string]$ModuleVersion = Get-Module -Name OSD | Sort-Object -Property Version | Select-Object -ExpandProperty Version -Last 1
$formMainWindow.Title = "OSDCloudGUI $ModuleVersion"
#================================================
# Branding
#================================================
if ($Global:OSDCloudGuiBranding) {
    $formMainWindowControlBrandingTitleControl.Content = $Global:OSDCloudGuiBranding.Title
    $formMainWindowControlBrandingTitleControl.Foreground = $Global:OSDCloudGuiBranding.Color
}
#================================================
# Hide Windows
#================================================
Hide-CmdWindow
Hide-PowershellWindow
########################
## WIRE UP YOUR CONTROLS
########################
# simple example: $formMainWindowControlButton.Add_Click({ your code })
#
# example with BackgroundScriptBlock and UpdateElement
# $formmainControlButton.Add_Click({
# $sb = {
# $SyncClass.UpdateElement("formmainControlProgress","Value",25)
# }
# Start-BackgroundScriptBlock $sb
# })

############################
###### DISPLAY DIALOG ######
############################
[void]$formMainWindow.ShowDialog()

##########################
##### SCRIPT CLEANUP #####
##########################
$jobCleanup.Flag = $false #Stop Cleaning Jobs
$jobCleanup.PowerShell.Runspace.Close() #Close the runspace
$jobCleanup.PowerShell.Dispose() #Remove the runspace from memory