MSEndpointMgr

ConfigMgr OSD Notification Service – Teams

Notification Service

Back in August I created a blog post on using Pushover and powershell for OSD deployment notifications, in the comments section Shawn Esterman suggested that Microsoft Teams could be used too. This was a really good suggestion but due to work commitments I have not had much time to look at this until now.

Microsoft Teams

Is a platform that combines workplace chat, meetings, notes and attachments, this is integrated into Office 365 and as such needs a subscription. Teams runs on desktop, laptops and mobile devices, but what makes this product so good is the ever extending list of connectors. So far I have been very impressed with, it has been something that I have not used but was aware of. Its worth noting that Teams will eventually replace/intergrate Skype for Business.

Creating your Team and Channel

If you have never used Teams you will need to get your head around how Teams and Channels work, below is a picture and link that Microsoft provide which really helps. You can see that this has huge potential.

Microsoft Teams Documentation site 

Things worth remembering
  • You can have many Channels on each Team
  • Each Channel can have Connectors (or bots)
  • You can have multiple Connectors on each Channel
  1. Create your Team
  2. Give your Team a name, it’s worth thinking about your structure and not being to specific with the name. 
  3. You can add members or groups to your Team, they will have to have an Office 365 accounts i’m not 100% sure you can use members from a different tenancy.
  4. The Team has been created now look at the settings.
  5. Give your Team an icon.
  6. Now create a channel, the first one is for “OSD Completed Successfully” but I will have multiple channels for “Failed“, “Started” etc
  7. Time to add a connector, there are so many and you can have multiple connectors per channel.
  8. You can add an incoming Webhook which can be used to send data to a Teams channel.
  9. Give your connector a name.
  10. Copy the URL which you will add to the script later.

Powershell Script

<#	
  .NOTES
  ===========================================================================
   Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.140
   Created on:   	05/10/2017 13:07
   Created by:   	Terence Beggs
   Organization: 	SCConfigMgr 
   Filename:     	MicrosoftTeams-OSD-Successful-v0.4.ps1

  ===========================================================================
  .DESCRIPTION
    This script uses Microsoft Teams to notify when a OSD task sequence has completed successfully.

  .SOCIAL
   	Twitter : @terencebeggs
    Blog : https://msendpointmgr.com	
#>

$uri = 'INSERT URL'

# Date and Time
$DateTime = Get-Date -Format g #Time

# Time
$Time = get-date -format HH:mm

# Computer Make
$Make = (Get-WmiObject -Class Win32_BIOS).Manufacturer

# Computer Model
$Model = (Get-WmiObject -Class Win32_ComputerSystem).Model

# Computer Name
$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

# Uses TS Env doesnt give much on x64 arch
#$TSenv = New-Object -COMObject Microsoft.SMS.TSEnvironment -ErrorAction SilentlyContinue
#$TSlogPath = $TSenv.Value("_SMSTSLogPath")

# these values would be retrieved from or set by an application

$body = ConvertTo-Json -Depth 4 @{
  title    = "$Name Completed Successfully"
  text	 = " "
  sections = @(
    @{
      activityTitle    = 'Task Sequence'
      activitySubtitle = 'Windows 10 1703 x64 (Staff & Student)'
      #activityText	 = ' '
      activityImage    = 'https://pbs.twimg.com/profile_images/628680712841924608/xIIFOxFH.png' # this value would be a path to a nice image you would like to display in notifications
    },
    @{
      title = '<h2 style=color:blue;>Deployment Details'
      facts = @(
        @{
          name  = 'Name'
          value = $Name
        },
        @{
          name  = 'Finished'
          value = "$DateTime"
        },
        @{
          name  = 'IP Addresss'
          value = $IPAddress
        },
        @{
          name  = 'Make'
          value = $Make
        },
        @{
          name  = 'Model'
          value = $Model
        },
        @{
          name  = 'Serial'
          value = $SerialNumber
        }
      )
    }
  )
}

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

Executing the script

When the script is run the message instantly arrives on the clients, below is an example of the message on the desktop and the iOS client. You can add this to the end of a Task Sequences and similarly create one for failed Task Sequences.

Desktop Client
iPhone Client

References

If you are not too familiar with powershell or are not too sure how this actually works it’s worth reading up. I have added some resources below and once you understand it you can alter to fit your own environment.

Cmdlet’s

ConvertTo-JSON cmdlet converts any object to a string in JavaScript Object Notation (JSON) format. The properties are converted to field names, the field values are converted to property values, and the methods are removed.

Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that returns richly structured data.

Actionable message card reference

Cards are meant to provide easy to read, at-a-glance information that users can very quickly decipher and act upon when appropriate. As such, the guiding principle for designing great card is “content over chrome,” which means cards are straight to the point and minimize the use of anything that would be distracting such as icons or custom colors.

A great resource on this is https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference.

Card Playground

https://messagecardplayground.azurewebsites.net is great site for testing your “Message Cards”.

Terence Beggs

An Irish man living in London, after completing a BSc in Computer Science in 2005 he started working in the IT Industry. Currently Senior Systems Officer at London Metropolitan University managing Azure and several thousand endpoints across several campuses in London. Technology focuses include SCCM, MDT, Azure, Office 365, Active Directory, Group Policy, Application Packaging, PowerShell, Virtualization and Automation.

45 comments

  • im getting this error when it runs…

    PowerShell.exe does not exist at ‘X:\Windows\system32\windowspowershell\v1.0\powershell.exe’

    full error is this…

    PowerShell script file created successfully
    Running PowerShell script generatedin temporary folder ‘X:\Windows\TEMP\SMSTSPowerShellScripts’ with execution policy: ‘Bypass’
    Filesystem::File::Exists(sPowerShellPath.c_str()) == true, HRESULT=80070490 (main.cpp,661)
    ExecutePowerShellScriptPath(sScriptFile, sScriptFolder, sParameters,sExecutionPolicy, sOutputVariableName, dwPSRunExitCode), HRESULT=80070490 (main.cpp,913)
    PowerShell.exe does not exist at ‘X:\Windows\system32\windowspowershell\v1.0\powershell.exe’
    Run PowerShell script (that was created) failed to run, hr=0x80070490

    • I created two separate channels, for the failed channel the script is run in the error handling part of the TS
      _SMSTSLastActionSucceeded equals false
      _SMSTSOSUpgradeActionReturnCode not equals 0

      that sort of thing, so it only runs if the TS fails.

  • I have been using this successfully for the last couple months, now all of a sudden it has stopped doing notifications. When I try to run manually I get: “System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.” I have no idea what this means, nothing has changed with the script.

    Any ideas whats going on?

    Thanks,

    Vern

  • I am experiencing to not get any notifications in Teams, tho it says in SMSTS log that it runs successfully. Testet script both on the ConfigMgr server and on the client – works. But no notification in Teams, what could it be?

    • Have you run the script separately to make sure the webhook is actually working correctly?

      • When you look at the TS deployment do you see the step where you added Teams running as successful but nothing appears, just out of interest check that the DP has the latest version and that powershell is set to bypass on the TS Step.

  • Put this in my error section this week to notify my team of failures. Not sure if I care as much about successes and such. I do want to create a link or button to easily open the path to the log files that I copy to a server.

    Also implemented the option for techs to receive SMS, email, or both when a deployment fails or completes. Pretty exciting to get instant notification and work on other tasks. Just using a csv with the tech info.. userid,name,phone#,@carrier.email. The tech can either enter thier userid manually or drop it in a file on usb media if they want. Then I just email the phone# + carrier’s SMS email with the message.

    Not sure yet if i want a text myself for every error but i will probably try it for a while since ours is a new implementation.

  • Just to clarify, where should we place this in our TS? At the end? I’ve noticed that if my TS fails earlier than the end of the TS, I do not get the notification because it never makes it to the end. What am I missing?

    • I use one towards the end of the TS and one in the section for errors or put a variable on the step “_SMSTSLastActionSucceeded equals false”

  • Same. I’ve added it at the end of my TS. Deployment details states execution was successful but it never posts to Teams. I’ve tested the script before and it works. Not sure why it’s not working in the TS.

  • I put the step to send the message but nothing arrives 🙁 I use the ‘MicrosoftTeams-OSD-Successful-v0.4.ps1’ from a package without parameters and bypass option.

  • Hi Terence

    How to use this script if TS is failed?
    Also do you know if there’s a problems to use a COM Object ‘Microsoft.SMS.TSEnvironment’ in WinPE 10 x64?

  • Hi Terence,

    I have changed the “$Name” variable to

    if ($TSenv.Value(“_SMSTSPackageName”) -ne “”) { $Name = $TSenv.value(“OSDComputerName”) } else { $Name = (Get-WmiObject -Class Win32_ComputerSystem).Name }

    In this case I am using the Teams notification to show me FAILED and SUCCESSFUL task sequences and if it fails in PE the win32_computerSystem will not use the given variable name which is OSDComputerName instead will use the given PE name which is the generic _SMSTSMachineName = MININT-….

  • Hello Terrence,

    In the message we get the time the task sequence finished, I was wondering how hard it is to add a variable for total time it took the task sequence to complete.
    Thanks

  • Hello I have problems with Proxy of my company, it´s possible configurate Proxy Setting in the script?

  • Just a note – the problem with TSenv above is that it’s defined as TSsenv and then called as TSenv, which doesn’t match. Even in 64-bit WinPE, it works fine now.

    I suggest a few tweaks, namely:
    1) Set product, because both model and product are useful for Lenovo devices.
    if ($Make.tolower() -eq ‘lenovo’) { $Product = (Get-WmiObject -Class Win32_ComputerSystemProduct).Version } else { $Product = $Model }

    2) Detect the proper computer name, if we can.
    if ($TSenv.Value(“OSDComputerName”) -ne “”) { $Name = $TSenv.value(“OSDComputerName”) } else { $Name = (Get-WmiObject -Class Win32_ComputerSystem).Name }

    3) Detect the name of our task sequence dynamically, if possible.
    if ($TSenv.Value(“_SMSTSPackageName”) -ne “”) { $JobName = $TSenv.value(“_SMSTSPackageName”) } else { $JobName = “Unknown Task Sequence” }

    All of these have been tested and are working properly for us on WinPE 1703×64.

    • Hi Jeremy,

      Great catch of the TSenv variable typo. Terrence hasn’t replied to your post so I wonder if he saw this because the script still hasn’t been updated.

      • Oh I thought I had replied to this and updated the script, will do it now.

    • Hello Bill

      You will need to put the URL in $uri = ‘INSERT URI’ part of the script, replace INSERT URI with your URL. I will change this to URL to stop any confusion in the future.

      Thanks

      Uri
      Specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent. This parameter supports HTTP, HTTPS, FTP, and FILE values.+
      This parameter is required. The parameter name (-Uri) is optional.

      • Hi Terence,

        Thank you very much for your response! I’m excited to test this out today!

  • For anyone looking to implement something like this I found the following things helpful:

    1. The card playground mentioned was super helpful
    2. I needed a helper function to escape strings so they were JSON safe:

    # Tried using [Regex]::Escape($String), but it also escaped spaces and that wasn’t going to work for me so I wrote this function
    function EscapeForJson {
    param (
    [Parameter(ValueFromPipeline = $true)]
    $String
    )
    Process
    {
    if ( $String -eq $null ) { return ‘ ‘ }
    $String = $String.ToString().Replace(‘”‘,’\”‘).Replace(‘\’,’\\’).Replace(“`n”,’\n’).Replace(“`r”,’\r’).Replace(“`t”,’\t’)
    return $String
    }
    }
    3a. We do 3 times of task sequence notification: Build Start, Build Finish, and Build Error
    3b. To send build errors, we use the concept behind this article https://blogs.inframon.com/2017/02/windows-10-task-sequence-error-handling/ and if an error occurs there is a task sequence step to send the last error code to a Teams webhook. You create 2 steps, one to store the last result in a custom task sequence variable and one to send the message passing in that custom variable

  • This script inspired me to learn how to do this in Slack instead of Microsoft Teams. While the Slack App still wants JSON, they have different requirements for the array that you convert to json. I’ve uploaded my code here:

    https://pastebin.com/embed_js/4P2ZmDGJ

  • Hi,

    very usefull tips, that’s appreciated ! I’m wondering how can i use this to be used with my ~30 task sequences ? Do i have to edit and create multiple different script ?

    Regards

    • No you should be able to use the same one just change a few things to make it more dynamic like the task sequence name. Plus you should be able to change settings in a task sequence using powershell so shouldn’t take you long

Sponsors