So in follow up to my last post I have been testing a script to look for the smsts log when the task sequence fails and send me an email with the smsts.log as attachment.
I set this up in my MDT task sequence under the gather logs and statestore on failure section so that way whenever the task sequence has an error I get the logs.
</pre>
#Script to send email when imaging fails. Wrote by Logan Seals.
$serial = gwmi win32_bios | select-object SerialNumber | Format-Table -HideTableHeaders | Out-String
$serial = $serial.Trim()
$make = gwmi win32_bios | select-object Manufacturer | Format-Table -HideTableHeaders | Out-String
$make = $make.Trim()
$model = gwmi win32_computersystem | select-object model | Format-Table -HideTableHeaders | Out-String
$model = $model.Trim()
$hostname = hostname
$processor = gwmi win32_processor | select-object name | Format-Table -HideTableHeaders | Out-String
$processor = $processor.Trim()
$totalmemory = gwmi Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}
$operatingsystem = gwmi Win32_OperatingSystem | Select-Object caption, version | Format-Table -HideTableHeaders | Out-String
$operatingsystem = $operatingsystem.Trim()
$msgSubject = “$hostname has failed imaging!”
$msgBody = “PC has failed imaging is host $hostname with Serial $serial and Make and Model $make $model. Processor is $processor. Ram is $totalmemory GB. Operating System is $operatingsystem”
$files = @('x:\windows\temp\smstslog\smsts.log','x:\smstslog\smsts.log','c:\_SMSTaskSequence\Logs\Smstslog\smsts.log',`
'c:\windows\ccm\logs\Smstslog\smsts.log','c:\windows\ccm\logs\smsts.log','C:\windows\SysWOW64\ccm\logs\smsts.log','C:\windows\system32\ccm\logs\smstslog\smsts.log','C:\windows\system32\ccm\logs\smsts.log')
foreach ($file in $files) {
if (Test-Path $file) {
if (Test-Path C:\SMSTSLog) {
mkdir C:\emaillogs
$date = (Get-Date -Format yyyyMMddTHHmmssffff).ToString()
$newfilename = "smstslog$date.log"
Copy-Item $file -Destination "C:\emaillogs\$newfilename"
}
if (Test-Path X:\sources) {
mkdir X:\emaillogs
$date = (Get-Date -Format yyyyMMddTHHmmssffff).ToString()
$newfilename = "smstslog$date.log"
Copy-Item $file -Destination "X:\emaillogs\$newfilename"
}
}
}
if (Test-Path C:\emaillogs) {
Get-ChildItem "C:\emaillogs" | Where {-NOT $_.PSIsContainer} | foreach `
{$_.fullname} | `
send-mailmessage -from "image@email.com" -to "email@email.com" -subject $msgSubject -Body $msgBody -Priority High -dno OnSuccess, onFailure `
-smtpServer x.x.x.x
}
if (Test-Path X:\emaillogs) {
Get-ChildItem "X:\emaillogs" | Where {-NOT $_.PSIsContainer} | foreach `
{$_.fullname} | `
send-mailmessage -from "image@email.com" -to "email@email.com" -subject $msgSubject -Body $msgBody -Priority High -dno OnSuccess, onFailure `
-smtpServer x.x.x.x
}
<pre>
I have a nice array of locations of the smsts log and a foreach loop to look through them test if they exist using Test-Path cmdlet and attach them to my email.
Seems to work well from what I have seen so far. Below is what you get in an email.
Update – I have revised the script as I have had files be locked and I realized that by only attaching the first file it finds I can miss attaching a later smsts.log. I think this will be more robust and work better. I am still debating if there is a better way than to sending two different emails depending if C:\smstslog is available. The goal was to still allow it to send logs even after it is out of the pxe boot image phase of imaging. Not sure if that piece works yet.
