Teams Channel Notification when OSD Fails

/ Endpoint Management / How-To / MECM/MEMCM/SCCM / Office / Powershell / Scripting

How would you like this lovely notification appearing within MS Teams every time a build failed? Better than getting a helpdesk ticket or not finding out at all.

Well, now you can!

Removed broken screenshot - editor

Step 1 - Configure Teams

You need to make sure your O365 Administrator has enabled Incoming webhooks connectors in your O365 Tenant.

  • Open Teams.
  • Select a channel, or create a new channel specific for these notifications (This is best practice so normal channels are not spammed)
  • Click the Next to the channel name and choose Connectors

Removed broken screenshot - editor

  • Click Add / Configure

Removed broken screenshot - editor

  • Give your Connector an appropriate name and custom image.

Removed broken screenshot - editor

  • It will then generate a unique API code for you to use later. Copy this URI and keep it safe!

Removed broken screenshot - editor

Step 2 - PowerShell Script

I have modified the PowerShell script created by Terence Beggs from https://www.scconfigmgr.com/2017/10/06/configmgr-osd-notification-service-teams/

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
#
    .NOTES
    ==================================================================
    Originally Created by:     Terence Beggs (https://www.scconfigmgr.com)
    Modified by:    Grant Dickins
    ==================================================================
    .DESCRIPTION
    This script uses Microsoft Teams to notify when a OSD task sequence has failed.
    .REFERENCE
    https://docs.microsoft.com/outlook/actionable-messages/message-card-reference
    https://www.scconfigmgr.com/2017/10/06/configmgr-osd-notification-service-teams/
    https://messagecardplayground.azurewebsites.net/
    ==================================================================
    .TODO
    - Add Error codes to MessageCard
#>
$uri = 'INSERT URI HERE'

# Open TSEnv for use.
$TSenv = New-Object -COMObject Microsoft.SMS.TSEnvironment -ErrorAction SilentlyContinue

# Date and Time
$DateTime = Get-Date -Format "dddd dd/MM/yyyy HH:mm"
    
# Computer Make
$Make = (Get-WmiObject -Class Win32_BIOS).Manufacturer
    
# Computer Model
$Model = (Get-WmiObject -Class Win32_ComputerSystem).Model
    
# Computer Name
#DEBUG# $Name = (Get-WmiObject -Class Win32_ComputerSystem).Name
if ($TSenv.Value("_SMSTSPackageName") -ne "") { $Name = $TSenv.value("OSDComputerName") } else { $Name = (Get-WmiObject -Class Win32_ComputerSystem).Name }

# Computer Serial Number
[string]$SerialNumber = (Get-WmiObject win32_bios).SerialNumber
    
# IP Address of the Computer
$IPAddress = (Get-WmiObject win32_Networkadapterconfiguration | Where-Object { $_.ipaddress -notlike $null }).IPaddress | Select-Object -First 1
    
#$TS Name
#DEBUG# $TSName = "TSName"
if ($TSenv.Value("_SMSTSPackageName") -ne "") { $TSName = $TSenv.value("_SMSTSPackageName") } else { $TSName = "Unknown Task Sequence" }

#Error Code
$TSLastError = ($TSenv.Value("ErrorReturnCode"))
If([string]::IsNullOrEmpty($TSLastError)){$TSLastError = "Unknown Error. Check Logs."}
    
# JSON for the Teams MessageCard (LEGACY)
$body = @"
    {
        "@type": "MessageCard",
        "@context": "https://schema.org/extensions",
        "summary": "OSD Failure Card",
        "themeColor": "0078D7",
        "title": "$Name Failed to build",
        "text": "",
        "sections": [
            {
                "activityTitle": "Task Sequence",
                "activitySubtitle": "$TSName",
                "activityImage": "https://media.giphy.com/media/27EhcDHnlkw1O/giphy.gif"
            },
            {
                "title": "## Deployment Details",
                "facts": [
                    {
                        "name": "Name:",
                        "value": "$Name"
                    },
                    {
                        "name": "Date Time:",
                        "value": "$DateTime"
                    },
                    {
                        "name": "IP Number:",
                        "value": "$IPAddress"
                    },
                    {
                        "name": "Make:",
                        "value": "$Make"
                    },
                    {
                        "name": "Model:",
                        "value": "$Model"
                    },
                    {
                        "name": "Serial:",
                        "value": "$SerialNumber"
                    },
                    {
                        "name": "Last Error",
                        "value": "$TSLastError"
                    }
                ]
            },
            {
                "title": "## Log Location",
                "text": "\\\\\\SERVER\\Logs\\OSD\\"
            }
        ]
    }
"@

Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json'

Customizing the MessageCard

If you want to change the JSON, there is a reference guide here: https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference

And a Playground site: https://messagecardplayground.azurewebsites.net/

This modification of the PowerShell script means you can just copy/paste the JSON as long as it’s wrapped inside $body = @" … “@

NB: Incoming Webconnectors only support the Legacy MessageCard format at this time.

Step 3 - Create Package for Script

  • Copy the PowerShell script to your Config Manager package source share.
  • In Config Manager Console, create a new Package.
  • Give it a Name, Description, and specify the source share

Removed broken screenshot - editor

  • Select Do not create a program

Removed broken screenshot - editor

  • Complete the wizard.
  • Distribute the Package contents.

Step 4 - Task Sequence

Edit the task sequence for OSD.

Our TS is set up with logic and a section to run if the task sequence encounters an error. This is where we will put the OSD Failure notification script.

For more information on how to set up Error Handling in OSD Task Sequence, see Jörgen Nilsson’s post about it here: https://ccmexec.com/2016/12/error-handling-in-ts-without-mdt-using-osdbackground/

Removed broken screenshot - editor

  • After the Gather step, we want to add a Run PowerShell Script task.
  • Select the package with our Script.
  • Change the PowerShell execution policy to Bypass

Removed broken screenshot - editor

  • Click OK and that’s it set up!

If you want to try emulating a failure, create a Run Command Line task somewhere in your Task Sequence that runs the following:

cmd /c exit 8008135

Removed broken screenshot - editor

So now, if your task sequence runs into an error, you will now get a lovely message straight into your Teams channel:

Removed broken screenshot - editor

Photo of Grant Dickins
Grant Dickins Contributor

Grant has worked supporting IT in the Oil & Gas industry for 10 years, has a keen interest in Config Manager and trying to bring the industry kicking and screaming into the modern cloud-based world.