forked from mardahl/intune-TeamsNotification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intune-TeamsNotification.ps1
181 lines (151 loc) · 7.28 KB
/
intune-TeamsNotification.ps1
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#Requires -Version 5.0
#Requires -runasadministrator
<#
.SYNOPSIS
Sends a notification to a specified Teams Channel.
.DESCRIPTION
This script will send detailed information about an enrolled computer to a predefined Teams Channel.
.EXAMPLE
Run the script from a Win32app package or just as a PowerShell Configuration script *executed as system.
.NOTES
NAME: Intune-TeamsNotification.ps1
VERSION: 1907d
PREREQ: Microsoft Teams (Duh!)
- Install the Incoming Webhook Connector to your Teams Channel.
- Internet connectivity to the webhook and to psgallery
.COPYRIGHT
@michael_mardahl / https://www.iphase.dk
Licensed under the MIT license.
Please credit me if you fint this script useful and do some cool things with it.
Thanks go out to EvotecIT for creating the awesome PSTeams module! (https://github.com/EvotecIT)
#>
####################################################################################################
#
# Configuration Section (This is where you can edit stuff to fit your needs)
#
####################################################################################################
# The webhook URL you got from the Incoming Webhook Connector configuration guide
$WebhookURL = 'https://outlook.office.com/webhook/844208xrthrxtjxb10375320@ac3cfed8-c7xrtjxrtjd3a6e2b7/IncomingWebhook/58b1d853f3xjrxjrtjrxfe7c3c265b9/ff7xjrjxtrjtrjxt8b2885210'
# Enable testing mode (will post to the channel on every execution of this script if set to $true (default is $false))
$enableTesting = $false
##### Begin custom logic #####
# Put any custom logic here, that you need to generate output for the Buttons or Facts in the notification.
# I have added som example code as an inspiration
# Get logged in user
$currentUser = Get-WMIObject -class Win32_ComputerSystem | select -ExpandProperty username
# Get teamviewer ID (if available)
try { $teamviewerID = Get-ItemProperty -Path HKLM:\SOFTWARE\WOW6432Node\TeamViewer -ErrorAction Stop | select -ExpandProperty ClientID } catch { $teamviewerID = "N/A" }
# Get OS install date
$myinstallDate = ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate).ToString()
# Get a nice display friendly OS Name
$OSinfo = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$OSDisplayName = "$($OSinfo.ProductName) $($OSinfo.ReleaseID)"
##### End custom logic #####
##### Design section starts here, and will determine the look of the notification
# The color of top border of the Teams notification card.
# Find more cool color names here: https://encycolorpedia.com/1e90ff
$Color = "DodgerBlue"
# The first two lines of our Notification
$messageTitle = "$($env:COMPUTERNAME) just enrolled!"
$messageText = "But it might not be finished provisioning yet...."
$activityTitle = "Intune says... "
$activitySubtitle = "I am collecting data about this device."
$activityText = "You might find the following facts interesting!"
$activityImageLink = "https://static-s.aa-cdn.net/img/gp/20600001711818/unUtqpVgwh3J6h_C4wmb0_Zc4ZuESSFejC9eJ8APpa8qy7EV1ulb1x9NufuSuBwm8A=w300"
# Tables containing facts and buttons you wish to show in the notification
# Add a new line for each fact / button, labels MUST be unique!
# Fact Example: "Fact Label"="Custom text"
# Button Example: "Button Label"="https://custom.url"
# Markup: You can add markup to the custom text by wrapping a word or some text with *'s
# ***Italic and Bold*** - **Bold** - *Italic*
# Links: You can add links within the text, like so: [scconfigmgr](https://www.scconfigmgr.com)
$facts = [ordered]@{
"Computername" = "$($env:COMPUTERNAME)"
"Operating System" = "$OSDisplayName"
"Install Date" = "$myinstallDate"
"TeamViewerID" = "$teamviewerID"
}
$Buttons = [ordered]@{
"Visit scconfigmgr.com" = "https://www.scconfigmgr.com"
"Visit iphase.dk" = "https://www.iphase.dk"
}
####################################################################################################
#
# Functions Section (This is a collection of usefull code we are using in the execution section)
#
####################################################################################################
function isNewInstall () {
# Determine if testing is enabled, and skip checks.
if ($enableTesting) {
return $true
}
# Determine if this computer was enrolled more than a day ago
$DMClientTime = Get-ItemPropertyValue HKLM:\SOFTWARE\Microsoft\Provisioning\Diagnostics\ConfigManager\DMClient -Name Time -ErrorAction SilentlyContinue | Get-Date
$nowMinus24Hours = (get-date).AddHours(-24)
# Placing a cookie file, so this script won't run again by mistake.
$cookieFile = "$($env:windir)\Temp\intune_notification-cookie.txt"
if (Test-Path -Path $cookieFile) {
return $false
} else {
Write-Output "this file indicates that the 'intune-TeamsNotification.ps1' script has run on this computer" > $cookieFile
}
if ($nowMinus24Hours -gt $DMClientTime) {
return $false
} else {
return $true
}
}
function Install-PSTeams () {
# Installs or updates the required PSTeams module
if (!(Get-Module PSTeams -ListAvailable)) {
try {
Install-Module PSTeams -Force -ErrorAction Stop
} catch {
Throw "Failed to install the required PSTeams Module! Better check your self, before you wreck your self!"
}
} else {
Update-Module PSTeams
Import-Module PSTeams
}
}
function makeButtonsFromHashtable($Hashtable) {
# Generating Buttons Code from Hashtable in config section
foreach ($Button in $Hashtable.Keys) {
New-TeamsButton -Name $Button -Link $Hashtable["$Button"]
}
}
function makeFactsFromHashtable($Hashtable) {
# Generating Facts Code from Hashtable in config section
# Currently this does not support multi-line facts, instead you will have to add those kind of facts manually.
foreach ($Fact in $Hashtable.Keys) {
New-TeamsFact -Name $Fact -Value "$($Hashtable["$Fact"])"
}
}
####################################################################################################
#
# Execution Section (This is where stuff actually get's run!)
#
####################################################################################################
# Determine if this computer was recently installed or not (we dont want to send a notification from all previously enrolled computers)
if ((isNewInstall) -eq $false) {
Write-Output "This computer was enrolled more than a day ago, so we wont't send a notification to the Teams Channel."
Exit 0
}
# Installing the PSTeams Module if unavailable
Install-PSTeams
# Building the notification design
$Section1 = New-TeamsSection `
-ActivityTitle "$activityTitle" `
-ActivitySubtitle $activitySubtitle `
-ActivityImageLink $activityImageLink `
-ActivityText $activityText `
-Buttons (makeButtonsFromHashtable -Hashtable $Buttons) `
-ActivityDetails $(makeFactsFromHashtable -Hashtable $Facts)
# Sending the notification to the channel
Send-TeamsMessage `
-URI $WebhookURL `
-MessageTitle $messageTitle `
-MessageText $messageText `
-Color $Color `
-Sections $Section1 `
-Verbose