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.


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

$msg = new-object Net.Mail.MailMessage

$msg.From = “imagedpc@email.com”

$msg.To.Add(“email@email.com”)
$msg.To.Add("email@email.com")

$msg.Subject = “$hostname has failed imaging!”

$msg.Body = “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”

$SMTPServer = “IP address”

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

$msg.Priority = [System.Net.Mail.MailPriority]::High

$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) {
$att = new-object Net.Mail.Attachment($file)
$msg.Attachments.Add($att)
}
}

 

$SMTPClient.Send($msg)
if (-not !$variablename) {
$att.Dispose() }

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.

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

 

 

 

Removing unneeded software updates from SCCM

Happy Friday everyone,  I can’t take any credit at all for this one but someone (much better at Powershell than me) has wrote this amazing script on removing updates that are not deployed or not required on any machines but are still in a deployment package. This is been something I’ve needed for a long time to save disk space on DP’s.

See link below.

http://www.scconfigmgr.com/2017/08/17/clean-software-update-packages-in-configmgr-with-powershell

Number of active active sync devices connected to Exchange

Happy Monday everyone,  I haven’t posted anything in a while so I thought I would do a quick post of a short script I wrote the other day.  I was curious how many ActiveSync devices connected that were actively being used in our Exchange.  So after scouring around and discovering the get-mobiledevicestatistics cmdlet for Exchange I came up with the below.


get-mailbox -resultsize unlimited | foreach {Get-MobileDeviceStatistics -mailbox $_.Identity  | Where-Object {$_.LastSuccessSync -gt "06/01/2017"} | select-object identity, LastSuccessSync }

This will give you the identity of the device and the last successful sync time.  You could then pipe this into a export-csv to get a copy of the report or measure-object  to get a count of the objects.   Make sure to change the LastSuccessSync date.

Have a great week everyone.

Finding messages sent to a distribution group in Exchange message tracking.

It’s pretty easy to use the get-messagetrackinglog cmdlet to find what mail was sent to a particular mailbox and by whom but what if you wanted to search for emails sent to a distribution list to make sure they were indeed being sent there.  I had a distribution list today that someone said they weren’t receiving any emails sent to it.  It was caused by another issue but the message tracking log allowed me to get a better idea of what was actually being sent to the distribution group.

capture

So if for some reason you want to find out what is being sent to a distribution group
see the PowerShell below.

Get-MessageTrackingLog -resultsize unlimited -EventID Expand | ? {$_.RelatedRecipientAddress -like "distributiongroup@domain.com"} | ft Timestamp,Sender,MessageSubject -Autosize

Mail-enable already created distribution group.

Today I had an AD administrator create a distribution group and add all the members to it in Active Directory.  They needed it enabled in Exchange so I thought easy just go into the EAC (Exchange Administrative Center) and add an existing group.  This was the case in Exchange 2007 and 2010 but not in 2013.  In order to add an existing group so you don’t have to recreate it from the EAC and add all the users back you go into EMC (Exchange Management Console).  Then once in Powershell type in the command belowmail-enable.

Enable-DistributionGroup -Identity “groupnameinAD” -Alias “groupname”
-DisplayName “displayname"

 

 

Creating a scheduled task without being administrator

Good morning, I was working on a scheduled task that would run on a user’s PC and start a particular application when the user logged into their machine and it had to run as the user in particular logging in.  What I discovered was that if the user wasn’t administrator on the box I would get an access denied in both PowerShell and schtasks.

scheduledtask

What I figured out was that if you choose to run the task at logon (I assume this probably applies to at startup as well) it requires administrative rights but if you schedule the task as an hourly, daily, weekly, etc. task it doesn’t require administrative rights to create it.  Now this requires that what the task is running itself doesn’t need administrative rights but in my case it does not.

scheduledtask2

Now you may be asking yourself why didn’t he simply create a scheduled task through group policy.  Well, the reason for that is I wanted it to target a specific computer collection in SCCM that targeted only laptops.  I could have done it with a GPO and even filtered the GPO with WMI filtering and accomplished the same thing but the application is pushed out through SCCM and I wanted everything that went with it targeted to that collection.  Maybe slightly more work on my part but good to know nonetheless.