Send Task Sequence status to MS Teams

Final output to Teams

Before I start…

I need to specify that I didn’t figure this out on my own. There are some great blogs which I referenced.

Recommendations

Lets begin!

Adding Webhook Connector to Teams

In Microsoft Teams, click on the appropriate Team and select a channel you would like to use for the notifications.  In my example I will be using the “Test Notifications” channel. 

  1. To the right of the channel name, click on the three (3) dots and then select Connectors

  2. Search for Incoming Webhook and select Configure

    (Click to enlarge)

  3. Next step is to name your Webhook , upload an image (optional), and finally create.

    (Click to enlarge)

  4. On the next screen we need to copy the connector URL. Scroll down about half way and click the copy button.
    IMPORTANT: We will need the copied URL later when we modify the PowerShell script.

    (Click to Enlarge)

    1.  

The Task Sequence

This should be simple, there are four (4) main steps that are needed for this to work.

  1. Start time
  2. Capture failure conditions
  3. End time
  4. Send status to Teams

This is where we will be leveraging Adam’s blog as well as Mike’s blog.

We need two (2) groups in the Task Sequence.

  • Main Group (set to Continue on error)
  • Failure Group | Condition: TS Variable _SMSTSLastActionSucceeded equals “False”

Steps Breakdown

  1. Record Build Start Time
    New step → Run Command Line
    PowerShell (New-Object -COMObject Microsoft.SMS.TSEnvironment).Value('OSDStartInfo')=(Get-Date -Format 'dd-MMM-yyyy HH:mm:ss')
  2. Capture Failure Conditions
    New step → Set Dynamic Variables
  3. Record Build End Time
    New step → Run Command Line
    PowerShell (New-Object -COMObject Microsoft.SMS.TSEnvironment).Value('OSDEndInfo')=(Get-Date -Format 'dd-MMM-yyyy HH:mm:ss')
  4. Send Status to Teams
    New step → Run PowerShell Script
    You have three (3) options here
    1.  Create a package and select the script from the package source
    2.  Import the script to the Scripts node (recommended method)
    3.  Paste the script contents directly (I’ll be using this for the example)

    Notice that I’m using Bypass for my execution policy, this is mainly for my example.  Best practice is to sign all your scripts and use the All Signed option.

 The Script

After you’ve copied/downloaded the script – we need to modify line #17 $uri = '<TEAMS CHANNEL URI>'

We will past the URL which was copied from Step 4 in the Adding Webhook Connector to Teams section to line 17

  Download

9 comments on “Send Task Sequence status to MS Teams

  1. AvatarJames

    Hello,
    I was working on getting this process working but I am having a few error. Would you happen to be able to clarify?
    The error occurs with the Command line step in the TS, running the line
    Powershell (New-Object -COMObject Microsoft.SMS.TSEnvironment).Value(‘OSDStartInfo’)=(Get-Date -Format ‘dd-MMM-yyyy HH:mm:ss’)

    Gives me the following error below:

    New-Object : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed due to the
    following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

    I want those start and end times in the message but I cannot get it to move past these steps. Any idea why this fails or what I am missing for this component to work? Thank you!

    Reply
    1. AvatarAmar Rathore Post author

      Hi James,

      This can only run when the TS environment is running/loaded. One way you can do that, that I’m aware of, is to create a task sequence with a pause step and deploy it to your machine (available deployment). Run the TS and then launch powershell as SYSTEM to get those values. If you want to wait, I’ll be back next week from vacation and be able to help a bit better.

      Reply
      1. AvatarJames

        Thank you Amar for reaching out so quickly. I had a feeling it had something to do with the environment it ran in. I’ll try a few things and get back to you! Enjoy your vacation!

        Reply
          1. AvatarJames

            Amar,

            ended up getting it working, Powershell scripting wasn’t enabled in the boot.wim but I modified it. I was also able to add some custom rows on the script which worked perfectly. Thank you so much Amar for your work on this, it was a huge help and will be very useful in my environment.

Leave a Reply

Your email address will not be published. Required fields are marked *