Software Updates Deployments – Notification Script Update

In the last blog post the code used was also triggering notifications for Configuration Baselines.  EventIDs 30196 and 30197 are also used for Baseline deployments.  If we run the query in SQL Management Studio to retrieve all Assignments, we can see that there is an AssignmentType, and looking at that table gives us the following info:

AssignmentType

A quick modification to the query to include the AssignmentType, and then an if statement to only continue the script if AssignmentType = 5 will get the job done.

DISCLAIMER: The information on this site is provided “AS IS” with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation.  Please test any/all information here in a test environment before running on a production network.

SCCM_Software_Update_Events.ps1 – View on GitHub Gist

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
param($msgID, $AssignmentID)
trap { $_ | Out-File -FilePath C:\TEMP\sccm_scripting_error.txt -Force }
## USER CONFIG ##############################################
#$email_to = “joe@northwinds.local”
#$email_from = “noreply@northwinds.local”
#$email_server = “192.168.1.1”
# SQL\SERVERNAME\INSTANCE\Databases\DATABASENAME
$sqlString = “SQL\SCCMCAS\DEFAULT\Databases\CM_CAS”
#############################################################
 
New-EventLog -LogName Application -Source “SMS Scripting” -ErrorAction SilentlyContinue
 
Import-Module SQLPS
 
Set-Location $sqlString
 
$query = Invoke-Sqlcmd -Query “SELECT VCIA.AssignmentId, VCIA.AssignmentName, VCIA.UseGMTTimes, VCIA.CreationTime,
                                     VCIA.LastModifiedBy, VCIA.LastModificationTime, VCIA.StartTime, VCIA.EnforcementDeadline,
                                     VCIA.SourceSite, COLS.CollectionName, SRV.SiteCode, CMC.AssignedCount, VCIA.AssignmentType
                               FROM vCI_CIAssignments VCIA INNER JOIN Collections COLS ON COLS.CollectionID = VCIA.TargetCollectionID
                                     INNER JOIN CollectionMemberCounts CMC ON CMC.CollectionID = COLS.COllectionID
                                     INNER JOIN ServerData SRV ON SRV.ID = CMC.SiteNumber
                               WHERE VCIA.AssignmentID = $AssignmentID;”

 
$info = “”
if($query[0].AssignmentId -eq $AssignmentID) { $info = $query[0] } else { $info = $query }
if($info.AssignmentType -eq 5)
{
    $totalCount = 0
    foreach($rec in $query) { $totalCount += $rec.AssignedCount }
 
    $message = “”
    switch($msgID)
    {
        30196 { $message = “Updates Deployment CREATED on ‘$($info.CollectionName)’ targeting $totalCount machine(s).`r`n`r`n” }
        30197 { $message = “Updates Deployment MODIFIED on ‘$($info.CollectionName)’ targeting $totalCount machine(s).`r`n`r`n” }
        Default { $message = “Unhandled msgID was passed.” }
    }
 
    $message += “AssignmentID: $($info.AssignmentID)`r`n”
    $message += “AssignmentName: $($info.AssignmentName)`r`n”
    $message += “CreationTime: $($info.CreationTime)`r`n”
    $message += “LastModifiedBy: $($info.LastModifiedBy)`r`n”
    $message += “LastModificaitonTime: $($info.LastModificationTime)`r`n”
    $message += “StartTime: $($info.StartTime)”
    if($info.UseGMTTimes) { $message += ” UTC” }
    $message += “`r`n”
    if($info.EnforcementDeadline.ToString().Trim() -eq “”)
    {
        $message += “EnforcementDeadLine: (None – Available Deployment)`r`n”
    } else {
        $message += “EnforcementDeadLine: $($info.EnforcementDeadline)”
        if($info.UseGMTTimes) { $message += ” UTC” }
        $message += “`r`n”
    }
    $message += “CollectionName: $($info.CollectionName)`r`n”
    foreach($rec in $query) { $message += “$($rec.SiteCode) AssignedCount: $($rec.AssignedCount)`r`n” }
    $message += “TotalCount: $totalCount`r`n”
 
    Write-EventLog -LogName Application -Source “SMS Scripting” -EntryType Information -EventId $msgID -Message $message
 
    #$email = New-Object System.Net.Mail.MailMessage
    #$email.To.Add($email_to)
    #$email.From = $email_from
    #$email.Subject = “SCCM 2012 | $($info.SourceSite) Updates Deployment | $totalCount Targets”
    #$email.Body = $message
    #$smtp = New-Object System.Net.Mail.SmtpClient $email_server
    #$smtp.Send($email)
}
#
#