0

Is my understanding correct about this?

USMT copies user data, settings, ect... off to a network drive and then you install the operating system, then you use USMT again to copy the files, settings, ect... back.

1 Answer 1

1

If you want the short, easy, boiled-down version yes USMT is "just" a backup tool. I'm fairly new with it, but when scripted, it can become a valuable tool for the admin. Hope this helps!

The following, which is a batch file, automates the loadsate portion of USMT. I do not have the code in place for the array_compare.ps1, but if anyone is really interested I can post it. Have fun and let me know if you have any questions!

Checking to see if the script is running in an elevated state and if not, elevates itself.

:::::::::::::::::::::::::::::::::::::::::
::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
: checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )  
:getPrivileges 
if '%1'=='ELEV' 
(shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO ************************************** 
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B  
:gotPrivileges 
::::::::::::::::::::::::::::
::START::::::::::::::::::::::::::::
setlocal & pushd . 

REM Run shell as admin (example)
:::::::::::::::::::::::::::: 

Mapping a network drive that I used to store and maintain miscellaneous files that are needed. It also stored the USMT files to be loaded in a bit.

@ECHO OFF
START /wait NET USE z: \\NETWORK SHARE\FOLDER\FOLDER
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Lines 42 - 44 Run the array_compare.ps1 as Admin      ::
:: to run a search to find the new user name and output  ::
:: it to a file that we will read from shortly.          ::
:: THIS CAN BE PROVIDED...LET ME KNOW!!                  ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

The array_compare.ps1, takes two lists of usernames and compares them, outputs the to a csv file that will be read to import the profiles.

SET ThisScriptsDirectory=%~dp0 
SET PowerShellScriptPath="Z:\Path\to\powershell\comparison\array_compare.ps1"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '->NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::  Change the Powershell ExecutionPolicy so that scripts will run without issue. ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted'}"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::      ***Declaring our variables***               ::
:: Location is the folder name where we stored the scanstate information.  ::
:: Domain2 is the new domain that we are migrating to.                     ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Scanstate saves the files on a network share in a folder named after the host. Here we are prompted to input the hostname of the device that is being replaced.

set /P location= Please enter the OLD computer name:

Since USMT has two different .exe's based on the bit level of the OS, Win 7x86 vs Win 7x64, I wanted to run a check and based off of that, execute the proper loadstate.

Interrogating the processor to determine the architecture type.

:CheckOS

   if /i %processor_architecture%==AMD64 GOTO AMD64
Else    
if /i %processor_architecture%==x86 GOTO x86
Else
if /i %PROCESSOR_ARCHITEW6432%==AMD64 GOTO AMD64

 :AMD64
Z:
CD Z:\Program Files\USMT\x64\
xcopy *.* /e /v /y c:\windows\usmt\

C:
CD c:\windows\usmt\

setlocal EnableDelayedExpansion



Set inputfile=Z:\%location%\NAME_OF_FILE_THAT_CONTAINS_BOTH_USERIDS.txt

for /f "tokens=1-2" %%A in (%inputfile%) do call :USMTx64 %%A%%B

:USMTx64
ECHO Syncing %1 ...
loadstate Z:\%location% /c /lac /lae /i:miguser.xml /i:migapp.xml /i:novell_excl.xml /config:config.xml /ue:Administrator /ue:Admin* /ue:EO-* /ue:*\* /ui:%1 /mu:<OLDDOMAIN>\%1:ID\%2 /mu:%1:ID\%2 /v:13 

if NOT ["%errorlevel%"]==["0"] (
    GOTO END        
    )
exit /b

:x86
Z:
CD Z:\Program Files\USMT\x86
xcopy *.* /e /v /y c:\windows\usmt\

C:
CD c:\windows\usmt\

Set inputfile=Z:\%location%\NAME_OF_FILE_THAT_CONTAINS_BOTH_USERIDS.txt

Created a for /f loop that reads the output file from the array_compare.ps1 from earlier. This is what really automates USMT for us. It will go through X number of times, each time loading a new user.

for /f "tokens=1-2" %%A in (%inputfile%) do call :USMTx86 %%A%%B
:USMTx86
ECHO Syncing %1 ...
loadstate Z:\%location% /c /lac /lae /i:miguser.xml /i:migapp.xml /i:novell_excl.xml /config:config.xml /ue:Administrator /ue:EO-* /ue:Admin* /ui:%1 /ue:*\* /mu:<OLDDOMAIN>\%1:ID\%2 /mu:%1:ID\%2 /v:13 

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::    :::::::::::: 
:: Checks for errors in the USMT commands.  
:: If found exits the CALL sub and continues with the script.
::::  SOURCE: https://coderwall.com/p/jexjlw
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

This error loop was absolutely necessary, well at least for me! When the for /f gets to the last iteration, USMT will error out. If that happens and there are still commands to run, the batch file will fail. This handles it, exits the for /f loop and continues on.

if NOT ["%errorlevel%"]==["0"] (
    GOTO END
    )
exit /b

:END

:: Deletes the mapped network drive

net use z: /delete /yes

Exit
5
  • Is this more of what is expected? Still learning the ropes on this site. Thought I give a little since it gave/gives a lot!
    – Dave
    Aug 28, 2014 at 20:12
  • Hi Dave, that looks much better! Because the code is not commented for everything it does (I'd say only about half), could you explain as well what this script does? It looks much better though, and is able to be upvoted and used by other people looking for help! Thanks! Aug 28, 2014 at 20:13
  • @CanadianLuke Thanks for the advice. Made a couple of changes.
    – Dave
    Aug 28, 2014 at 20:36
  • It's excellent, Dave! And welcome to super user, we hope you stick around Aug 28, 2014 at 20:42
  • I forgot to mention that this was a migration to a new machine and moving the user from one domain to another. The user had an account in both domains, which made the migration....interesting.
    – Dave
    Aug 29, 2014 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .