?
Solved

USMT to Send email when complete

Posted on 2014-01-16
14
Medium Priority
?
350 Views
Last Modified: 2014-01-22
Hello! I am wondering if there is a script someone could write that would send an email when a machine has finished the User State Migration Tool process? The email should contain Computer name, completed time and possibly fully successful or errors encountered status. Thanks in advance!
0
Comment
Question by:jbyrd1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 39786993
Hi, I haven't tested this, but it should work for you.  Modify the messaging parameters at the top, and the USMT command and log path, and see how it goes.

Regards,

Rob.

' Email variables:
strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com"
strFrom = "john.doe@abc.com"
strBody = "Find attached the USMT log file:" & vbCrLf

' Run USMT
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strUSMTLog = "\\server\share\USMT\USMTcapture_" & objNetwork.ComputerName & ".log"
strUSMTCmd = "cmd /c ""\\server\share\USMT\x86\scanstate.exe"" \\server\backupshare\" & objNetwork.ComputerName & " /v:5 /o /c /i:""\\server\share\USMT\x86\MigApp.xml"" /i:""\\server\share\USMT\x86\MigDocs.xml"" /l:""" & strUSMTLog & """"
objShell.Run strUSMTCmd, 1, True
SendEmail strServer, strTo, strFrom, "USMT Log File from " & objNetwork.ComputerName & ": " & Now, strBody, strUSMTLog

Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
  		If strAttachment <> "" Then objMessage.AddAttachment strAttachment
  		
        '==This section provides the configuration information for the remote SMTP server.
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'Name or IP of Remote SMTP Server
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
        'Server port (typically 25)
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25      
        objMessage.Configuration.Fields.Update
        '==End remote SMTP server configuration section==
 
        objMessage.Send
        Set objMessage = Nothing
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39788758
Thanks Rob... I already have a CMD file that is run to complete the USMT process. I just need to integrate the Email piece into what I already have. Should have been a little more specific. This runs as a vbs file, right?
0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39789064
I think I almost have this working but cannot figure out how it is looking for the log file to attach. Which lines of the code pertain to this? Also I don't think I would want to attach the whole log file since it can be MB in size. Can we just pull a few words from the log like this to put in the email? This way it would inform me that there were no errors encountered.  

[0x000000] Activity: 'MIGACTIVITY_SUCCESS'
[0x000000] Success.[gle=0x00000091]

Other than that I think I can add and execution statment for this vbs file of my usmt CMD script and it should work just fine.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 65

Expert Comment

by:RobSampson
ID: 39790574
Ok. On Monday I'll have a look at pulling summary lines from the log for you.
0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39791040
Appreciate it!!! I could probably use it in its current state but would be nice to include this info. Thanks!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 39793099
OK, try this..

Rob.

' Email variables:
strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com"
strFrom = "john.doe@abc.com"

' Run USMT
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strUSMTLog = "\\server\share\USMT\USMTcapture_" & objNetwork.ComputerName & ".log"
strUSMTCmd = "cmd /c ""\\server\share\USMT\x86\scanstate.exe"" \\server\backupshare\" & objNetwork.ComputerName & " /v:5 /o /c /i:""\\server\share\USMT\x86\MigApp.xml"" /i:""\\server\share\USMT\x86\MigDocs.xml"" /l:""" & strUSMTLog & """"
objShell.Run strUSMTCmd, 1, True

' Read log file to get result
Set objLog = objFSO.OpenTextFile(strUSMTLog, 1, False)
arrContents = Split(objLog.ReadAll, vbCrLf)
objLog.Close
For intLine = UBound(arrContents) To 1 Step -1
	If InStr(1, arrContents(intLine), "Activity: 'MIGACTIVITY", vbTextCompare) > 0 Then
		strResult = arrContents(intLine) & vbCrLf & _
			arrContents(intLine + 1) & vbCrLf & _
			arrContents(intLine + 2) & vbCrLf
		Exit For
	End If
Next
strResult = arrContents(0) & vbCrLf & strResult

strBody = "The result of the USMT scan is:" & vbCrLf & strResult & vbCrLf & "The log file is located at " & strUSMTLog & vbCrLf

SendEmail strServer, strTo, strFrom, "USMT Log File from " & objNetwork.ComputerName & ": " & Now, strBody & strResult, ""

Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
  		If strAttachment <> "" Then objMessage.AddAttachment strAttachment
  		
        '==This section provides the configuration information for the remote SMTP server.
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'Name or IP of Remote SMTP Server
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
        'Server port (typically 25)
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25      
        objMessage.Configuration.Fields.Update
        '==End remote SMTP server configuration section==
 
        objMessage.Send
        Set objMessage = Nothing
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39797665
Intergated this beautifully! Your awesome! A couple of questions... Will it still email the clip from the log file if it errored or was a failure indicating so? Also, is there any way to have this zip the log file and attach? If you edit the code to include this would you point out the line(s) related to the zip addition? Thanks again!!!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39798404
To perform the zip operation, would you have the ability to use a command line ZIP program like 7-ZIP?  It can just sit on a server share without needing installation, just to reference the zipping program.

It would still email the summary as long as the failed lines began with
Activity: 'MIGACTIVITY

Rob.
0
 
LVL 1

Author Closing Comment

by:jbyrd1981
ID: 39798483
Again!!! Many Thanks! :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39798869
No problem. Thanks for the grade.  If you are able to use the 7-Zip command line tool from a server share, you can post another question, and I can have it zip the log file with the email.

Rob.
0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39800579
No problem... I am giving up the zip idea but thought of one more thing. I was trying to figure out how to get it to include the start time of the USMT in the email as well. If you don't mind what would I need to add to the script to acomplish this? Thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39801375
Looking at my USMT logs, the start time is the first line in it, so we'll just add that to the summary.

Under the "Next" line in the code, I have edited the snippet above and added
strResult = arrContents(0) & vbCrLf & strResult

Open in new window


Regards,

Rob.
0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39801489
That worked! Don't understand how but it did. For some reason it is putting weird formatting on the new line. 2014-01-22 12:41:35, Info                  [0x000000] USMT Started at 2014/01/22:12:41:35.043
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39801521
Hmmm, odd....maybe there's some special characters at the start of the file.  Try changing
strResult = arrContents(0) & vbCrLf & strResult 

Open in new window


to
strResult = Mid(arrContents(0), InStr(arrContents(0), "[0x") - 1) & vbCrLf & strResult 

Open in new window


Rob.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question