Asked by:
GUI for extracting USMT.MIG files

Question
-
I created this GUI in PowerShell and just wanted to share. It is for extracting encrypted USMT.MIG files. You could also modify it to accept non-excrypted MIG files.
<# * This script cannot be run from within the PowerShell ISE because USMT (usmtutils.exe) must be run as Administrator. * To workaround this I have created a .lnk file that runs a .bat file that calls this .ps1 file. (I know, I know). The .bat file immediately exits after calling the .ps1 file. * The .lnk file has been set to run as "Mimized" and "Run as administrator". #> #Add Assemblies Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() #Form. $Form = New-Object System.Windows.Forms.Form $Form.ClientSize = '771,600' $Form.text = "CNB MIG Extractor" $Form.TopMost = $false #Source Computer label. $Source_Computer_Label = New-Object System.Windows.Forms.Label $Source_Computer_Label.Text = "* SOURCE COMPUTER: Enter Source Computer hostname (PQDN):" $Source_Computer_Label.AutoSize = $true $Source_Computer_Label.Width = 25 $Source_Computer_Label.Height = 10 $Source_Computer_Label.Location = New-Object System.Drawing.Point(10,10) $Source_Computer_Label.Font = 'Microsoft Sans Serif,10,style=Bold' #Source Computer text box. $Source_Computer_Text_Box = New-Object System.Windows.Forms.TextBox $Source_Computer_Text_Box.Multiline = $false $Source_Computer_Text_Box.Width = 435 $Source_Computer_Text_Box.Height = 20 $Source_Computer_Text_Box.Location = New-Object System.Drawing.Point(10,30) $Source_Computer_Text_Box.Font = 'Microsoft Sans Serif,10' #User State Store Location label. $User_State_Store_Location_Label = New-Object System.Windows.Forms.Label $User_State_Store_Location_Label.Text = "* USER STATE STORE LOCATION: Enter full path to User State Store Location (copy/paste from SCCM Console):" $User_State_Store_Location_Label.AutoSize = $true $User_State_Store_Location_Label.Width = 25 $User_State_Store_Location_Label.Height = 10 $User_State_Store_Location_Label.Location = New-Object System.Drawing.Point(10,70) $User_State_Store_Location_Label.Font = 'Microsoft Sans Serif,10,style=Bold' #User State Store Location text box. $User_State_Store_Location_Text_Box = New-Object System.Windows.Forms.TextBox $User_State_Store_Location_Text_Box.Multiline = $false $User_State_Store_Location_Text_Box.Width = 750 $User_State_Store_Location_Text_Box.Height = 20 $User_State_Store_Location_Text_Box.Location = New-Object System.Drawing.Point(10,90) $User_State_Store_Location_Text_Box.Font = 'Microsoft Sans Serif,8' #Recovery Key label. $Recovery_Key_Label = New-Object System.Windows.Forms.Label $Recovery_Key_Label.Text = "* RECOVERY KEY: Enter User State Recovery Key (copy/paste from SCCM Console):" $Recovery_Key_Label.AutoSize = $true $Recovery_Key_Label.Width = 25 $Recovery_Key_Label.Height = 10 $Recovery_Key_Label.Location = New-Object System.Drawing.Point(9,130) $Recovery_Key_Label.Font = 'Microsoft Sans Serif,10,style=Bold' #Recovery Key text box. $Recovery_Key_Text_Box = New-Object System.Windows.Forms.TextBox $Recovery_Key_Text_Box.Multiline = $false $Recovery_Key_Text_Box.Width = 750 $Recovery_Key_Text_Box.Height = 20 $Recovery_Key_Text_Box.Location = New-Object System.Drawing.Point(10,150) $Recovery_Key_Text_Box.Font = 'Microsoft Sans Serif,10' #Extraction Location label. $Extraction_Location_Label = New-Object System.Windows.Forms.Label $Extraction_Location_Label.Text = "* EXTRACTION LOCATION: Enter a location to extract to, or browse to select a location:" $Extraction_Location_Label.AutoSize = $true $Extraction_Location_Label.Width = 25 $Extraction_Location_Label.Height = 10 $Extraction_Location_Label.Location = New-Object System.Drawing.Point(10,190) $Extraction_Location_Label.Font = 'Microsoft Sans Serif,10,style=Bold' #Extraction Location text box. $Extraction_Location_Text_Box = New-Object System.Windows.Forms.TextBox $Extraction_Location_Text_Box.Multiline = $false $Extraction_Location_Text_Box.Width = 660 $Extraction_Location_Text_Box.Height = 20 $Extraction_Location_Text_Box.Location = New-Object System.Drawing.Point(10,210) $Extraction_Location_Text_Box.Font = 'Microsoft Sans Serif,10' #Example label. $Example_1_Label = New-Object System.Windows.Forms.Label $Example_1_Label.Text = "Example: C:\BARS_Extractions OR \\server\share" $Example_1_Label.AutoSize = $true $Example_1_Label.Width = 25 $Example_1_Label.Height = 10 $Example_1_Label.Location = New-Object System.Drawing.Point(15,234) $Example_1_Label.Font = 'Microsoft Sans Serif,9' #Start Extract button. $Start_Extraction_Button = New-Object System.Windows.Forms.Button $Start_Extraction_Button.BackColor = "#7ed321" $Start_Extraction_Button.Text = "Start Extraction" $Start_Extraction_Button.Width = 300 $Start_Extraction_Button.Height = 60 $Start_Extraction_Button.Location = New-Object System.Drawing.Point(10,260) $Start_Extraction_Button.Font = 'Microsoft Sans Serif,20,style=Bold' $Start_Extraction_Button.ForeColor = "" #Cancel Extraction button. $Cancel_Extraction_Button = New-Object System.Windows.Forms.Button $Cancel_Extraction_Button.BackColor = "#c22f41" $Cancel_Extraction_Button.Text = "Cancel Extraction" $Cancel_Extraction_Button.Width = 170 $Cancel_Extraction_Button.Height = 60 $Cancel_Extraction_Button.Location = New-Object System.Drawing.Point(325,260) $Cancel_Extraction_Button.Font = 'Microsoft Sans Serif,14,style=Bold' $Cancel_Extraction_Button.ForeColor = "" #Browse button. $Browse_Button = New-Object System.Windows.Forms.Button $Browse_Button.Text = "Browse..." $Browse_Button.Width = 85 $Browse_Button.Height = 25 $Browse_Button.Location = New-Object System.Drawing.Point(675,209) $Browse_Button.Font = 'Microsoft Sans Serif,10,style=Bold' #Clear All button. $Clear_All_Button = New-Object System.Windows.Forms.Button $Clear_All_Button.Text = "Clear All Fields" $Clear_All_Button.Width = 90 $Clear_All_Button.Height = 50 $Clear_All_Button.Location = New-Object System.Drawing.Point(670,10) $Clear_All_Button.Font = 'Microsoft Sans Serif,12,style=Bold' #How to Use button. $How_To_Use_Button = New-Object System.Windows.Forms.Button $How_To_Use_Button.Text = "How to Use CNB MIG Extractor" $How_To_Use_Button.Width = 90 $How_To_Use_Button.Height = 50 $How_To_Use_Button.Location = New-Object System.Drawing.Point(570,10) $How_To_Use_Button.Font = 'Microsoft Sans Serif,10,style=Bold' #About button. $About_Button = New-Object System.Windows.Forms.Button $About_Button.Text = "About" $About_Button.Width = 50 $About_Button.Height = 20 $About_Button.Location = New-Object System.Drawing.Point(10,580) $About_Button.Font = 'Microsoft Sans Serif,9' #Output label. $Output_Label = New-Object System.Windows.Forms.Label $Output_Label.Text = "Output:" $Output_Label.AutoSize = $true $Output_Label.Width = 25 $Output_Label.Height = 10 $Output_Label.Location = New-Object System.Drawing.Point(10,325) $Output_Label.Font = 'Microsoft Sans Serif,10,style=Bold' #While You Wait group box. $While_You_Wait_Group_Box = New-Object System.Windows.Forms.Groupbox $While_You_Wait_Group_Box.Height = 60 $While_You_Wait_Group_Box.Width = 250 $While_You_Wait_Group_Box.Text = "Play some games while you wait" $While_You_Wait_Group_Box.Location = New-Object System.Drawing.Point(510,260) #Output text box. $Output_Text_Box = New-Object System.Windows.Forms.TextBox $Output_Text_Box.Multiline = $true $Output_Text_Box.Width = 750 $Output_Text_Box.Height = 235 $Output_Text_Box.Location = New-Object System.Drawing.Point(10,345) $Output_Text_Box.Font = 'Microsoft Sans Serif,10' $Output_Text_Box.ScrollBars = 3 #Both horizontal and vertical scroll bars. https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-display-scroll-bars-in-the-windows-forms-richtextbox-control $Output_Text_Box.WordWrap = $false #RaidenX picture box. $RaidenX_Picture_Box = New-Object system.Windows.Forms.PictureBox $RaidenX_Picture_Box.Width = 80 $RaidenX_Picture_Box.Height = 35 $RaidenX_Picture_Box.Location = New-Object System.Drawing.Point(5,17) $RaidenX_Picture_Box.ImageLocation = "$PSScriptRoot\Games\RaidenX.jpg" $RaidenX_Picture_Box.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom #Tetris picture box. $Tetris_Picture_Box = New-Object system.Windows.Forms.PictureBox $Tetris_Picture_Box.Width = 80 $Tetris_Picture_Box.Height = 35 $Tetris_Picture_Box.Location = New-Object System.Drawing.Point(80,17) $Tetris_Picture_Box.ImageLocation = "$PSScriptRoot\Games\Tetris.png" $Tetris_Picture_Box.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom #Matrix picture box. $Matrix_Picture_Box = New-Object system.Windows.Forms.PictureBox $Matrix_Picture_Box.Width = 80 $Matrix_Picture_Box.Height = 35 $Matrix_Picture_Box.Location = New-Object System.Drawing.Point(155,17) $Matrix_Picture_Box.ImageLocation = "$PSScriptRoot\Games\Matrix.png" $Matrix_Picture_Box.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::zoom #Credit label. $Credit_Label = New-Object System.Windows.Forms.Label $Credit_Label.Text = "By CamJam" $Credit_Label.AutoSize = $true $Credit_Label.Width = 25 $Credit_Label.Height = 10 $Credit_Label.Location = New-Object System.Drawing.Point(702,584) $Credit_Label.Font = 'Microsoft Sans Serif,7' #Add objects. #To Form. $Form.controls.AddRange(@( $Source_Computer_Text_Box, $Source_Computer_Label, $User_State_Store_Location_Label, $User_State_Store_Location_Text_Box, $Recovery_Key_Text_Box,$Recovery_Key_Label, $Extraction_Location_Label, $Extraction_Location_Text_Box, $Example_1_Label, $Start_Extraction_Button, $Cancel_Extraction_Button, $Browse_Button, $Clear_All_Button, $Output_Label, $While_You_Wait_Group_Box, $Output_Text_Box, #$Credit_Label, $How_To_Use_Button, $About_Button )) #To Group Boxes. $While_You_Wait_Group_Box.controls.AddRange(@( $RaidenX_Picture_Box, $Tetris_Picture_Box, $Matrix_Picture_Box )) #Add mouse click functions to buttons. $RaidenX_Picture_Box.Add_MouseClick({RaidenX_Button}) #"RaidenX" button. $Tetris_Picture_Box.Add_MouseClick({Tetris_Button}) #"Tetris" button. $Matrix_Picture_Box.Add_MouseClick({Matrix_Button}) #"Matrix" button. $Start_Extraction_Button.Add_MouseClick({Start_Extraction_Button}) #"Start Extraction" button. $Cancel_Extraction_Button.Add_MouseClick({Cancel_Extraction_Button}) #"Cancel Extraction" button. $Browse_Button.Add_MouseClick({Browse_Button}) #"Browse" button. $Clear_All_Button.Add_MouseClick({Clear_All_Button}) #"Clear All Fields" button. $How_To_Use_Button.Add_MouseClick({How_To_Use_Button}) #"How to Use CND MIG Extractor" button. $About_Button.Add_MouseClick({About_Button}) #"About" button. ############################## FUNCTIONS ############################## ########## RaidenX button ########## Function RaidenX_Button { Start-Process -FilePath "$PSScriptRoot\Games\Adobe Flash Player.exe" -ArgumentList "$PSScriptRoot\Games\RaidenX.swf" } #################################### ########## Tetris button ########## Function Tetris_Button { Start-Process -FilePath "$PSScriptRoot\Games\Adobe Flash Player.exe" -ArgumentList "$PSScriptRoot\Games\Tetris.swf" } #################################### ########## Matrix button ########## Function Matrix_Button { Start-Process -FilePath "$PSScriptRoot\Games\Adobe Flash Player.exe" -ArgumentList "$PSScriptRoot\Games\Matrix Bullet Time Fighting.swf" } #################################### ########## Matrix button ########## Function How_To_Use_Button { #Invoke-Expression “cmd.exe /C start http://gurucore.com” [Diagnostics.Process]::Start(‘<URL here‘) } #################################### ########## Clear All button ########## Function Clear_All_Button { #Set all text boxes to empty. $Source_Computer_Text_Box.Text = "" $User_State_Store_Location_Text_Box.Text = "" $Recovery_Key_Text_Box.Text = "" $Extraction_Location_Text_Box.Text = "" $Output_Text_Box.Text = "" } #################################### ########## About button ########## Function About_Button { #Pop Up Message $Buttons = 0 $Icon = 64 $Time = 0 $Message_Title = "About CNB MIG Extractor" $Message_Body = "CNB MIG Extractor is an application that is used to extract data from within encrypted USMT.MIG files to a folder." #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) } #################################### ########## Browse button ########## Function Browse_Button { #Load Assembly and truncate it. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null $Folder_Browser_Dialog = New-Object System.Windows.Forms.FolderBrowserDialog $Folder_Browser_Dialog.Description = "Select a location for USMT.MIG file extraction:" #Set Extraction_Location_Text_Box as chosen folder. If ($Folder_Browser_Dialog.ShowDialog() -eq "OK") { $Extraction_Location_Text_Box.Text = "" #Clear text box so that new text is not just appended to the path. $Selected_Folder = $Folder_Browser_Dialog.SelectedPath } $Source_Computer = $Source_Computer_Text_Box.Text $Extraction_Location_Text_Box.AppendText("$Selected_Folder")#Enter select folder path into text box. } #################################### ########## Start Extraction button ########## Function Start_Extraction_Button { #Set data entered into Text Boxes as variables $Source_Computer = $Source_Computer_Text_Box.Text $User_State_Store_Location = $User_State_Store_Location_Text_Box.Text $Recovery_Key = $Recovery_Key_Text_Box.Text $Extraction_Location = $Extraction_Location_Text_Box.Text write-host "$Extraction_Location" ###### If mandatory fields do not contain data ##### If ( ($Source_Computer -eq "") -or ($User_State_Store_Location -eq "") -or ($Recovery_Key -eq "") -or ($Extraction_Location -eq "") ) { #Pop Up Message $Buttons = 0 $Icon = 48 $Time = 0 $Message_Title = "MANDATORY DATA REQUIRED" $Message_Body = "Mandatory fields are missing data. Check that you have entered data into the fields marked with an asterisk (*)." #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) } ###### If invalid User State Store Location ##### ElseIf (($User_State_Store_Location -notlike "*:\*") -and ($User_State_Store_Location -notlike "\\*")) { #Pop Up Message $Buttons = 0 $Icon = 48 $Time = 0 $Message_Title = "INVALID USER STATE STORE PATH" $Message_Body = "You have entered an invalid or incorrect path to the User State Store location. - If the USMT.MIG file is located on your State Migration Point server, then you must use the 'User state store location' from the 'User State Recovery Information' in SCCM. - If the USMT.MIG file has been moved to a Network share or a local drive, then you must enter this. (e.g. C:\... or \\server\share\...)" #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) } ###### If invalid Extraction Location ##### ElseIf (($Extraction_Location -notlike "*:\*") -and ($Extraction_Location -notlike "\\*")) { #Pop Up Message $Buttons = 0 $Icon = 48 $Time = 0 $Message_Title = "INVALID EXTRACTION LOCATION" $Message_Body = "You have entered an invalid Extraction Location. Must be a local path (e.g. C:\...) or a Network path (e.g. \\server\share\...)" #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) } ###### If Extraction Location already exists ##### ElseIf (Test-Path -Path "$Extraction_Location\*") { #Pop Up Message $Buttons = 0 $Icon = 48 $Time = 0 $Message_Title = "FILES EXIST IN EXTRACTIOB LOCATION" $Message_Body = "There are existing files in the Extraction Location you have specified. Either delete the files in this location, or specify a different path for this extraction." #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) } ###### Perform extraction ##### Else { Function USMT_Extraction { $Date = Get-Date -UFormat "%r %d/%m/%Y" #Output to Output_Text_Box #`r`n -this means return to next line. $Output_Text_Box.AppendText("`r`n") $Output_Text_Box.AppendText("______________________________________________________________________________________________________`r`n") $Output_Text_Box.AppendText("Started: $Date`r`n") $Output_Text_Box.AppendText("`r`n") $Output_Text_Box.AppendText("EXTRACTION DETAILS ENTERED:`r`n") $Output_Text_Box.AppendText(" Source Computer: $Source_Computer`r`n") $Output_Text_Box.AppendText(" Store Location: $User_State_Store_Location`r`n") $Output_Text_Box.AppendText(" Recovery Key: $Recovery_Key`r`n") $Output_Text_Box.AppendText(" Extraction Location: $Extraction_Location`r`n") $Output_Text_Box.AppendText("`r`n") $Output_Text_Box.AppendText("Starting in...`r`n") Start-Sleep -Seconds 2 $Output_Text_Box.AppendText("3 Seconds...`r`n") Start-Sleep -Seconds 1 $Output_Text_Box.AppendText("2 Seconds...`r`n") Start-Sleep -Seconds 1 $Output_Text_Box.AppendText("1 Second...`r`n") Start-Sleep -Seconds 2 $Output_Text_Box.AppendText("`r`n") $Output_Text_Box.AppendText("Extraction started...`r`n") #Extract USMT.MIG file Start-Process -FilePath "$PSScriptRoot\usmtutils\usmtutils.exe" -ArgumentList "/extract `"$User_State_Store_Location`" `"$Extraction_Location`" /decrypt /key:`"$Recovery_Key`" /l:`"$Extraction_Location\Extraction Log.log`"" <#Possible useful loop...one day $Test = Get-Process -Name "usmtutils" | Select-Object -ExpandProperty ProcessName While($true) { If ($Test -eq "usmtutils") { Start-Sleep -Seconds 3 $Output_Text_Box.AppendText("Still extracting`r`n") } Else { $Date = Get-Date -UFormat "%r %d/%m/%Y" $Output_Text_Box.AppendText("Finish: $Date`r`n") } } #> } #If $User_State_Store_Location does not contain USMT.MIG file, Test-Path until USMT.MIG file is found and set $User_State_Store_Location as this folder path. If (Test-Path -Path "$User_State_Store_Location\USMT.MIG") { $User_State_Store_Location = "$User_State_Store_Location\USMT.MIG" USMT_Extraction } ElseIf (Test-Path -Path "$User_State_Store_Location\USMT\USMT.MIG") { $User_State_Store_Location = "$User_State_Store_Location\USMT\USMT.MIG" USMT_Extraction } #Output after extraction (whether or not it was successful or failed). $Output_Text_Box.AppendText("`r`n") $Output_Text_Box.AppendText("When the Command Prompt window disappears, this means that the extraction has ended.`r`n") $Output_Text_Box.AppendText("Check the Extraction Log file for any failures.`r`n") $Output_Text_Box.AppendText(" Log: `"$Extraction_Location\Extraction Log.log`"`r`n") } } #################################### ########## Cancel Extraction button ########## #Script must be run as Administrator in order for this function to work, otherwise Access Denied errors will occur. Function Cancel_Extraction_Button { #Stop the extraction process $Process = "usmtutils" #USMT $Running_Process = Get-Process -Name "$Process" | Select-Object -ExpandProperty "ProcessName" If ($Running_Process -like "$Process") { Stop-Process -Name "$Process" -Force #Delete the partially complete extraction $Extraction_Location = $Extraction_Location_Text_Box.Text Remove-Item -Path "$Extraction_Location" -Recurse -Force #####Pop Up Message: Cancelled extraction##### $Buttons = 0 $Icon = 48 $Time = 0 $Message_Title = "EXTRACTION CANCELLED" $Message_Body = "The extraction was cancelled. The partially completed extraction location has been deleted." #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) #Output to Output_Text_Box $Output_Text_Box.AppendText("`r`n") $Output_Text_Box.AppendText("You cancelled the extraction!`r `n") } Else { #####Pop Up Message: No extraction is running##### $Buttons = 0 $Icon = 16 $Time = 0 $Message_Title = "NO EXTRACTION RUNNING" $Message_Body = "No extraction was cancelled because no extraction was in progress." #Create COM object $wshell = New-Object -ComObject Wscript.Shell #Launch pop-up message and catch which button was clicked $wshell.Popup($Message_Body,$Time,$Message_Title,$Buttons+$Icon) } } #################################### [void]$Form.ShowDialog()
- Edited by trigga4ever Monday, April 23, 2018 1:30 AM
- Moved by Bill_Stewart Friday, July 27, 2018 6:09 PM This is not "debug/fix/rewrite my script for me" forum
Monday, April 23, 2018 12:52 AM
All replies
-
Please post shared files here: Gallery
This forum is not a file sharing forum.
When posting code in this forum please use the code posting tool provided on the edit bar.
\_(ツ)_/
Monday, April 23, 2018 1:04 AM