Projects/ApplyFFU.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 |
#======================================================================= # 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-ApplyFFU" #======================================================================= # 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 'ApplyFFU.xaml') #======================================================================= # Title #======================================================================= $TitleLabel.Content = 'Start-ApplyFFU' #======================================================================= # ApplyDrives #======================================================================= # Create empty array of hard disk numbers $Global:ArrayOfDiskNumbers = @() #Get all the ApplyDrives $Global:ApplyDrives = Get-Disk.fixed | Where-Object {$_.IsBoot -eq $false} # Populate the ComboBox $Global:ApplyDrives | foreach { $ApplyDriveComboBox.Items.Add("Disk $($_.DiskNumber) $($_.BusType) $($_.MediaType) - $($_.FriendlyName)") | Out-Null $Global:ArrayOfDiskNumbers += $_.Number } $ApplyDriveDetails.Content = '' #Select the first item $ApplyDriveComboBox.SelectedIndex = 0 #======================================================================= # Set-ApplyDriveComboBox #======================================================================= function Set-ApplyDriveComboBox { $ApplyDrive = Get-Disk.fixed | Where-Object { $_.Number -eq $Global:ArrayOfDiskNumbers[$ApplyDriveComboBox.SelectedIndex] } # Work out if the size should be in GB or TB if ([math]::Round(($ApplyDrive.Size/1TB),2) -lt 1) { $ApplyDriveSize = "$([math]::Round(($ApplyDrive.Size/1000000000),0))GB" } else { $ApplyDriveSize = "$([math]::Round(($ApplyDrive.Size/1000000000000),2))TB" } $Global:DiskNumber = $ApplyDrive.DiskNumber $Global:DismApplyDrive = "\\.\PhysicalDrive$($Global:DiskNumber)" Write-Host -ForegroundColor Cyan "ApplyDrive: $Global:DismApplyDrive" $Global:DismName = "disk$($Global:DiskNumber)" Write-Host -ForegroundColor Cyan "Name: $Global:DismName" $ApplyDriveDetails.Content = @" $ApplyDriveSize $($ApplyDrive.PartitionStyle) $($ApplyDrive.NumberOfPartitions) Partitions "@ } #======================================================================= # Set-ImageFileComboBox #======================================================================= function Set-ImageFileComboBox { $Global:DestinationDrives = @() $Global:DestinationDrives = Get-Disk.storage | Where-Object {$_.DiskNumber -ne $Global:DiskNumber} $ImageFileComboBox.Items.Clear() $ImageFiles = @() if (-NOT ($Global:DestinationDrives)) { $ImageFileComboBox.IsEnabled = "False" } else { foreach ($DestinationDrive in $Global:DestinationDrives) { if (Test-Path "$($DestinationDrive.DriveLetter):\CaptureFFU") { $ImageFiles += Get-ChildItem "$($DestinationDrive.DriveLetter):\CaptureFFU" -Include *.ffu -File -Recurse -Force -ErrorAction Ignore | Select-Object -ExpandProperty FullName } } if ($ImageFiles) { foreach ($Item in $ImageFiles) { $ImageFileComboBox.Items.Add($Item) | 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 /Apply-FFU /ImageFile=`"$Global:DismImageFile`" /ApplyDrive=$Global:DismApplyDrive" $ImageFileComboBox.IsEnabled = "True" } Set-ApplyDriveComboBox Set-ImageFileComboBox Set-DismCommandText <# $DestinationDetails.Content = @" DiskNumber: $($DestinationDrive.DiskNumber) FileSystemLabel: $($DestinationDrive.FileSystemLabel) FileSystem: $($DestinationDrive.FileSystem) Size: $($DestinationDrive.Size) SizeRemaining: $($DestinationDrive.SizeRemaining) "@ #> #======================================================================= # ApplyDriveComboBox Events #======================================================================= $ApplyDriveComboBox.add_SelectionChanged({ Set-ApplyDriveComboBox Set-ImageFileComboBox Set-DismCommandText }) $ApplyDriveComboBox.add_DropDownClosed({ Set-ApplyDriveComboBox 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 /Apply-FFU /ImageFile=`"$Global:DismImageFile`" /ApplyDrive=$Global:DismApplyDrive" $xamGUI.Close() Show-Powershell #======================================================================= # Checks #======================================================================= if (-NOT (Test-InWinPE)) { Write-Warning "ApplyFFU must be run in WinPE" Break } #======================================================================= # Create FFU #======================================================================= $CommandLine = "Dism.exe /Apply-FFU /ImageFile=`"$Global:DismImageFile`" /ApplyDrive=$Global:DismApplyDrive" Write-Host "CommandLine: $CommandLine" Get-OSDPower -Property High Start-Process PowerShell.exe -Wait -WorkingDirectory $ParentDirectory -ArgumentList '-NoExit','-NoLogo','Dism.exe','/Apply-FFU',"/ImageFile='$Global:DismImageFile'","/ApplyDrive='$Global:DismApplyDrive'" Get-OSDPower -Property Balanced if (Test-Path $Global:DismImageFile) { Get-WindowsImage -ImagePath $Global:DismImageFile } #======================================================================= } }) #======================================================================= # ShowDialog #======================================================================= $xamGUI.ShowDialog() | Out-Null #======================================================================= |