A Custom vbScript to Restore and Capture User Data using USMT 4.

By | January 31, 2014
Share

The reason for a custom script, is that the customer wanted to see a prompt displaying how large the backup .MIG file would be before initiating the backup.

The script was used in an SCCM 2012/MDT 2012U1 UDI Task Sequence.  I kicked off the script using a “Run a Command Line” step and used the “ServiceUI.exe” application, because the script displays a prompt that needs to be visible to the user.  The prompt displays an estimate on how large the .MIG file will be, if the file is too large, the deployment technician can skip the User Capture.

The process is split in 2 scripts (CaptureUserState.vbs and RestoreUserState.vbs):
 CaptureUserState.vbs:

'==========================================================================
' NAME: CaptureUserState.vbs
'
' AUTHOR: Brian Gonzalez
' DATE  : 12.11.2013
'
' Changlog:
' 12.11.13 - First revision
'
' PURPOSE: User State Capture Script.
'==========================================================================
'On Error Resume Next
'Setup Objects, Constants and Variables.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("WScript.Shell")
Set oNetwork = WScript.CreateObject("WScript.Network")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
sScriptFolder = oFSO.GetParentFolderName(Wscript.ScriptFullName) 'NoTrailingBS
sScriptVerNumber = "1.3"

'Network Share Variables
sDriveLetter = "U:"
sShare = "\\<ServerShare>\<ShareFolder>"
sUser = "<UserName>"
sPass = "<Password>"

'Close SCCM Progress UI
Set oTSProgressUI = CreateObject("Microsoft.SMS.TSProgressUI")
Set oEnvironment = CreateObject("Microsoft.SMS.TSEnvironment")
oTSProgressUI.CloseProgressDialog()

sCompName = oEnvironment("OSDComputerName")
Set oLogFile = oFSO.CreateTextFile("X:\Windows\Temp\SMSTSLog\CaptureUserState.log", ForWriting, True)
oLogFile.WriteLine Time & ": Kicked Off Script(CaptureUserState.vbs v" & sScriptVerNumber & ")"

If oFSO.FolderExists("C:\Program Files\Internet Explorer") Then
	sOfflineWinDir = "C:\Windows"
ElseIf oFSO.FolderExists("D:\Program Files\Internet Explorer") Then
	sOfflineWinDir = "D:\Windows"
ElseIf oFSO.FolderExists("E:\Program Files\Internet Explorer") Then
	sOfflineWinDir = "E:\Windows"
ElseIf oFSO.FolderExists("F:\Program Files\Internet Explorer") Then
	sOfflineWinDir = "F:\Windows"
Else
	sOfflineWinDir = "Not Found"
	oLogFile.WriteLine Time & ": No Windows Directory Found, exiting script(10)."
	Wscript.Quit(10)
End If

oLogFile.WriteLine Time & ": Windows Directory Found:" & sOfflineWinDir

oLogFile.WriteLine Time & ": Attempt to map network drive."
sCmd = "CMD /C NET USE " & sDriveLetter & " /DELETE"
oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 1, True)
sCmd = "CMD /C NET USE " & sDriveLetter & " """ & sShare & """ /USER:" & sUser & " " & sPass
'oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 0, True)
If NOT oFSO.FolderExists("U:\") Then
	oLogFile.WriteLine Time & ": Failed to map network drive(" & sShare & "). Exiting Script(1)."
	Wscript.Quit(1)
End If

oLogFile.WriteLine Time & ": Mapped U: Successfully."
sStorePath = "U:\" & sCompName & "\StateStore"

oLogFile.WriteLine Time & ": Begin to pull estimate of USMT file size."
sCmd = """" & sScriptFolder & "\scanstate.exe"" """ & sStorePath & _
	""" /c /i:""" & sScriptFolder & "\MigDocs.xml"" /i:""" & sScriptFolder & _
	"\ManateeCaptureUSMT.XML"" /config:""" & sScriptFolder  & _
	"\config.xml"" /offline:""" &  sScriptFolder & _
	"\Offline.xml"" /l:""X:\Windows\Temp\SMSTSLog\USMTCapture.log"" /o /uel:30 /p:""X:\Windows\Temp\SMSTSLog\USMTEstimate.log"" /v:13"
oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 1, True)
If sRet <> 0 Then
	oLogFile.WriteLine Time & ": Failed to pull USMT Estimate(" & sShare & "). Exiting Script(1)."
	Wscript.Quit(1)
End If

Set oEstimateFile = oFSO.OpenTextFile("X:\Windows\Temp\SMSTSLog\USMTEstimate.log")
sEstimateFileContents = oEstimateFile.ReadAll
sSizeEstimate = (Round((RegExpTest("[0-9]{1,}(?=</size)", sEstimateFileContents) / 1048000),1))
oLogFile.WriteLine Time & ": Successfully pulled USMT Estimate of: " & sSizeEstimate
sAnswer = oShell.Popup("Would you like to continue the user state capture routine? " & vbCrlf & _
	"(Estimated .MIG Size: " & sSizeEstimate & " MB)",, "USMT Prompt", 4)
If sAnswer = "7" Then 'Answered NO
	oLogFile.WriteLine Time & ": User specified to not copy data. Exiting Script(10)."
	'CopyLogFile
	Wscript.Quit(10)
End If

sCmd = """" & sScriptFolder & "\scanstate.exe"" """ & sStorePath & """" & _
	" /c /i:""" & sScriptFolder & "\MigDocs.xml"" /i:""" & sScriptFolder & _
	"\ManateeCaptureUSMT.XML"" /config:""" & sScriptFolder & _
	"\config.xml"" /offline:""" & sScriptFolder & "\Offline.xml"" /l:""X:\Windows\Temp\SMSTSLog\USMTCapture.log"" /o /uel:30 /v:13"
oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 1, True)

sMIGFilePath = sStorePath & "\USMT\USMT.mig"
If oFSO.FileExists(sMIGFilePath) Then
	oLogFile.WriteLine Time & ": Successfully ran USMT Capture Routine to """ & sMIGFilePath & """"
	sCmd = "CMD /C NET USE " & sDriveLetter & " /DELETE"
	oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
	Wscript.Quit(0)
Else
	oLogFile.WriteLine Time & ": Failed to run USMT Capture Routine to """ & sMIGFilePath & """.  Check the ""X:\Windows\Temp\SMSTSLog\USMTCapture.log""(1)."
	sCmd = "CMD /C NET USE " & sDriveLetter & " /DELETE"
	oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
	Wscript.Quit(1)
End If

Function RegExpTest(strMatchPattern, strPhrase) 
	Set objRegEx = New RegExp
	objRegEx.Global = True 
	objRegEx.IgnoreCase = True 
	objRegEx.Pattern = strMatchPattern 

	Set Matches = objRegEx.Execute(strPhrase) 
	RegExpTest = Matches(0).Value
End Function

 RestoreUserState.vbs:

'==========================================================================
' NAME: RestoreUserState.vbs
'
' AUTHOR: Brian Gonzalez
' DATE  : 12.11.2013
'
' Changlog:
' 12.11.13 - First revision
'
' PURPOSE: User State Capture Script.
'==========================================================================
On Error Resume Next
'Setup Objects, Constants and Variables.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("WScript.Shell")
Set oNetwork = WScript.CreateObject("WScript.Network")
sSystemDriveLetter = oShell.ExpandEnvironmentStrings("%SystemDrive%")
sCompName = oShell.ExpandEnvironmentStrings("%ComputerName%")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
sScriptFolder = oFSO.GetParentFolderName(Wscript.ScriptFullName) 'NoTrailingBS
sScriptVerNumber = "1.0"

'Network Share Variables
sDriveLetter = "R:"
sShare = "\\<ServerShare>\<ShareFolder>"
sUser = "<UserName>"
sPass = "<Password>"

'Close SCCM Progress UI
Set oTSProgressUI = CreateObject("Microsoft.SMS.TSProgressUI")
Set oEnvironment = CreateObject("Microsoft.SMS.TSEnvironment")
oTSProgressUI.CloseProgressDialog()

If oFSO.FolderExists(sSystemDriveLetter & "\_SMSTaskSequence\Logs") Then
	sLogFilePath = sSystemDriveLetter & "\_SMSTaskSequence\Logs\RestoreUserState.log"
	sLogFolder = sSystemDriveLetter & "\_SMSTaskSequence\Logs"
ElseIf oFSO.FolderExists(sSystemDriveLetter & "\Windows\CCM\Logs\SMSTSLogs") Then
	sLogFilePath = sSystemDriveLetter & "\Windows\CCM\Logs\SMSTSLogs\RestoreUserState.log"
	sLogFolder = sSystemDriveLetter & "\Windows\CCM\Logs\SMSTSLogs"
ElseIf oFSO.FolderExists(sSystemDriveLetter & "\Windows\CCM\Logs") Then
	sLogFilePath = sSystemDriveLetter & "\Windows\CCM\Logs\RestoreUserState.log"
	sLogFolder = sSystemDriveLetter & "\Windows\CCM\Logs"
Else
	sLogFilePath = sSystemDriveLetter & "\Windows\Temp\RestoreUserState.log"
	sLogFolder = sSystemDriveLetter & "\Windows\Temp"	
End If

Set oLogFile = oFSO.CreateTextFile(sLogFilePath, ForWriting, True)
oLogFile.WriteLine Time & ": Kicked Off Script(RestoreUserState.vbs v" & sScriptVerNumber & ")"

If oFSO.FolderExists("X:\Windows") Then
	oLogFile.WriteLine Time & ": Running in WinPE OS, script must be run in Windows 7. Exiting Script(1)."
	Wscript.Quit(1)
End If

If oFSO.FolderExists("C:\Program Files\Internet Explorer") Then
	sWinDir = "C:\Windows"
ElseIf oFSO.FolderExists("D:\Program Files\Internet Explorer") Then
	sWinDir = "D:\Windows"
ElseIf oFSO.FolderExists("E:\Program Files\Internet Explorer") Then
	sWinDir = "E:\Windows"
ElseIf oFSO.FolderExists("F:\Program Files\Internet Explorer") Then
	sWinDir = "F:\Windows"
Else
	oLogFile.WriteLine Time & ": No Windows Directory Found, exiting script(10)."
	Wscript.Quit(10)
End If

oLogFile.WriteLine Time & ": Windows Directory Found:" & sWinDir

oLogFile.WriteLine Time & ": Attempt to map network drive."
sCmd = "CMD /C NET USE """ & sDriveLetter & """ /DELETE"
oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 1, True)
sCmd = "CMD /C NET USE " & sDriveLetter & " """ & sShare & """ /USER:" & sUser & _
	" " & sPass
'oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 1, True)
If NOT oFSO.FolderExists(sDriveLetter & "\") Then
	oLogFile.WriteLine Time & ": Failed to map network drive(" & sShare & "). Exiting Script(1)."
	Wscript.Quit(1)
End If

oLogFile.WriteLine Time & ": Mapped " & sDriveLetter & " Successfully."
sMIGFilePath = sDriveLetter & "\" & sCompName & "\StateStore\USMT\USMT.mig"
sStorePath = sDriveLetter & "\" & sCompName & "\StateStore"

If oFSO.FileExists(sMIGFilePath) Then
	oLogFile.WriteLine Time & ": MIG file for Computer was located """ & sMIGFilePath & """."
Else
	oLogFile.WriteLine Time & ": MIG file for Computer was not located """ & sMIGFilePath & """.(10)"
	Wscript.Quit(10)
End If

oLogFile.WriteLine Time & ": Begin to run loadstate using USMT file size:" & sMIGFilePath
sCmd = """" & sScriptFolder & "\loadstate.exe"" """ & sStorePath & _
	""" /i:""" & sScriptFolder & "\MigDocs.xml"" /i:""" & sScriptFolder & _
	"\ManateeCaptureUSMT.XML"" /config:""" & sScriptFolder  & _
	"\config.xml"" /l:""" & sLogFolder & "\USMTRestore.log"" /v:13"
oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
sRet = oShell.Run(sCmd, 1, True)
If sRet <> 0 Then
	oLogFile.WriteLine Time & ": Failed to restore User Data. Exiting Script(1). Review the: " & sLogFolder & "\USMTRestore.log"
	sCmd = "CMD /C NET USE """ & sDriveLetter & """ /DELETE"
	oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
	sRet = oShell.Run(sCmd, 1, True)
	Wscript.Quit(1)
Else
	oLogFile.WriteLine Time & ": Restore of data was successful."
	sCmd = "CMD /C NET USE """ & sDriveLetter & """ /DELETE"
	oLogFile.WriteLine Time & ": About to run(" & sCmd & ")."
	sRet = oShell.Run(sCmd, 1, True)
	Wscript.Quit(0)
End If

/BG

2 thoughts on “A Custom vbScript to Restore and Capture User Data using USMT 4.

  1. Tuan

    Brian,

    Do you an instruction of how to run these scripts?

    Thank you!

    Reply
    1. Brian Gonzalez Post author

      Tuan, At this time with the script improvements with MDT 2013, I would suggest not using these scripts and using the built-in USMT capture and restore.

      Reply

Leave a Reply to Brian Gonzalez Cancel reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.