Projects/CaptureFFU.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 |
#======================================================================= # Clear Screen #======================================================================= #Clear-Host #======================================================================= # PowershellWindow Functions # Hide the PowerShell Window # https://community.spiceworks.com/topic/1710213-hide-a-powershell-console-window-when-running-a-script #======================================================================= $Script:showWindowAsync = Add-Type -MemberDefinition @" [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); "@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru function Show-Powershell() { $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 10) } function Hide-Powershell() { $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 2) } #Hide-Powershell #======================================================================= # MahApps.Metro #======================================================================= # 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\System.Windows.Interactivity.dll") | Out-Null [System.Reflection.Assembly]::LoadFrom("$Global:MyScriptDir\assembly\MahApps.Metro.dll") | Out-Null #======================================================================= # Console Title #======================================================================= #$host.ui.RawUI.WindowTitle = "Start-CaptureFFU" #======================================================================= # Test-InWinPE #======================================================================= function Test-InWinPE { return Test-Path -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlset\Control\MiniNT } #======================================================================= # LoadForm #======================================================================= function LoadForm { [CmdletBinding()] param( [Parameter(Mandatory = $True, 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 #======================================================================= LoadForm -XamlPath (Join-Path $Global:MyScriptDir 'CaptureFFU.xaml') #======================================================================= # Title #======================================================================= $TitleLabel.Content = 'Start-CaptureFFU' #======================================================================= # Variables #======================================================================= $Global:Manufacturer = Get-MyComputerManufacturer -Brief $Global:Model = Get-MyComputerModel -Brief $Global:SerialNumber = Get-MyBiosSerialNumber -Brief $Global:DismDescription = "$Global:Manufacturer $Global:Model $Global:SerialNumber" Write-Host -ForegroundColor Cyan "Description:$Global:DismDescription" $Global:DismCompress = 'Default' Write-Host -ForegroundColor Cyan "Compress:$Global:DismCompress" #======================================================================= # Title #======================================================================= #$TitleLabel.Content = 'CaptureFFU' #======================================================================= # CaptureDrives #======================================================================= # Create empty array of hard disk numbers $Global:ArrayOfDiskNumbers = @() #Get all the CaptureDrives $Global:CaptureDrives = Get-Disk.fixed | Where-Object {$_.IsBoot -eq $false} # Populate the ComboBox $Global:CaptureDrives | foreach { $CaptureDriveComboBox.Items.Add("Disk $($_.DiskNumber) $($_.BusType) $($_.MediaType) - $($_.FriendlyName)") | Out-Null $Global:ArrayOfDiskNumbers += $_.Number } $CaptureDriveDetails.Content = '' #Select the first item $CaptureDriveComboBox.SelectedIndex = 0 #======================================================================= # Set-CaptureDriveComboBox #======================================================================= function Set-CaptureDriveComboBox { $CaptureDrive = Get-Disk.fixed | Where-Object { $_.Number -eq $Global:ArrayOfDiskNumbers[$CaptureDriveComboBox.SelectedIndex] } # Work out if the size should be in GB or TB if ([math]::Round(($CaptureDrive.Size/1TB),2) -lt 1) { $CaptureDriveSize = "$([math]::Round(($CaptureDrive.Size/1000000000),0))GB" } else { $CaptureDriveSize = "$([math]::Round(($CaptureDrive.Size/1000000000000),2))TB" } $Global:DiskNumber = $CaptureDrive.DiskNumber $Global:DismCaptureDrive = "\\.\PhysicalDrive$($Global:DiskNumber)" Write-Host -ForegroundColor Cyan "CaptureDrive: $Global:DismCaptureDrive" $Global:DismName = "disk$($Global:DiskNumber)" Write-Host -ForegroundColor Cyan "Name: $Global:DismName" $CaptureDriveDetails.Content = @" $CaptureDriveSize $($CaptureDrive.PartitionStyle) $($CaptureDrive.NumberOfPartitions) Partitions "@ } #======================================================================= # Set-ImageFileComboBox #======================================================================= function Set-ImageFileComboBox { $Global:DestinationDrives = @() $Global:DestinationDrives = Get-Disk.storage | Where-Object {$_.DiskNumber -ne $Global:DiskNumber} $ImageFile = $null $ImageFileComboBox.Items.Clear() if (-NOT ($Global:DestinationDrives)) { $ImageFileComboBox.IsEnabled = "False" $RunButton.IsEnabled = "False" } else { $RunButton.IsEnabled = "True" foreach ($DestinationDrive in $Global:DestinationDrives) { if ($DestinationDrive.DriveLetter -gt 0) { $ImageFileName = "disk$Global:DiskNumber" $ImageFile = "$($DestinationDrive.DriveLetter):\CaptureFFU\$Global:Manufacturer\$Global:Model\$($Global:SerialNumber)_$($ImageFileName).ffu" $ImageFileComboBox.Items.Add($ImageFile) | Out-Null } } $ImageFileComboBox.SelectedIndex = 0 } } #======================================================================= # Set-DismCommandText #======================================================================= function Set-DismCommandText { #Write-Host "Text: $($ImageFileComboBox.Text)" $Global:DismImageFile = $ImageFileComboBox.Text if ($Global:DismImageFile -gt 0) { Write-Host -ForegroundColor Cyan "ImageFile: $Global:DismImageFile" } $DismCommand.Text = "Dism.exe /Capture-FFU /ImageFile=`"$Global:DismImageFile`" /CaptureDrive=$Global:DismCaptureDrive /Name:`"$Global:DismName`" /Description:`"$Global:DismDescription`" /Compress:$Global:DismCompress" $ImageFileComboBox.IsEnabled = "True" } Set-CaptureDriveComboBox Set-ImageFileComboBox Set-DismCommandText <# $DestinationDetails.Content = @" DiskNumber: $($DestinationDrive.DiskNumber) FileSystemLabel: $($DestinationDrive.FileSystemLabel) FileSystem: $($DestinationDrive.FileSystem) Size: $($DestinationDrive.Size) SizeRemaining: $($DestinationDrive.SizeRemaining) "@ #> #======================================================================= # CaptureDriveComboBox Events #======================================================================= $CaptureDriveComboBox.add_SelectionChanged({ Set-CaptureDriveComboBox Set-ImageFileComboBox Set-DismCommandText }) $CaptureDriveComboBox.add_DropDownClosed({ Set-CaptureDriveComboBox Set-ImageFileComboBox Set-DismCommandText }) #======================================================================= # ImageFileComboBox Events #======================================================================= $ImageFileComboBox.add_SelectionChanged({ #Write-Host -ForegroundColor Magenta "add_SelectionChanged" Set-DismCommandText }) $ImageFileComboBox.add_DropDownClosed({ #Write-Host -ForegroundColor Magenta "add_DropDownClosed" Set-DismCommandText }) $ImageFileComboBox.add_IsKeyboardFocusedChanged({ #Write-Host -ForegroundColor Magenta "add_IsKeyboardFocusedChanged" Set-DismCommandText }) $ImageFileComboBox.add_IsKeyboardFocusWithinChanged({ #Write-Host -ForegroundColor Magenta "add_IsKeyboardFocusWithinChanged" Set-DismCommandText }) $ImageFileComboBox.add_KeyUp({ #Write-Host -ForegroundColor Magenta "add_KeyUp" Set-DismCommandText }) #======================================================================= # RunButton #======================================================================= $RunButton.add_Click({ if ($null -eq $Global:DismImageFile) { Write-Warning "DismImageFile value is null" } elseif ($Global:DismImageFile -eq '') { Write-Warning "DismImageFile value is nothing" } else { $ParentDirectory = Split-Path $Global:DismImageFile -Parent -ErrorAction Stop Write-Host "Dism.exe /Capture-FFU /ImageFile=`"$Global:DismImageFile`" /CaptureDrive=$Global:DismCaptureDrive /Name:`"$Global:DismName`" /Description:`"$Global:DismDescription`" /Compress:$Global:DismCompress" $xamGUI.Close() Show-Powershell #======================================================================= # Checks #======================================================================= if (Test-Path $Global:DismImageFile) { Write-Warning "ImageFile already exists. Rename the ImageFile and try again"; Break } if (-NOT (Test-InWinPE)) { Write-Warning "CaptureFFU must be run in WinPE" Break } #======================================================================= # Create FFU #======================================================================= if (!(Test-Path "$ParentDirectory")) { Try {New-Item -Path $ParentDirectory -ItemType Directory -Force -ErrorAction Stop} Catch {Write-Warning "Destination appears to be Read Only. Try another Destination Drive"; Break} } $CommandLine = "Dism.exe /Capture-FFU /ImageFile=`"$Global:DismImageFile`" /CaptureDrive=$Global:DismCaptureDrive /Name:`"$Global:DismName`" /Description:`"$Global:DismDescription`" /Compress:$Global:DismCompress" Write-Host "CommandLine: $CommandLine" Get-OSDPower -Property High Start-Process PowerShell.exe -Wait -WorkingDirectory $ParentDirectory -ArgumentList '-NoExit','-NoLogo','Dism.exe','/Capture-FFU',"/ImageFile='$Global:DismImageFile'","/CaptureDrive='$Global:DismCaptureDrive'","/Name:'$Global:DismName'","/Description:'$Global:DismDescription'","/Compress:$Global:DismCompress" Get-OSDPower -Property Balanced if (Test-Path $Global:DismImageFile) { Get-WindowsImage -ImagePath $Global:DismImageFile } #======================================================================= } }) #======================================================================= # ShowDialog #======================================================================= $xamGUI.ShowDialog() | Out-Null #======================================================================= |