SCCM Package Distribution Email Notification

In my last post, we looked at a way to monitor ConfigMgr Package distributions with PowerShell.  In this post, we’ll add an email notification to it.  Sometimes when you are distributing a large package to multiple distribution points, it’s handy to receive a notification that the distribution has been completed so you don’t have to keep checking the distribution status.

In this script, we do much the same as in the previous post, except we watch for a change in the distribution status in WMI to see when the distribution has completed.  Then we send an email.

Simply add your email details, sitecode, and the polling frequency into the script, then run it. Choose which active distribution you want to monitor, then you’ll get an email when the distribution to all the relevant distribution points has completed.

As I mentioned in my previous post, ConfigMgr does some post-processing before the actual distribution is started, so if you try to monitor a distribution immediately after you distributed it, you might see no results.  One way to check when it’s started is to search for ‘Created package transfer job to send package … to distribution point’ in the distmgr.log on the site server.

dp1


<#

This script monitors an active distribution that you choose and sends an email once the distribution to all DPs has completed.
Note: Run on the SCCM Site Server, and complete the variables first.

#>

## Complete the variables ##

$ToEmail = "trevor.jones@mycompany.com"
$FromEmail = "sccmsiteserver@mycompany.com"
$smtpServer = "emailserver.mycompany.com"
$SiteCode = "ABC"
# Poll frequency in seconds
$Seconds = "120"

## Start of script

Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
$Drive = $SiteCode + ':'
cd $Drive

$Djobs = Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_DistributionStatus -Filter "Type='2'" | Select Assets,PackageID

if ($djobs -eq $Null)
{
Write-host "There are no active distributions!" -ForegroundColor Yellow
Write-host "If you just distributed something, try again in a couple of minutes as the server may be processing the request."
write-host "Press any key"
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()
exit
}

Write-host ""
Write-host "The following distributions are active:"
write-host ""

foreach ($Djob in $Djobs)
{
$ID = $djob.PackageID
$assets = $djob.Assets
# Get PackageName and Type

$PKG = Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_Package -Filter "PackageID='$ID'" | Select Name
If ($PKG -ne $null)
{
$Name = $PKG.Name
$Type = "Standard Package"
}

$BIPKG = Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_BootImagePackage -Filter "PackageID='$ID'" | Select Name
If ($BIPKG -ne $null)
{
$Name = $BIPKG.Name
$Type = "Boot Image Package"
}

$CNPKG = Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_ContentPackage -Filter "PackageID='$ID'" | Select Name
If ($CNPKG -ne $null)
{
$Name = $CNPKG.Name
$Type = "Application Content Package"
}

$DRVPKG = Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_DriverPackage -Filter "PackageID='$ID'" | Select Name
If ($DRVPKG -ne $null)
{
$Name = $DRVPKG.Name
$Type = "Driver Package"
}

$IMGPKG = Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_ImagePackage -Filter "PackageID='$ID'" | Select Name
If ($IMGPKG -ne $null)
{
$Name = $IMGPKG.Name
$Type = "Image Package"
}

$SUPKG = Get-WmiObject -Namespace root\SMS\Site_D1G -Class SMS_SoftwareUpdatesPackage -Filter "PackageID='$ID'" | Select Name
If ($SUPKG -ne $null)
{
$Name = $SUPKG.Name
$Type = "Software Updates Package"
}

Write-host " Package ID: $ID"
write-host " Package Name: $name"
write-host " Package Type: $Type"
write-host " Number of DPs: $Assets"
write-host ""
}

$PKG2Monitor = Read-host "Enter the Package ID of the distrbution you want to monitor"

write-host " "
write-host "Monitoring the distribution status of package $PKG2Monitor on all DPs. An email will be sent when complete." -ForegroundColor Yellow
write-host "Do not close this PowerShell window!" -ForegroundColor Yellow

while ((Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_DistributionStatus | where {($_.Type -eq "2" -or $_.Type -eq "4") -and $_.PackageID -eq "$PKG2Monitor"}).Type -eq '2' `
-or (Get-WmiObject -Namespace root\SMS\Site_$SiteCode -Class SMS_DistributionStatus | where {($_.Type -eq "2" -or $_.Type -eq "4") -and $_.PackageID -eq "$PKG2Monitor"}).Type -eq '4')
{
Start-Sleep -Seconds $Seconds
}

if ($PKG)
{$Class = "SMS_Package"}
if ($TSPKG)
{$Class = "SMS_TaskSequencePackage"}
if ($BIPKG)
{$Class = "SMS_BootImagePackage"}
if ($CNPKG)
{$Class = "SMS_ContentPackage"}
if ($DRVPKG)
{$Class = "SMS_DriverPackage"}
if ($IMGPKG)
{$Class = "SMS_ImagePackage"}
if ($SUPKG)
{$Class = "SMS_SoftwareUpdatesPackage"}

$NName = Get-WmiObject -Namespace root\SMS\Site_D1G -Class $Class -Filter "PackageID='$PKG2Monitor'" | Select Name
$NName = $NName.Name

send-mailmessage -To $ToEmail -From $FromEmail -Subject "Package distribution completed for $NName ($PKG2Monitor)" -body "The package distribution for $NName ($PKG2Monitor) has completed on all DPs." -smtpServer $smtpServer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s