Created
November 22, 2019 17:19
-
-
Save amar-r/be6600c2bfb141e192305d99a5427626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.DESCRIPTION | |
Sends Task Sequence failure status to Microsoft Teams | |
.NOTES | |
Version: 2.0 | |
Author: Amar Rathore | |
Creation Date: 2019-11-18 | |
#> | |
<# | |
References: | |
- https://www.scconfigmgr.com/2017/10/06/configmgr-osd-notification-service-teams/ | |
- https://tunnelofsludge.com/2018/06/24/sending-messages-in-osd-to-slack/ | |
- https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using | |
#> | |
$uri = '<TEAMS CHANNEL URI>' | |
# Capturing device information | |
$Make = (Get-CimInstance -Class Win32_BIOS).Manufacturer | |
If ($Make -eq 'LENOVO') { $Model = (Get-CimInstance -Class win32_computersystemproduct).Version }Else { $Model = (Get-CimInstance -Class Win32_ComputerSystem).Model } | |
$ComputerName = (Get-CimInstance -Class Win32_ComputerSystem).Name | |
[string]$SerialNumber = (Get-CimInstance -ClassName Win32_BIOS).SerialNumber | |
# Connecting to the Task Sequence environment to pull TS data | |
try { $TSenv = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction Stop }catch { "Unable to connect to the TS Environment" } | |
$TSName = $tsenv.Value('_SMSTSPackageName') | |
$FailedStepName = $tsenv.Value('FailedStepname') | |
$FailedStepReturnCode = $tsenv.Value('FailedStepReturnCode') | |
$TimeSpan = New-TimeSpan -Start $($TSenv.Value('OSDStartInfo')) -End $($TSenv.Value('OSDEndInfo')) | |
[datetime]$Start = $TSenv.Value('OSDStartInfo') | |
[datetime]$End = $TSenv.Value('OSDEndInfo') | |
# Defining the Teams message settings | |
$body = ConvertTo-Json -Depth 10 @{ | |
type = "MessageCard" | |
context = "http://schema.org/extensions" | |
themecolor = "d70000" | |
title = "$TSName failed on $ComputerName" | |
text = " " # Do NOT remove, required for Invoke-RestMethod | |
sections = @( | |
@{ | |
activityTitle = '<h1 style=color:White; style=text-decoration:underline;>Deployment Details' | |
activityImage = 'https://i.imgur.com/T2hpaLa.jpg' | |
}, | |
@{ | |
facts = @( | |
@{ | |
name = 'ComputerName' | |
value = $ComputerName | |
}, | |
@{ | |
name = 'Serial' | |
value = "$SerialNumber" | |
}, | |
@{ | |
name = 'Start Time' | |
value = "$($Start.ToString('hh:mm:sstt'))" | |
}, | |
@{ | |
name = 'End Time' | |
value = "$($end.ToString('hh:mm:sstt'))" | |
}, | |
@{ | |
name = 'Duration' | |
value = "$($timespan.Hours) Hours $($timespan.Minutes) Minutes $($timespan.Seconds) Seconds" | |
}, | |
@{ | |
name = 'Make and Model' | |
value = "$Make $Model" | |
}, | |
@{ | |
name = 'Failed Step' | |
value = $FailedStepName | |
}, | |
@{ | |
name = 'Return Code' | |
value = $FailedStepReturnCode | |
} | |
) | |
} | |
) | |
} | |
# Sending data to Teams | |
Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment