-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFU-Script3.0.ps1
340 lines (315 loc) · 12.7 KB
/
FU-Script3.0.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<#
.SYNOPSIS
This script creates files, folders as well as all the custom scripts possibly needed when upgrading to Windows 11 as a Feature Update
.DESCRIPTION
The script create a FeatureUpdate folder in ProgramData, as well as 4 custom scripts:
SetupComplete.cmd, SetupComplete.ps1, PostRollBack.cmd, PostRollBack.ps1
The script also create a WSUS folder in LocalAppData in the Default userprofile, as well as a SetupConfig.ini file
This script is intended to be run as a preliminary step using Intune or Configuration Manager
.NOTES
Filename: FU-Script.ps1
Version: 3.0
Author: Martin Bengtsson
Blog: www.imab.dk
Twitter: @mwbengtsson
Version history:
1.0 - Script created
2.0 - Added option to send notifications to a Teams channel
- Use the option $sendStatusTeams = $true
3.0 - Added option to escrow BitLocker recovery keys to Azure AD
- Use the option $bitLockerRecoveryKeystoAAD = $true
.LINK
https://www.imab.dk/remove-built-in-teams-app-and-chat-icon-in-windows-11-during-a-feature-update-via-setupconfig-ini-and-setupcomplete-cmd
https://www.imab.dk/monitor-your-windows-11-feature-updates-with-custom-action-scripts-and-notifications-send-to-microsoft-teams
https://www.imab.dk/escrow-bitlocker-recovery-keys-to-azure-ad-during-feature-update-to-windows-11
#>
# Global variables
$global:iniFileFolderPath = "$env:SystemDrive\Users\Default\AppData\Local\Microsoft\Windows\WSUS"
$global:iniFilePath = "$env:SystemDrive\Users\Default\AppData\Local\Microsoft\Windows\WSUS\SetupConfig.ini"
$global:featureUpdateFolder = "$env:ALLUSERSPROFILE\FeatureUpdates"
# Functions
function Create-FeatureUpdatesFolders() {
Write-Verbose -Verbose -Message "Running Create-FeatureUpdateFolders function"
if (-NOT(Test-Path -Path $iniFileFolderPath)) {
New-Item -Path $iniFileFolderPath -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
}
if (-NOT(Test-Path -Path $featureUpdateFolder)) {
New-Item -Path $featureUpdateFolder -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
}
}
function Create-CustomActionScript() {
[CmdletBinding()]
param (
[Parameter(Position="0")]
[ValidateSet("SetupComplete","PostRollBack")]
[string]$Type,
[Parameter(Position="1")]
[String]$Path = $global:featureUpdateFolder
)
Write-Verbose -Verbose -Message "Running Create-CustomActionScript function: $Type"
switch ($Type) {
SetupComplete {
# CMD file
$CMDFileName = $Type + '.cmd'
$CMDFilePath = $Path + '\' + $CMDFileName
New-item -Path $Path -Name $CMDFileName -Force -OutVariable PathInfo | Out-Null
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:featureUpdateFolder\SetupComplete.ps1`""
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
# PS1 file
$PS1FileName = $Type + '.ps1'
$PS1FilePath = $Path + '\' + $PS1FileName
New-item -Path $Path -Name $PS1FileName -Force -OutVariable PathInfo | Out-Null
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = @'
# Script goes here
<#
.SYNOPSIS
SetupComplete.ps1 file located in ProgramData\FeatureUpdates. Will be initiated by SetupComplete.cmd referenced by SetupConfig.ini
.DESCRIPTION
Same as above
.NOTES
Filename: SetupComplete.ps1
Version: 3.0
Author: Martin Bengtsson
Blog: www.imab.dk
Twitter: @mwbengtsson
#>
# Variables
$companyName = "imab.dk"
$targetWindowsBuild = "21H2"
$registryPath = "HKLM:\SOFTWARE\$companyName\WaaS\$targetWindowsBuild"
$runDateTime = Get-Date -Format g
# Escrow BitLocker recovery keys to Azure AD
$bitLockerRecoveryKeystoAAD = $true
# Teams message card variables
$sendStatusTeams = $true
$webhookUri = "<INSERT WEBHOOKE URI>"
$computerName = (Get-WmiObject -Class Win32_ComputerSystem).Name
$computerMake = (Get-WmiObject -Class Win32_BIOS).Manufacturer
$computerModel = (Get-WmiObject -Class Win32_ComputerSystem).Model
$ipAddress = (Get-WmiObject win32_Networkadapterconfiguration | Where-Object{ $_.ipaddress -notlike $null }).IPaddress | Select-Object -First 1
$body = @"
{
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": "WaaS Monitoring",
"themeColor": "f07f13",
"title": "Upgrade of $($computerName): SUCCESS!",
"sections": [
{
"activityTitle": "Kromann Reumert OSD",
"activitySubtitle": "Windows 11 v21H2 Feature Update",
"activityImage": "https://github.com/imabdk/Images/blob/main/success.png?raw=true",
"activityText": "",
"facts": [
{
"name": "Computername:",
"value": "$computerName"
},
{
"name": "Run date/time:",
"value": "$runDateTime"
},
{
"name": "IP address:",
"value": "$ipAddress"
},
{
"name": "Manufacturer:",
"value": "$computerMake"
},
{
"name": "Model:",
"value": "$computerModel"
}
],
"text": "<h2 style=color:#f07f13;>DEPLOYMENT DETAILS"
}
]
}
"@
# Main process
try {
# Removing built-in Teams client
$isTeamsInstalled = Get-AppxPackage -Name "MicrosoftTeams"
if (-NOT[string]::IsNullOrEmpty($isTeamsInstalled)) {
Remove-AppxPackage -Package $isTeamsInstalled.PackageFullName -ErrorAction Stop
}
# Removing chat icon
$chatIconPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Chat"
if (-NOT(Test-Path -Path $chatIconPath)) {
New-Item -Path $chatIconPath -Force
}
if (Test-Path -Path $chatIconPath) {
New-ItemProperty -Path $chatIconPath -Name "ChatIcon" -Value 2 -PropertyType "DWORD" -Force
}
# Escrow BitLocker recovery keys to Azure AD
if ($bitLockerRecoveryKeystoAAD -eq $true) {
$BitLockerVolume = Get-BitLockerVolume -MountPoint $env:SystemDrive -ErrorAction SilentlyContinue
if (-NOT[string]::IsNullOrEmpty($BitLockerVolume)) {
$KeyProtector = $BitLockerVolume.KeyProtector | Where-Object { $_.KeyProtectorType -eq "RecoveryPassword" }
BackupToAAD-BitLockerKeyProtector -MountPoint $env:SystemDrive -KeyProtectorId $KeyProtector.KeyProtectorId -ErrorAction SilentlyContinue
}
}
# Write success to registry
New-ItemProperty -Path $registryPath -Name "SetupCompletecmd" -Value 0 -PropertyType String -Force
}
catch {
# Write failure to registry
New-ItemProperty -Path $registryPath -Name "SetupCompletecmd" -Value 1 -PropertyType String -Force
}
finally {
if ($sendStatusTeams -eq $true) {
Invoke-RestMethod -uri $webhookUri -Method Post -body $body -ContentType 'application/json'
}
}
'@
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
# Do not run another type; break
Break
}
PostRollBack {
# CMD file
$CMDFileName = $Type + '.cmd'
$CMDFilePath = $Path + '\' + $CMDFileName
New-item -Path $Path -Name $CMDFileName -Force -OutVariable PathInfo | Out-Null
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File `"$global:featureUpdateFolder\PostRollBack.ps1`""
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
# PS1 file
$PS1FileName = $Type + '.ps1'
$PS1FilePath = $Path + '\' + $PS1FileName
New-item -Path $Path -Name $PS1FileName -Force -OutVariable PathInfo | Out-Null
$GetCustomScriptPath = $PathInfo.FullName
[String]$Script = @'
# Script goes here
.SYNOPSIS
PostRollBack.ps1 file located in ProgramData\FeatureUpdates. Will be initiated by PostRollBack.cmd referenced by SetupConfig.ini
.DESCRIPTION
Same as above
.NOTES
Filename: PostRollBack.ps1
Version: 3.0
Author: Martin Bengtsson
Blog: www.imab.dk
Twitter: @mwbengtsson
#>
# Variables
$companyName = "imab.dk"
$targetWindowsBuild = "21H2"
$registryPath = "HKLM:\SOFTWARE\$companyName\WaaS\$targetWindowsBuild"
$runDateTime = Get-Date -Format g
# Teams message card variables
$sendStatusTeams = $true
$webhookUri = "<INSERT WEBHOOKE URI>"
$computerName = (Get-WmiObject -Class Win32_ComputerSystem).Name
$computerMake = (Get-WmiObject -Class Win32_BIOS).Manufacturer
$computerModel = (Get-WmiObject -Class Win32_ComputerSystem).Model
$ipAddress = (Get-WmiObject win32_Networkadapterconfiguration | Where-Object{ $_.ipaddress -notlike $null }).IPaddress | Select-Object -First 1
$body = @"
{
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": "WaaS Monitoring",
"themeColor": "f07f13",
"title": "Upgrade of $($computerName): FAILURE!",
"sections": [
{
"activityTitle": "Kromann Reumert OSD",
"activitySubtitle": "Windows 11 v21H2 Feature Update",
"activityImage": "https://github.com/imabdk/Images/blob/main/failure.png?raw=true",
"activityText": "",
"facts": [
{
"name": "Computername:",
"value": "$computerName"
},
{
"name": "Run date/time:",
"value": "$runDateTime"
},
{
"name": "IP address:",
"value": "$ipAddress"
},
{
"name": "Manufacturer:",
"value": "$computerMake"
},
{
"name": "Model:",
"value": "$computerModel"
}
],
"text": "<h2 style=color:#f07f13;>DEPLOYMENT DETAILS"
}
]
}
"@
# Main process
try {
# Write success to registry
New-ItemProperty -Path $registryPath -Name "PostRollBackcmd" -Value 0 -PropertyType String -Force
}
catch {
# Write failure to registry
New-ItemProperty -Path $registryPath -Name "PostRollBackcmd" -Value 1 -PropertyType String -Force
}
finally {
if ($sendStatusTeams -eq $true) {
Invoke-RestMethod -uri $webhookUri -Method Post -body $body -ContentType 'application/json'
}
}
'@
if (-NOT[string]::IsNullOrEmpty($Script)) {
Out-File -FilePath $GetCustomScriptPath -InputObject $Script -Encoding ASCII -Force
}
# Do not run another type; break
Break
}
}
}
function Create-SetupConfigIni() {
Write-Verbose -Verbose -Message "Running Create-SetupConfigIni function"
[String]$iniFileContent = @'
[SetupConfig]
BitLocker=AlwaysSuspend
Compat=IgnoreWarning
Priority=Normal
DynamicUpdate=Disable
ShowOobe=None
Telemetry=Enable
POSTOOBE=C:\ProgramData\FeatureUpdates\SetupComplete.cmd
PostRollBack=C:\ProgramData\FeatureUpdates\PostRollBack.cmd
PostRollBackContext=System
'@
if (Test-Path -Path $iniFileFolderPath) {
$iniFileContent | Out-File -FilePath $iniFilePath -Encoding ASCII -Force
}
else {
Write-Verbose -Verbose -Message "Path to SetupConfig.ini file does not exist: $iniFileFolderPath"
}
}
# Main process
try {
Write-Verbose -Verbose -Message "Running Feature Updates script. Creating folders, scripts and SetupConfig.ini"
Create-FeatureUpdatesFolders
Create-CustomActionScript SetupComplete
Create-CustomActionScript PostRollBack
Create-SetupConfigIni
}
catch {
Write-Verbose -Verbose -Message "Feature Updates script failed to run properly. Please investigate"
exit 1
}
finally {
Write-Verbose -Verbose -Message "Feature Updates script is done running"
exit 0
}