Getting email alert and smsts log when imaging fails via PowerShell

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.

snip_20180727091106

Advertisements

Getting email alert when imaging is finished

I wanted to get some details every time a machine was imaged for my own details.  Mostly for asset tracking purposes I set up the below script to run at the end of my task sequence.


$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()

$EmailFrom = “imagedpc@email.com”

$EmailTo = “email@email.com”

$Subject = “$hostname has been imaged”

$Body = “PC imaged is $hostname with Serial $serial and Make and Model $make $model. Processor is $processor. Ram is $totalmemory GB. Operating System is $operatingsystem”

$SMTPServer = “ipaddress”

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

It is really nice to get all this info.

PC imaged is PCname with Serial R9ZRLB8 and Make and Model LENOVO 23594LU.  Processor is Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz.  Ram is 8.00 GB.  Operating System is Microsoft Windows 10 Pro 10.0.15063

I suppose this could also be modified so that the tech imaging the machine could also get the email.  Maybe basing it off of the machine’s OU.  Hopefully someone finds this useful but I have found it quite handy.  I have also finished a script to gather logs when the task sequence fails and send them to me but I am still testing that out and will save that for my next post.

Update – So just wanted to update that if you wanted to make this easier there is actually a send-mailmessage powershell cmdlet that does the same thing for the send email portion.  Mine still work just fine though 🙂

More info on send-mailmessage below.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1