Skip to content

Instantly share code, notes, and snippets.

@Spartan-196
Last active May 3, 2019 13:54
Show Gist options
  • Save Spartan-196/1cf5ee189155fd5fcd767ebb6e4619a9 to your computer and use it in GitHub Desktop.
Save Spartan-196/1cf5ee189155fd5fcd767ebb6e4619a9 to your computer and use it in GitHub Desktop.
Manually Restore USMT Capture stored on SCCM migration point from Client PC
##Requirements##
# Disclaimer: Provided as is no support
# Update 1/22/18 After eventual upgrade this still works with SCCM 1710
# Update 4/24/19 Added section for extracting USMT mig file to directory
# DO NOT LOGIN TARGET USER FIRST! RUN AS SEPARATE ADMIN ACCOUNT
# 1. USMT capture must have already been ran on source computer and completed
# 2. Read permissions on SCCM server with USMT Data Store.
# 3. USMT 5 or 10 from Windows ADK https://developer.microsoft.com/en-us/windows/hardware/windows-assessment-deployment-kit
# 4. Know how SCCM is reading your user accounts. My environment is not able to use the new UPN setup for user resolution so I have to use the SAM account names also known as the "pre-windows 2000" name in AD
# 5. This script is written as though it is placed alongside the loadstate.exe file in the USMT required files.
# 6. If running from a network location I use a batch file to start this PowerShell so I can leave the execution policy set as restricted on all machines, my batch file sits in the USMT Required files next to this PowerShell file.
#
# Contents of batch file:
# @echo off
# powershell.exe -executionpolicy bypass -file "%~dp0ManualSCCMusmtRestore.ps1"
# echo "Migration complete. REBOOT!"
# Pause
#
#Enter site server where data is stored, can be FQDN or DNS name
$SCCMSiteServer=Read-Host -Prompt "Input SCCM Server name"
#Enter Site name Space e.g. primary. This would make the name space read as root\sms\site_primary
$SCCMNameSpace=Read-Host -Prompt "Input Site Code"
$SourcePC= Read-Host -Prompt "Input Source Computer Name" #Enter computername shown in User State migration association e.g. bob-pc
$UserID= Read-Host -Prompt "Input User ID to Migrate" #Enter based on what Your SCCM enviroment uses for identifying user accounts
$SourceQuery = "Select ResourceID From SMS_R_System WHERE Active = 1 and NetbiosName = '" + $SourcePC + "'"
$SourceID = Get-WmiObject -Namespace "root\SMS\site_$SCCMNameSpace" -computer $SCCMSiteServer -Query $SourceQuery
$Query = "select * from SMS_StateMigration where SourceClientResourceID=" + $SourceID.ResourceID
$StateMigrationInfo = Get-WmiObject -Namespace "root\SMS\site_$SCCMNameSpace" -computer $SCCMSiteServer -Query $Query
$StorePath= $StateMigrationInfo.Storepath #store the location of the USMT data in variable
$keytxt= $StateMigrationInfo.GetEncryptDecryptKey().key #Store decryption key in variable
# I have located the USMT files on a network drive with this file and the batch file outlined above. I run the batchfile as an administrative account from the UNC network path
& $PSScriptRoot\loadstate.exe $StorePath /decrypt /key:"$keytxt" /ue:*\* /ui:$UserID /v:5 /c /l:"$env:ALLUSERSPROFILE\loadstate.log" /progress:"$env:ALLUSERSPROFILE\loadstateprogress.log" /i:"$PSScriptRoot\migdocs.xml" /i:"$PSScriptRoot\migapp.xml" /i:"$PSScriptRoot\miguser.xml"
# Use this Section for Extracting MIG to directory.
<#
Function Get-Folder($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder"
$foldername.rootfolder = "MyComputer"
if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}
$ExtractDir= Get-Folder
#$ExtractDir= Read-Host -prompt "Enter Directory to Extract files to"
$TargetDir="$ExtractDir`\$UserID"
& $PSScriptRoot\usmtutils.exe /extract "$StorePath\USMT\USMT.MIG" "$TargetDir" /decrypt /key:"$keytxt" /v:5 /l:"$TargetDir\Extract.log"
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment