USMT: script for both scanstate and loadstate in VBScript
(A little thanks for you is a big gift for me)
this script in compatible with both cscript and deployment TaskSequence :
On Error Resume Next
strComputer = "."
const HKEY_LOCAL_MACHINE = &H80000002
Const TIMEOUT = 3
' Detect and configure execution environment variables
Dim operatingSystemArchitecture, Wshshell, oEnvironment, oArchi
Dim ScanCommand, LoadCommand, UsmtCommand, Retour, message, Today
Dim ThisFolder, oArgs, usertomigrate, vcomputername, objWMIService, objQrySetting, objOS
Dim OSValue, oFso, SpecificsConfigurationFile, USMTConfigurationFile
Dim Configfolder, BinFolder, LogsFolder, StateStorePath, LotusFolderAclFile
Set oFso = CreateObject("Scripting.FileSystemObject")
Set UsmtCommand = WScript.CreateObject("WScript.Shell")
Set oArgs=WScript.Arguments
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
' Get the local environment
Set Wshshell = CreateObject("WScript.Shell")
Set oEnvironment = Wshshell.Environment("PROCESS")
Today=split(date,"/")(0)& "_" &split(date,"/")(1)& "_" &split(date,"/")(2)
ThisFolder = left(WScript.ScriptFullName,len(WScript.ScriptFullName)-len(WScript.ScriptName)-1)
‘Retrieve argument
if isnull(oArgs(1))=true or oArgs(1)="" then
if oArgs(0) = "scanstate" then
vcomputername = oEnvironment("COMPUTERNAME")
else
msgbox "Please give the source computername, this script will quit"
wscript.Quit
end if
elseif oArgs(0) = "loadstate" then
vcomputername = oArgs(1)
usertomigrate = oArgs(2)
elseif isnull(oArgs(0))=true or oArgs(0)="" then
msgbox "Please give the command to run: scanstate or loadstate, this script will quit"
wscript.Quit
end if
' Detect OS Version between XP or 7
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set objQrySetting = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOS In objQrySetting
If instr(lcase(objOS.Name),lcase("Windows XP")) > 0 Then
OSValue="XP"
ElseIf instr(lcase(objOS.Name),lcase("Windows 7")) > 0 Then
OSValue="WIN7"
Else
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objQrySetting = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
Dim objOSs
For Each objOSs In objQrySetting
if instr(lcase(objOSs.Name),lcase("Windows XP")) > 0 Then
OSValue="XP"
ElseIf instr(lcase(objOSs.Name),lcase("Windows 7")) > 0 Then
OSValue="WIN7"
Else
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objItem in colItems
if instr(lcase(objItem.Name),lcase("Windows XP")) > 0 Then
OSValue="XP"
ElseIf instr(lcase(objItem.Name),lcase("Windows 7")) > 0 Then
OSValue="WIN7"
End If
Next
End if
Next
End If
Next
' Detect OS Architecture and fill in the oArchi variable to be used to find USMT binaries
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strValueName = "PROCESSOR_ARCHITECTURE"
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,operatingSystemArchitecture
If operatingSystemArchitecture = "AMD64" Then
oArchi = "amd64"
Else
oArchi = "x86"
End If
' Analyse Folders path
Configfolder = ThisFolder & "\config"
BinFolder = ThisFolder & "\" & oArchi
LogsFolder = ThisFolder & "\logs"
StateStorePath = ThisFolder & "\ComputersStates"
SpecificsConfigurationFile = Configfolder & "\config.xml"
USMTConfigurationFile = Configfolder & "\" & OSValue & ".xml"
' Check if configuration files exist
If Not oFso.FileExists(SpecificsConfigurationFile) Then
' Define USMT usefull commands without specific configurations
ScanCommand = "scanstate.exe /o " & chr(34) & StateStorePath & chr(34) & "\%computername%" _
& " /ue:%computername%\*" & _
& " /i:MigUser.xml /config:" & chr(34) & USMTConfigurationFile & chr(34) _
& " /v:13 /l:" & chr(34) & LogsFolder & "\USMT_scanstate_%computername%.log" & chr(34)
'If user to migrate is not given all user profil will be migrate
if isnull(usertomigrate)=true or usertomigrate="" then
LoadCommand = "%comspec% /c loadstate.exe " & chr(34) & StateStorePath & chr(34) & "\" & vcomputername _
& " /lac /c" _
& " /i:MigUser.xml /config:" & chr(34) & USMTConfigurationFile & chr(34) _
& " /v:13 /l:" & chr(34) & LogsFolder & "\USMT_loadstate_" & vcomputername & "_" & Today & ".log" & chr(34)
else
'If user to migrate is given only this user profil will be migrate
LoadCommand = "%comspec% /c loadstate.exe " & chr(34) & StateStorePath & chr(34) & "\" & vcomputername _
& " /lac /c /ue:* /ui:"&usertomigrate _
& " /i:MigUser.xml /config:" & chr(34) & USMTConfigurationFile & chr(34) _
& " /v:13 /l:" & chr(34) & LogsFolder & "\USMT_loadstate_" & vcomputername & "_" & Today & ".log" & chr(34)
end if
else
' Define USMT usefull commands with specific configurations
ScanCommand = "scanstate.exe /o " & chr(34) & StateStorePath & chr(34) & "\%computername%" _
& " /ue:%computername%\* /i:" & chr(34) & SpecificsConfigurationFile & chr(34) _
& " /i:MigUser.xml /config:" & chr(34) & USMTConfigurationFile & chr(34) _
& " /v:13 /l:" & chr(34) & LogsFolder & "\USMT_scanstate_%computername%.log" & chr(34)
'If user to migrate is not given all user profil will be migrate
if isnull(usertomigrate)=true or usertomigrate="" then
LoadCommand = "%comspec% /c loadstate.exe " & chr(34) & StateStorePath & chr(34) & "\" & vcomputername _
& " /lac /c" _
& " /i:" & chr(34) & SpecificsConfigurationFile & chr(34) _
& " /i:MigUser.xml /config:" & chr(34) & USMTConfigurationFile & chr(34) _
& " /v:13 /l:" & chr(34) & LogsFolder & "\USMT_loadstate_" & vcomputername & "_" & Today & ".log" & chr(34)
else
'If user to migrate is given only this user profil will be migrate
LoadCommand = "%comspec% /c loadstate.exe " & chr(34) & StateStorePath & chr(34) & "\" & vcomputername _
& " /lac /c /ue:* /ui:"&usertomigrate _
& " /i:" & chr(34) & SpecificsConfigurationFile & chr(34) _
& " /i:MigUser.xml /config:" & chr(34) & USMTConfigurationFile & chr(34) _
& " /v:13 /l:" & chr(34) & LogsFolder & "\USMT_loadstate_" & vcomputername & "_" & Today & ".log" & chr(34)
end if
End If
' Execute the required USMT command
UsmtCommand.CurrentThisFolder = BinFolder
if oArgs(0) = "scan" then
UsmtCommand.popup "Running Backup of " & vcomputername, TIMEOUT
Retour = UsmtCommand.Run(ScanCommand, 1, true)
message = "Backup of " & vcomputername & " success" & vbclrf _
& "For the load state, run the commandline: usmt.vbs " & vcomputername & " " & chr(34) & "the username to restore" & chr(34)
elseif oArgs(0) = "load" then
if isnull(usertomigrate)=true or usertomigrate="" then
UsmtCommand.popup "Running Restore of " & vcomputername, TIMEOUT
else
UsmtCommand.popup "Running Restore of " & usertomigrate, TIMEOUT
end if
'running the loadstate
Retour = UsmtCommand.Run(LoadCommand, 1, true)
message = "Restore of " & vcomputername & " success"
end if
'Analysis Error Retour:
If Retour = 0 Then
wscript.Echo message
else
wscript.Echo "There was an error " & Retour & ". Please refert to your technical support"
End if
wscript.quit
This script make the scanstate and the loadstate for you.
Run in command line: “thiscript.vbs scan” and it will scan the no local computer profiles for you
Run in command line: “thisscript.vbs load theuser” and it will load the no local computer user you want to restore.
Xml configurations files coming soon
The following table lists each return code by numeric value, along with the associated error messages and suggested troubleshooting actions:
Commenter cet article