Projects/DiskImageGUI.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
#================================================
# PowershellWindow Functions
#================================================
$Script:showWindowAsync = Add-Type -MemberDefinition @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@
 -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
function Show-PowershellWindow() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 10)
}
function Hide-PowershellWindow() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 2)
}
#Hide-PowershellWindow
#================================================
# Load Assemblies
#================================================
# Assign current script directory to a global variable
$Global:MyScriptDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)

# Load presentationframework and Dlls for the MahApps.Metro theme
[System.Reflection.Assembly]::LoadWithPartialName("presentationframework") | Out-Null
[System.Reflection.Assembly]::LoadFrom("$Global:MyScriptDir\assembly\MahApps.Metro.dll") | Out-Null
[System.Reflection.Assembly]::LoadFrom("$Global:MyScriptDir\assembly\System.Windows.Interactivity.dll") | Out-Null
#================================================
# Set PowerShell Window Title
#================================================
$host.ui.RawUI.WindowTitle = "PowerShell Start-DiskImageGUI"
#================================================
# Test-InWinPE
#================================================
function Test-InWinPE {
    return Test-Path -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlset\Control\MiniNT
}
#================================================
# LoadForm
#================================================
function LoadForm {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $False, Position = 1)]
        [string]$XamlPath
    )

    # Import the XAML code
    [xml]$Global:xmlWPF = Get-Content -Path $XamlPath

    # Add WPF and Windows Forms assemblies
    Try {
        Add-Type -AssemblyName PresentationCore, PresentationFramework, WindowsBase, system.windows.forms
    } 
    Catch {
        Throw "Failed to load Windows Presentation Framework assemblies."
    }

    #Create the XAML reader using a new XML node reader
    $Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))

    #Create hooks to each named object in the XAML
    $xmlWPF.SelectNodes("//*[@Name]") | foreach {
        Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
    }
}
LoadForm -XamlPath (Join-Path $Global:MyScriptDir 'DiskImageGUI.xaml')
#================================================
# GlobalVariables
#================================================
$Global:DiskImageGUI = @{
    BiosVersion = Get-MyBiosVersion
    AvailableDisks = Get-Disk.fixed | Where-Object {$_.IsBoot -eq $false}
    Manufacturer = Get-MyComputerManufacturer -Brief
    Model = Get-MyComputerModel -Brief
    RegCurrentVersion = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
    SerialNumber = Get-MyBiosSerialNumber -Brief
    Title = 'DiskImageGUI'
    Version = Get-Module -Name OSDeploy | Sort-Object -Property Version | Select-Object -ExpandProperty Version -Last 1
}
#================================================
# RemovedDisks
#================================================
$RemovedDisks = @()
$RemovedDisks = Get-Disk.fixed | Where-Object {$_.IsBoot -eq $true}
if ($RemovedDisks) {
    foreach ($Item in $RemovedDisks) {
        Write-Warning "Removing Disk $($Item.Number) $($Item.FriendlyName) as you cannot capture or apply to a running disk"
    }
}
#================================================
# Subtitle
#================================================
$Global:DiskImageGUI.ProductName = ($Global:DiskImageGUI.RegCurrentVersion).ProductName

if ($Global:DiskImageGUI.RegCurrentVersion.DisplayVersion -gt 0) {
    $Global:DiskImageGUI.DisplayVersion = ($Global:DiskImageGUI.RegCurrentVersion).DisplayVersion
}
else {
    $Global:DiskImageGUI.DisplayVersion = ($Global:DiskImageGUI.RegCurrentVersion).ReleaseId
}
$Global:DiskImageGUI.BuildNumber = "$($Global:DiskImageGUI.RegCurrentVersion.CurrentBuild).$($Global:DiskImageGUI.RegCurrentVersion.UBR)"
#================================================
# Content
#================================================
$LabelTitle.Content = $Global:DiskImageGUI.Title
$LabelVersion.Content = $Global:DiskImageGUI.Version
$LabelManufacturer.Content = $Global:DiskImageGUI.Manufacturer
$LabelModel.Content = $Global:DiskImageGUI.Model
$LabelSerialNumber.Content = $Global:DiskImageGUI.SerialNumber
$LabelSubtitle.Content = "$($Global:DiskImageGUI.ProductName) $($Global:DiskImageGUI.DisplayVersion) ($($Global:DiskImageGUI.BuildNumber))"
$LabelBiosVersion.Content = "BIOS: $($Global:DiskImageGUI.BiosVersion)"
$DismDescriptionTextbox.Text = "$($Global:DiskImageGUI.Manufacturer) $($Global:DiskImageGUI.Model) $($Global:DiskImageGUI.SerialNumber)"
#================================================
# Right Side TPM
#================================================
try {
    $TpmSpecVersion = (Get-CimInstance -Namespace "root\CIMV2\Security\MicrosoftTPM" -ClassName Win32_Tpm).SpecVersion
}
catch {}

if ($TpmSpecVersion -match '2.0') {
    $LabelTpmVersion.Content = "TPM: 2.0"
    $LabelTpmVersion.Background = "Green"
}
elseif ($TpmSpecVersion -match '1.2') {
    $LabelTpmVersion.Content = "TPM: 1.2"
    $LabelTpmVersion.Background = "Red"
}
else {
    $LabelTpmVersion.Visibility = "Collapsed"
}
#================================================
# Hide
#================================================
function Reset-DismSource {
    $DismSourceLabel.Content = ""
    $DismSourceLabel.Visibility = "Collapsed"

    $DismSourceCombobox.Items.Clear()
    $DismSourceCombobox.Visibility = "Collapsed"
}
function Reset-DismDestination {
    $DismDestinationLabel.Content = "/ImageFile:"
    $DismDestinationLabel.Visibility = "Collapsed"

    $DismDestinationCombobox.Items.Clear()
    $DismDestinationCombobox.Visibility = "Collapsed"
}
function Reset-DismName {
    $DismNameLabel.Content = "/Name:"
    $DismNameLabel.Visibility = "Collapsed"

    $DismNameTextbox.Text = ""
    $DismNameTextbox.Visibility = "Collapsed"
}
function Reset-DismDescription {
    $DismDescriptionLabel.Content = "/Description:"
    $DismDescriptionLabel.Visibility = "Collapsed"

    $DismDescriptionTextbox.Text = ""
    $DismDescriptionTextbox.Visibility = "Collapsed"
}
function Reset-DismCompress {
    $DismCompressLabel.Content = "/Compress:"
    $DismCompressLabel.Visibility = "Collapsed"
    
    $DismCompressCombobox.Items.Clear()
    $DismCompressCombobox.Visibility = "Collapsed"
}
#================================================
# Show
#================================================
function Show-DismSource {
    $DismSourceLabel.Visibility = "Visible"
    $DismSourceCombobox.Visibility = "Visible"
}
function Show-DismDestination {
    $DismDestinationLabel.Visibility = "Visible"
    $DismDestinationCombobox.Visibility = "Visible"
}
function Show-DismName {
    $DismNameLabel.Visibility = "Visible"
    $DismNameTextbox.Visibility = "Visible"
}
function Show-DismDescription {
    $DismDescriptionLabel.Visibility = "Visible"
    $DismDescriptionTextbox.Visibility = "Visible"
    $DismDescriptionTextbox.Text = "$($Global:DiskImageGUI.Manufacturer) $($Global:DiskImageGUI.Model) $($Global:DiskImageGUI.SerialNumber)"
}
function Show-DismCompress {
    $DismCompressLabel.Visibility = "Visible"
    $DismCompressCombobox.Visibility = "Visible"
}
#================================================
# DismAction Defaults
#================================================
function Set-DismAction {
    $DismActionLabel.Content = "Dism.exe"

    $DismActionCombobox.Items.Clear()
    $DismActionCombobox.Items.Add('Command Line Help') | Out-Null
    $DismActionCombobox.Items.Add('/Apply-FFU') | Out-Null
    $DismActionCombobox.Items.Add('/Capture-FFU') | Out-Null
    $DismActionCombobox.SelectedIndex = "0"

    $DismCommandTextbox.Text = "Dism.exe /?"
    $StartButton.Visibility = "Visible"
}
function Reset-DismAction {
    #Write-Verbose -Verbose 'Start Reset-DismAction'
    $DismCommandTextbox.Text = "Dism.exe /?"
    $StartButton.Visibility = "Visible"

    Reset-DismSource
    Reset-DismDestination
    Reset-DismName
    Reset-DismDescription
    Reset-DismCompress
    #Write-Verbose -Verbose 'End Reset-DismAction'
}
Set-DismAction
#================================================
# DismAction add_SelectionChanged
#================================================
$DismActionCombobox.add_SelectionChanged({
    Reset-DismAction
    Start-DismAction
})
#================================================
# Start-DismAction
#================================================
function Start-DismAction {
    $DismCommandTextbox.Background = "#002846"
    $DismCommandTextbox.Foreground = "White"

    if ($DismActionCombobox.SelectedValue -eq 'Command Line Help') {

    }
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        Write-Host -ForegroundColor Cyan        'Dism.exe /Apply-FFU /?'
        Write-Host -ForegroundColor DarkGray    'Applies an .ffu image'

        $DismCommandTextbox.Text = "Dism.exe /Apply-FFU /?"
        $StartButton.Visibility = "Collapsed"
        Start-SourceAction
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        Write-Host -ForegroundColor Cyan        'Dism.exe /Capture-FFU /?'
        Write-Host -ForegroundColor DarkGray    'Captures a physical disk image into a new FFU file'

        $DismCommandTextbox.Text = "Dism.exe /Capture-FFU /?"
        $StartButton.Visibility = "Collapsed"

        $DismCompressLabel.Content = "/Compress:"

        $DismCompressCombobox.Items.Clear()
        $DismCompressCombobox.Items.Add('Default') | Out-Null
        $DismCompressCombobox.Items.Add('None') | Out-Null
        $DismCompressCombobox.SelectedIndex = "0"
        
        Start-SourceAction
    }
}
#================================================
# SourceAction
#================================================
function Start-SourceAction {
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        $DismSourceCombobox.Items.Clear()

        if (!($Global:DiskImageGUI.AvailableDisks)) {
            $DismCommandTextbox.Background = "Pink"
            $DismCommandTextbox.Foreground = "Red"
            $DismCommandTextbox.Text = "There are no disks present that can be used as the ApplyDrive. Make sure you are not booting to the disk that you want to Apply and try again"
            Write-Warning 'There are no disks present that can be used as the ApplyDrive'
            Write-Warning 'Make sure you are not booting to the disk that you want to Apply and try again'
        }
        else {
            $Global:ArrayOfDiskNumbers = @()
            $Global:DiskImageGUI.AvailableDisks | foreach {
                $DismSourceCombobox.Items.Add("\\.\PhysicalDrive$($_.DiskNumber) [$($_.BusType) $($_.MediaType) - $($_.FriendlyName)]") | Out-Null
                $Global:ArrayOfDiskNumbers += $_.Number
            }
            $DismSourceLabel.Content = "/ApplyDrive:"
            Show-DismSource
        }
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        $DismSourceCombobox.Items.Clear()

        if (!($Global:DiskImageGUI.AvailableDisks)) {
            $DismCommandTextbox.Background = "Pink"
            $DismCommandTextbox.Foreground = "Red"
            $DismCommandTextbox.Text = "There are no disks present that can be used as the CaptureDrive. Make sure you are not booting to the disk that you want to Capture and try again"
            Write-Warning 'There are no disks present that can be used as the CaptureDrive'
            Write-Warning 'Make sure you are not booting to the disk that you want to Capture and try again'
        }
        else {
            $Global:ArrayOfDiskNumbers = @()
            $Global:DiskImageGUI.AvailableDisks | foreach {
                $DismSourceCombobox.Items.Add("\\.\PhysicalDrive$($_.DiskNumber) [$($_.BusType) $($_.MediaType) - $($_.FriendlyName)]") | Out-Null
                $Global:ArrayOfDiskNumbers += $_.Number
            }
            $DismSourceLabel.Content = "/CaptureDrive:"
            Show-DismSource
        }
    }
}
#================================================
# DismSource add_SelectionChanged
#================================================
$DismSourceCombobox.add_SelectionChanged({
    Start-DestinationAction
})
#================================================
# DismDestination Start-DestinationAction
#================================================
function Start-DestinationAction {
    $DismCommandTextbox.Background = "#002846"
    $DismCommandTextbox.Foreground = "White"

    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        #Determine the Selected Disk information
        $Global:DiskImageGUI.SourceDiskNumber = (Get-Disk.fixed | Where-Object { $_.Number -eq $Global:ArrayOfDiskNumbers[$DismSourceCombobox.SelectedIndex] }).DiskNumber
        $Global:DiskImageGUI.PhysicalDrive = "\\.\PhysicalDrive$($Global:DiskImageGUI.SourceDiskNumber)"

        $DismCommandTextbox.Text = "Dism.exe /Apply-FFU /ApplyDrive:$($Global:DiskImageGUI.PhysicalDrive) /?"

        $DismDestinationCombobox.Items.Clear()

        $Global:DiskImageGUI.ApplyDrives = @()
        $Global:DiskImageGUI.ApplyDrives = Get-Disk.storage | Where-Object {$_.DiskNumber -ne $($Global:DiskImageGUI.SourceDiskNumber)}

        if (!($Global:DiskImageGUI.ApplyDrives)) {
            $DismCommandTextbox.Background = "Pink"
            $DismCommandTextbox.Foreground = "Red"
            $DismCommandTextbox.Text = "There are no drives present that can contain an ImageFile. Make sure you are not Applying on the drive that contains the ImageFile and try again"
            Write-Warning 'There are no drives present that can contain an ImageFile'
            Write-Warning 'Make sure you are not Applying on the drive that contains the ImageFile and try again'
        }
        else {
            $DismDestinationLabel.Content = "/ImageFile:"
            Show-DismDestination
            $ApplyImageFiles = @()

            foreach ($ApplyDrive in $Global:DiskImageGUI.ApplyDrives) {
                if (Test-Path "$($ApplyDrive.DriveLetter):\Images") {
                    $ApplyImageFiles += Get-ChildItem "$($ApplyDrive.DriveLetter):\Images" -Include *.ffu -File -Recurse -Force -ErrorAction Ignore | Select-Object FullName
                }
            }
    
            if ($ApplyImageFiles) {
                foreach ($Item in $ApplyImageFiles) {
                    $DismDestinationCombobox.Items.Add($Item.FullName) | Out-Null
                }
                $DismDestinationCombobox.SelectedIndex = 0
            }
            $DismDestinationCombobox.IsEditable = "True"
            Set-DismCommandApplyFFU
            $StartButton.Visibility = "Visible"
        }
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        #Determine the Selected Disk information
        $Global:DiskImageGUI.SourceDiskNumber = (Get-Disk.fixed | Where-Object { $_.Number -eq $Global:ArrayOfDiskNumbers[$DismSourceCombobox.SelectedIndex] }).DiskNumber
        $Global:DiskImageGUI.PhysicalDrive = "\\.\PhysicalDrive$($Global:DiskImageGUI.SourceDiskNumber)"

        $DismCommandTextbox.Text = "Dism.exe /Capture-FFU /CaptureDrive:$($Global:DiskImageGUI.PhysicalDrive) /?"

        $DismDestinationCombobox.Items.Clear()

        $Global:DiskImageGUI.ApplyDrives = @()
        $Global:DiskImageGUI.ApplyDrives = Get-Disk.storage | Where-Object {$_.DiskNumber -ne $Global:DiskImageGUI.SourceDiskNumber}

        if (!($Global:DiskImageGUI.ApplyDrives)) {
            $DismCommandTextbox.Background = "Pink"
            $DismCommandTextbox.Foreground = "Red"
            $DismCommandTextbox.Text = "There are no drives present that you can save an ImageFile on. Insert a FAST USB Drive or map a Network Drive and try again"
            Write-Warning 'There are no drives present that you can save an ImageFile on'
            Write-Warning 'Insert a FAST USB Drive or map a Network Drive and try again'
        }
        else {
            $DismDestinationLabel.Content = "/ImageFile:"
            Show-DismDestination
            #Dism Name
            $Global:DiskImageGUI.Name = "disk$($Global:DiskImageGUI.SourceDiskNumber)"
            $DismNameTextbox.Text = $Global:DiskImageGUI.Name

            foreach ($DestinationDrive in $Global:DiskImageGUI.ApplyDrives) {
                if ($DestinationDrive.DriveLetter -gt 0) {
                    $Global:DiskImageGUI.ImageFile = "$($DestinationDrive.DriveLetter):\Images\$($Global:DiskImageGUI.Manufacturer)\$($Global:DiskImageGUI.Model)\$($Global:DiskImageGUI.SerialNumber)_$($Global:DiskImageGUI.Name).ffu"
                    $DismDestinationCombobox.Items.Add($Global:DiskImageGUI.ImageFile) | Out-Null
                }
            }
            $DismDestinationCombobox.SelectedIndex = 0
            $DismDestinationCombobox.IsEditable = "True"

            Show-DismName
            Show-DismDescription
            Show-DismCompress
            Set-DismCommandCaptureFFU
            $StartButton.Visibility = "Visible"
        }
    }
}
#================================================
# Set-DismCommand
#================================================
function Set-DismCommandApplyFFU {
    $DismCommandTextbox.Text = "Dism.exe /Apply-FFU /ApplyDrive:$($Global:DiskImageGUI.PhysicalDrive) /ImageFile=`"$($DismDestinationCombobox.Text)`""
}
function Set-DismCommandCaptureFFU {
    $DismCommandTextbox.Text = "Dism.exe /Capture-FFU /CaptureDrive:$($Global:DiskImageGUI.PhysicalDrive) /ImageFile=`"$($DismDestinationCombobox.Text)`" /Name:`"$($DismNameTextbox.Text)`" /Description:`"$($DismDescriptionTextbox.Text)`" /Compress:$($DismCompressCombobox.SelectedValue)"
}
#================================================
# add_SelectionChanged
#================================================
$DismDestinationCombobox.add_SelectionChanged({
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        Set-DismCommandApplyFFU
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        Set-DismCommandCaptureFFU
    }
})
$DismDestinationCombobox.add_DropDownClosed({
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        Set-DismCommandApplyFFU
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        Set-DismCommandCaptureFFU
    }
})
$DismDestinationCombobox.add_KeyUp({
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        Set-DismCommandApplyFFU
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        Set-DismCommandCaptureFFU
    }
})
$DismNameTextbox.add_KeyUp({
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        Set-DismCommandApplyFFU
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        Set-DismCommandCaptureFFU
    }
})
$DismDescriptionTextbox.add_KeyUp({
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        Set-DismCommandApplyFFU
    }
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        Set-DismCommandCaptureFFU
    }
})
#================================================
# StartButton
#================================================
$StartButton.add_Click({
    #$xamGUI.Close()
    $Global:DismParams = @{}
    #================================================
    # Command Line Help
    #================================================
    if ($DismActionCombobox.SelectedValue -eq 'Command Line Help') {
        Start-Process PowerShell.exe -ArgumentList '-NoExit','-NoLogo','Dism.exe','/?'
    }
    #================================================
    # /Apply-FFU
    #================================================
    if ($DismActionCombobox.SelectedValue -eq '/Apply-FFU') {
        $ParentDirectory = Split-Path $DismDestinationCombobox.Text -Parent -ErrorAction Stop

        if (!(Test-Path $DismDestinationCombobox.Text)) {
            Write-Warning "ImageFile already exists. Rename the ImageFile and try again"
        }
        elseif (!(Test-InWinPE)) {
            Write-Warning "Dism.exe /Apply-FFU requires WinPE so nothing is going to happen here"
        }
        else {
            $xamGUI.Close()
            $Global:DismParams = @{
                Dism = '/Apply-FFU'
                ApplyDrive = $Global:DiskImageGUI.PhysicalDrive
                ImageFile = $DismDestinationCombobox.Text
            }
    
            Get-OSDPower -Property High
            Start-Process PowerShell.exe -Wait -WorkingDirectory $ParentDirectory -ArgumentList '-NoExit','-NoLogo','Dism.exe','/Apply-FFU',"/ApplyDrive:'$($Global:DismParams.ApplyDrive)'","/ImageFile:'$($Global:DismParams.ImageFile)'"
            Get-OSDPower -Property Balanced
        }
    }
    #================================================
    # /Capture-FFU
    #================================================
    if ($DismActionCombobox.SelectedValue -eq '/Capture-FFU') {
        $ParentDirectory = Split-Path $DismDestinationCombobox.Text -Parent -ErrorAction Stop
        New-Item -Path $ParentDirectory -ItemType Directory -Force -ErrorAction Ignore | Out-Null

        if (Test-Path $DismDestinationCombobox.Text) {
            Write-Warning "ImageFile already exists. Rename the ImageFile and try again"
        }
        elseif (!(Test-Path "$ParentDirectory")) {
            Write-Warning "ImageFile appears to be in a Read Only directory or some junk path. Try another Path"
        }
        elseif (!(Test-InWinPE)) {
            Write-Warning "Dism.exe /Capture-FFU requires WinPE so nothing is going to happen here"
        }
        else {
            $xamGUI.Close()
            $Global:DismParams = @{
                Dism = '/Capture-FFU'
                CaptureDrive = $Global:DiskImageGUI.PhysicalDrive
                ImageFile = $DismDestinationCombobox.Text
                Name = $DismNameTextbox.Text
                Description = $DismDescriptionTextbox.Text
                Compress = $DismCompressCombobox.SelectedValue
            }
    
            Get-OSDPower -Property High
            Start-Process PowerShell.exe -Wait -WorkingDirectory $ParentDirectory -ArgumentList '-NoExit','-NoLogo','Dism.exe','/Capture-FFU',"/CaptureDrive:'$($Global:DismParams.CaptureDrive)'","/ImageFile:'$($Global:DismParams.ImageFile)'","/Name:'$($Global:DismParams.Name)'","/Description:'$($Global:DismParams.Description)'","/Compress:$($Global:DismParams.Compress)"
            Get-OSDPower -Property Balanced
            
            if (Test-Path $DismDestinationCombobox.Text) {
                Get-WindowsImage -ImagePath $DismDestinationCombobox.Text -ErrorAction Ignore
            }
        }
    }
})
#================================================
# ShowDialog
#================================================
$xamGUI.ShowDialog() | Out-Null
#================================================