-
Notifications
You must be signed in to change notification settings - Fork 1
/
Invoke-Windows11Accelerator.ps1
718 lines (613 loc) · 27.9 KB
/
Invoke-Windows11Accelerator.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<#
.SYNOPSIS
Allows for a phased and controlled distribution of Windows 11 Feature Updates
following the run and capture of Update Readiness data, tagging devices
in Entra ID with their update readiness risk score for use with Dynamic
Security Groups.
.DESCRIPTION
The Invoke-Windows11Accelerator script allows for the controlled roll out of
Windows 11 Feature Updates based on device readiness risk assements data.
.PARAMETER tenantId
Provide the Id of the tenant to connecto to.
.PARAMETER featureUpdateBuild
Select the Windows 11 Feature Update verion you wish to deploy
Choice of 22H2 or 23H2.
.PARAMETER extensionAttribute
Configure the device extensionAttribute to be used for tagging Entra ID objects
with their Feature Update Readiness Assessment risk score.
Choice of 1 to 15
.PARAMETER target
Select the whether you want to target the deployment to groups of users or groups of devices.
Choice of Users or Devices.
.PARAMETER scopes
The scopes used to connect to the Graph API using PowerShell.
Default scopes configured are:
'Group.ReadWrite.All,Device.ReadWrite.All,DeviceManagementManagedDevices.ReadWrite.All,DeviceManagementConfiguration.ReadWrite.All'
.PARAMETER deploy
Select whether you want to run the script, with this switch it will tag devices or users with their risk state, without it the script will run in report only mode.
.INPUTS
None. You can't pipe objects to Invoke-Windows11Accelerator.
.OUTPUTS
None. Invoke-Windows11Accelerator doesn't generate any output.
.EXAMPLE
PS> .\Invoke-Windows11Accelerator.ps1 -tenantId 36019fe7-a342-4d98-9126-1b6f94904ac7 -featureUpdateBuild 23H2 -target Devices -extensionAttribute 15
.EXAMPLE
PS> .\Invoke-Windows11Accelerator.ps1 -tenantId 36019fe7-a342-4d98-9126-1b6f94904ac7 -featureUpdateBuild 22H2 -target Users -extensionAttribute 10
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[String]$tenantId,
[Parameter(Mandatory = $true)]
[ValidateSet('22H2', '23H2')]
[String]$featureUpdateBuild = '23H2',
[Parameter(Mandatory = $true)]
[ValidateSet('user', 'device')]
[String]$target = 'user',
[Parameter(Mandatory = $true)]
[ValidateRange(1, 15)]
[String]$extensionAttribute,
[Parameter(Mandatory = $false)]
[String[]]$scopes = 'Group.ReadWrite.All,Device.ReadWrite.All,DeviceManagementManagedDevices.ReadWrite.All,DeviceManagementConfiguration.ReadWrite.All,User.ReadWrite.All',
[Parameter(Mandatory = $false)]
[Switch]$deploy
)
#region Functions
Function Test-JSON() {
param (
$JSON
)
try {
$TestJSON = ConvertFrom-Json $JSON -ErrorAction Stop
$TestJSON | Out-Null
$validJson = $true
}
catch {
$validJson = $false
$_.Exception
}
if (!$validJson) {
Write-Host "Provided JSON isn't in valid JSON format" -f Red
break
}
}
Function New-ReportFeatureUpdateReadiness() {
[cmdletbinding()]
param
(
[parameter(Mandatory = $true)]
$JSON
)
$graphApiVersion = 'Beta'
$Resource = 'deviceManagement/reports/cachedReportConfigurations'
try {
Test-Json -Json $JSON
$uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
Invoke-MgGraphRequest -Uri $uri -Method Post -Body $JSON -ContentType 'application/json'
}
catch {
Write-Error $Error[0].ErrorDetails.Message
break
}
}
Function Get-ReportFeatureUpdateReadiness() {
[cmdletbinding()]
param (
[parameter(Mandatory = $false)]
$Id,
[parameter(Mandatory = $false)]
$JSON
)
$graphApiVersion = 'Beta'
if ($id) {
$Resource = "deviceManagement/reports/cachedReportConfigurations('$Id')"
}
elseif ($JSON) {
$Resource = 'deviceManagement/reports/getCachedReport'
}
try {
$uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
if ($id) {
Invoke-MgGraphRequest -Uri $uri -Method Get
}
elseif ($JSON) {
$tempFile = [System.IO.Path]::GetTempFileName()
Invoke-MgGraphRequest -Uri $uri -Method Post -Body $JSON -ContentType 'application/json' -OutputFilePath $tempFile
Get-Content -Raw $tempFile | ConvertFrom-Json
Remove-Item $tempFile
}
}
catch {
Write-Error $Error[0].ErrorDetails.Message
break
}
}
Function Add-ObjectAttribute() {
[cmdletbinding()]
param
(
[parameter(Mandatory = $true)]
[ValidateSet('User', 'Device')]
$object,
[parameter(Mandatory = $true)]
$JSON,
[parameter(Mandatory = $true)]
$Id
)
$graphApiVersion = 'Beta'
if ($object -eq 'User') {
$Resource = "users/$Id"
}
else {
$Resource = "devices/$Id"
}
try {
Test-Json -Json $JSON
$uri = "https://graph.microsoft.com/$graphApiVersion/$($Resource)"
Invoke-MgGraphRequest -Uri $uri -Method Patch -Body $JSON -ContentType 'application/json'
}
catch {
Write-Error $Error[0].ErrorDetails.Message
break
}
}
Function Get-EntraIDObject() {
[cmdletbinding()]
param
(
[parameter(Mandatory = $true)]
[ValidateSet('User', 'Device')]
$object
)
$graphApiVersion = 'beta'
if ($object -eq 'User') {
$Resource = 'users'
}
else {
$Resource = 'devices'
}
try {
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"
$GraphResults = Invoke-MgGraphRequest -Uri $uri -Method Get
$Results = @()
$Results += $GraphResults.value
$Pages = $GraphResults.'@odata.nextLink'
while ($null -ne $Pages) {
$Additional = Invoke-MgGraphRequest -Uri $Pages -Method Get
if ($Pages) {
$Pages = $Additional.'@odata.nextLink'
}
$Results += $Additional.value
}
$Results
}
catch {
Write-Error $Error[0].ErrorDetails.Message
break
}
}
Function Get-ManagedDevices() {
[cmdletbinding()]
param
(
)
$graphApiVersion = 'beta'
$Resource = 'deviceManagement/managedDevices'
try {
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"
$GraphResults = Invoke-MgGraphRequest -Uri $uri -Method Get
$Results = @()
$Results += $GraphResults.value
$Pages = $GraphResults.'@odata.nextLink'
while ($null -ne $Pages) {
$Additional = Invoke-MgGraphRequest -Uri $Pages -Method Get
if ($Pages) {
$Pages = $Additional.'@odata.nextLink'
}
$Results += $Additional.value
}
$Results
}
catch {
Write-Error $Error[0].ErrorDetails.Message
break
}
}
#endregion Functions
#region authentication
if (Get-MgContext) {
Write-Host 'Disconnecting from existing Graph session.' -ForegroundColor Cyan
Disconnect-MgGraph
}
$moduleName = 'Microsoft.Graph'
$Module = Get-InstalledModule -Name $moduleName
if ($Module.count -eq 0) {
Write-Host "$moduleName module is not available" -ForegroundColor yellow
$Confirm = Read-Host Are you sure you want to install module? [Y] Yes [N] No
if ($Confirm -match '[yY]') {
Install-Module -Name $moduleName -AllowClobber -Scope AllUsers -Force
}
else {
Write-Host "$moduleName module is required. Please install module using 'Install-Module $moduleName -Scope AllUsers -Force' cmdlet." -ForegroundColor Yellow
break
}
}
else {
If ($IsMacOS) {
Connect-MgGraph -Scopes $scopes -UseDeviceAuthentication -TenantId $tenantId
Write-Host 'Disconnecting from Graph to allow for changes to consent requirements' -ForegroundColor Cyan
Disconnect-MgGraph
Write-Host 'Connecting to Graph' -ForegroundColor Cyan
Connect-MgGraph -Scopes $scopes -UseDeviceAuthentication -TenantId $tenantId
}
ElseIf ($IsWindows) {
Connect-MgGraph -Scopes $scopes -UseDeviceCode -TenantId $tenantId
Write-Host 'Disconnecting from Graph to allow for changes to consent requirements' -ForegroundColor Cyan
Disconnect-MgGraph
Write-Host 'Connecting to Graph' -ForegroundColor Cyan
Connect-MgGraph -Scopes $scopes -UseDeviceAuthentication -TenantId $tenantId
}
Else {
Connect-MgGraph -Scopes $scopes -TenantId $tenantId
Write-Host 'Disconnecting from Graph to allow for changes to consent requirements' -ForegroundColor Cyan
Disconnect-MgGraph
Write-Host 'Connecting to Graph' -ForegroundColor Cyan
Connect-MgGraph -Scopes $scopes -TenantId $tenantId
}
$graphDetails = Get-MgContext
if ($null -eq $graphDetails) {
Write-Host "Not connected to Graph, please review any errors and try to run the script again' cmdlet." -ForegroundColor Red
break
}
}
#endregion authentication
#region Variables
$ProgressPreference = 'SilentlyContinue';
$rndWait = Get-Random -Minimum 1 -Maximum 5
$extensionAttributeValue = 'extensionAttribute' + $extensionAttribute
$fu = Switch ($featureUpdateBuild) {
'22H2' { 'NI22H2' }
'23H2' { 'NI23H2' }
'24H2' { 'NI24H2' }
}
$featureUpdateCreateJSON = @"
{
"id": "MEMUpgradeReadinessDevice_00000000-0000-0000-0000-000000000001",
"filter": "(TargetOS eq '$fu') and (DeviceScopesTag eq '00000')",
"orderBy": [],
"select": [
"DeviceName",
"DeviceManufacturer",
"DeviceModel",
"OSVersion",
"ReadinessStatus",
"SystemRequirements",
"AppIssuesCount",
"DriverIssuesCount",
"AppOtherIssuesCount"
],
"metadata": "TargetOS=>filterPicker=V2luZG93cyUyMDExJTIwLSUyMHZlcnNpb24lMjAyMkgy,DeviceScopesTag=>filterPicker=RGVmYXVsdA=="
}
"@
$featureUpdateGetJSON = @'
{
"Id": "MEMUpgradeReadinessDevice_00000000-0000-0000-0000-000000000001",
"Skip": 0,
"Top": 50,
"Search": "",
"OrderBy": [],
"Select": [
"DeviceName",
"DeviceManufacturer",
"DeviceModel",
"OSVersion",
"ReadinessStatus",
"SystemRequirements",
"AppIssuesCount",
"DriverIssuesCount",
"AppOtherIssuesCount",
"DeviceId",
"AadDeviceId",
"Ownership"
],
"filter": ""
}
'@
#endregion Variables
#region Intro
Write-Host
Write-Host '░██╗░░░░░░░██╗██╗███╗░░██╗██████╗░░█████╗░░██╗░░░░░░░██╗░██████╗ ░░███╗░░░░███╗░░'
Write-Host '░██║░░██╗░░██║██║████╗░██║██╔══██╗██╔══██╗░██║░░██╗░░██║██╔════╝ ░████║░░░████║░░'
Write-Host '░╚██╗████╗██╔╝██║██╔██╗██║██║░░██║██║░░██║░╚██╗████╗██╔╝╚█████╗░ ██╔██║░░██╔██║░░'
Write-Host '░░████╔═████║░██║██║╚████║██║░░██║██║░░██║░░████╔═████║░░╚═══██╗ ╚═╝██║░░╚═╝██║░░'
Write-Host '░░╚██╔╝░╚██╔╝░██║██║░╚███║██████╔╝╚█████╔╝░░╚██╔╝░╚██╔╝░██████╔╝ ███████╗███████╗'
Write-Host '░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░░╚════╝░░░░╚═╝░░░╚═╝░░╚═════╝░ ╚══════╝╚══════╝'
Write-Host ''
Write-Host '░█████╗░░█████╗░░█████╗░███████╗██╗░░░░░███████╗██████╗░░█████╗░████████╗░█████╗░██████╗░'
Write-Host '██╔══██╗██╔══██╗██╔══██╗██╔════╝██║░░░░░██╔════╝██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗██╔══██╗'
Write-Host '███████║██║░░╚═╝██║░░╚═╝█████╗░░██║░░░░░█████╗░░██████╔╝███████║░░░██║░░░██║░░██║██████╔╝'
Write-Host '██╔══██║██║░░██╗██║░░██╗██╔══╝░░██║░░░░░██╔══╝░░██╔══██╗██╔══██║░░░██║░░░██║░░██║██╔══██╗'
Write-Host '██║░░██║╚█████╔╝╚█████╔╝███████╗███████╗███████╗██║░░██║██║░░██║░░░██║░░░╚█████╔╝██║░░██║'
Write-Host '╚═╝░░╚═╝░╚════╝░░╚════╝░╚══════╝╚══════╝╚══════╝╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝'
Write-Host ''
Write-Host ' By Nick Benton (@ennnbeee)'
Write-Host ' Version : 1.1.0'
Write-Host
Write-Host
Write-Host
Start-Sleep -Seconds $rndWait
Write-Host ('Connected to Tenant {0} as account {1}' -f $graphDetails.TenantId, $graphDetails.Account) -ForegroundColor Green
Write-Host
if ($deploy) {
Write-Host "Starting the 'Get Ready for Windows 11' Script..." -ForegroundColor Red
Write-Host
}
else {
Write-Host "Starting the 'Get Ready for Windows 11' Script in demo mode..." -ForegroundColor Magenta
Write-Host
}
Write-Host 'The script will carry out the following:' -ForegroundColor Green
Write-Host " - Capture all Windows Device or User objects from Entra ID, these are used for assigning an Extension Attribute ($extensionAttributeValue) used in the Dynamic Groups." -ForegroundColor White
Write-Host " - Start a Windows 11 Feature Update Readiness report for your selected build version of $featureUpdateBuild." -ForegroundColor White
Write-Host " - Capture and process the outcome of the Windows 11 Feature Update Readiness report for build version $featureUpdateBuild" -ForegroundColor White
Write-Host " - Based on the Risk level for the device, will assign a risk based flag to the Primary User or Device using Extension Attribute $extensionAttributeValue" -ForegroundColor White
Write-Host
Write-Host 'The script can be run multiple times, as the Extension Attributes are overwritten with each run.' -ForegroundColor Cyan
Write-Host
Write-Host "Before proceding with the running of the script, please create Entra ID Dynamic $target Groups for each of the below risk levels, using the provided rule:" -ForegroundColor Green
Write-Host " - Low Risk: ($target.$extensionAttributeValue -eq"LowRisk-W11-$featureUpdateBuild")" -ForegroundColor White
Write-Host " - Medium Risk: ($target.$extensionAttributeValue -eq"MediumRisk-W11-$featureUpdateBuild")" -ForegroundColor White
Write-Host " - High Risk: ($target.$extensionAttributeValue -eq"HighRisk-W11-$featureUpdateBuild")" -ForegroundColor White
Write-Host " - Not Ready: ($target.$extensionAttributeValue -eq"NotReady-W11-$featureUpdateBuild")" -ForegroundColor White
Write-Host " - Unknown: ($target.$extensionAttributeValue -eq"Unknown-W11-$featureUpdateBuild")" -ForegroundColor White
Write-Host
Write-Warning 'Please review the above and confirm you are happy to continue.' -WarningAction Inquire
#endregion Intro
#region pre-flight
Write-Host
if ($target -eq 'user') {
Write-Host 'Getting user objects and associated IDs from Entra ID...' -ForegroundColor Cyan
$entraUsers = Get-EntraIDObject -object User | Where-Object { $_.accountEnabled -eq 'true' -and $_.userType -eq 'Member' }
Write-Host "Found $($entraUsers.Count) user objects and associated IDs from Entra ID." -ForegroundColor Green
Write-Host
Write-Host 'Getting device objects and associated IDs from Microsoft Intune...' -ForegroundColor Cyan
$intuneDevices = Get-ManagedDevices | Where-Object { $_.operatingSystem -eq 'Windows' }
Write-Host "Found $($intuneDevices.Count) device objects and associated IDs from Microsoft Intune." -ForegroundColor Green
Write-Host
}
Write-Host 'Getting device objects and associated IDs from Entra ID...' -ForegroundColor Cyan
$entraDevices = Get-EntraIDObject -object Device | Where-Object { $_.operatingSystem -eq 'Windows' }
Write-Host "Found $($entraDevices.Count) Windows devices and associated IDs from Entra ID." -ForegroundColor Green
Write-Host
Write-Host "Checking for existing data in attribute $extensionAttributeValue in Entra ID..." -ForegroundColor Cyan
$attributeErrors = 0
$safeAttributes = @("LowRisk-W11-$featureUpdateBuild", "MediumRisk-W11-$featureUpdateBuild", "HighRisk-W11-$featureUpdateBuild", "NotReady-W11-$featureUpdateBuild", "Unknown-W11-$featureUpdateBuild")
$entraObjects = switch ($target) {
'user' { $entraUsers }
'device' { $entraDevices }
}
$extAttribute = switch ($target) {
'user' { 'onPremisesExtensionAttributes' }
'device' { 'extensionAttributes' }
}
foreach ($entraObject in $entraObjects) {
$attribute = ($entraObject.$extAttribute | ConvertTo-Json | ConvertFrom-Json).$extensionAttributeValue
if ($attribute -notin $safeAttributes) {
if ($null -ne $attribute) {
Write-Host "$($entraObject.displayName) already has a value of '$attribute' configured in $extensionAttributeValue" -ForegroundColor Yellow
$attributeErrors = $attributeErrors + 1
}
}
}
if ($attributeErrors -gt 0) {
Write-Host "Please review the objects reporting as having existing data in the selected attribute $extensionAttributeValue, and run the script using a different attribute selection." -ForegroundColor Red
break
}
Write-Host "No issues found using the selected attribute $extensionAttributeValue for risk assignment." -ForegroundColor Green
Write-Host
#endregion pre-flight
#region Feature Update Readiness
Write-Host "Starting the Feature Update Readiness Report for Windows 11 $featureUpdateBuild..." -ForegroundColor Magenta
Write-Host
$startFeatureUpdateReport = New-ReportFeatureUpdateReadiness -JSON $featureUpdateCreateJSON
While ((Get-ReportFeatureUpdateReadiness -Id $startFeatureUpdateReport.id).status -ne 'completed') {
Write-Host 'Waiting for the Feature Update report to finish processing...' -ForegroundColor Cyan
Start-Sleep -Seconds $rndWait
}
Write-Host 'Feature Update Readiness report completed processing.' -ForegroundColor Green
Write-Host
Write-Host 'Getting Feature Update Report data...' -ForegroundColor Magenta
Write-Host
$featureUpdateReport = Get-ReportFeatureUpdateReadiness -JSON $featureUpdateGetJSON
Write-Host "Found Feature Update Report Details for $($featureUpdateReport.TotalRowCount) devices." -ForegroundColor Green
Write-Host
Write-Host "Processing data for 0 out of $($featureUpdateReport.TotalRowCount) devices..." -ForegroundColor Cyan
$featureUpdateReportDetails = @()
$featureUpdateReportDetails += $featureUpdateReport.Values
$i = 0
if ($($featureUpdateReport.TotalRowCount) -gt 50) {
while ($i -le $($featureUpdateReport.TotalRowCount)) {
$i = $i + 50
$getNextJSON = @"
{
"Id": "MEMUpgradeReadinessDevice_00000000-0000-0000-0000-000000000001",
"Skip": $i,
"Top": 50,
"Search": "",
"OrderBy": [],
"Select": [
"DeviceName",
"DeviceManufacturer",
"DeviceModel",
"OSVersion",
"ReadinessStatus",
"SystemRequirements",
"AppIssuesCount",
"DriverIssuesCount",
"AppOtherIssuesCount",
"DeviceId",
"AadDeviceId",
"Ownership"
],
"filter": ""
}
"@
# Sleep to stop throttling issues
Start-Sleep -Seconds $rndWait
$featureUpdateReportNext = Get-ReportFeatureUpdateReadiness -JSON $getNextJSON
Write-Host "Processing data for $i out of $($featureUpdateReport.TotalRowCount) devices..." -ForegroundColor Cyan
$featureUpdateReportDetails += $featureUpdateReportNext.Values
}
}
Write-Host "Processed data for $($featureUpdateReport.TotalRowCount) devices..." -ForegroundColor Green
Write-Host
Write-Host "Processing Windows 11 $featureUpdateBuild feature update readiness data for $($featureUpdateReport.TotalRowCount) devices..." -ForegroundColor Cyan
Write-Host
$reportArray = @()
foreach ($featureUpdateReportEntry in $featureUpdateReportDetails) {
$riskState = switch ($featureUpdateReportEntry[10]) {
'0' { "LowRisk-W11-$featureUpdateBuild" }
'1' { "MediumRisk-W11-$featureUpdateBuild" }
'2' { "HighRisk-W11-$featureUpdateBuild" }
'3' { "NotReady-W11-$featureUpdateBuild" }
'5' { "Unknown-W11-$featureUpdateBuild" }
}
$reportArray += [PSCustomObject]@{
'AadDeviceId' = $featureUpdateReportEntry[0]
'AppIssuesCount' = $featureUpdateReportEntry[1]
'AppOtherIssuesCount' = $featureUpdateReportEntry[2]
'DeviceId' = $featureUpdateReportEntry[3]
'DeviceManufacturer' = $featureUpdateReportEntry[4]
'DeviceModel' = $featureUpdateReportEntry[5]
'DeviceName' = $featureUpdateReportEntry[6]
'DriverIssuesCount' = $featureUpdateReportEntry[7]
'OSVersion' = $featureUpdateReportEntry[8]
'Ownership' = $featureUpdateReportEntry[9]
'ReadinessStatus' = $featureUpdateReportEntry[10]
'SystemRequirements' = $featureUpdateReportEntry[11]
'RiskState' = $riskState
'deviceObjectID' = $(($entraDevices | Where-Object { $_.deviceid -eq $featureUpdateReportEntry[0] }).id)
'userObjectID' = $(($intuneDevices | Where-Object { $_.azureActiveDirectoryDeviceId -eq $featureUpdateReportEntry[0] }).userId)
'userPrincipalName' = $(($intuneDevices | Where-Object { $_.azureActiveDirectoryDeviceId -eq $featureUpdateReportEntry[0] }).userPrincipalName)
}
}
$reportArray = $reportArray | Sort-Object -Property ReadinessStatus -Descending
Write-Host "Processed Windows 11 $featureUpdateBuild feature update readiness data for $($featureUpdateReport.TotalRowCount) devices." -ForegroundColor Green
Write-Host
#endregion Feature Update Readiness
#region Attributes
Write-Host "Starting the assignment of risk based extension attributes to $extensionAttributeValue" -ForegroundColor Magenta
Write-Host
Write-Warning 'Please confirm you are happy to continue.' -WarningAction Inquire
Write-Host
Write-Host "Assigning the Risk attributes to $extensionAttributeValue..." -ForegroundColor cyan
Write-Host
# users are a pain
if ($target -eq 'user') {
# Removes devices with no primary user
$userReportArray = $reportArray | Where-Object { $_.userPrincipalName -ne $null -and $_.userPrincipalName -ne '' } | Group-Object userPrincipalName
foreach ( $user in $userReportArray ) {
Start-Sleep -Seconds $rndWait
# one device for a user
if ($user.count -eq 1) {
$userObject = $user.Group
$riskColour = switch ($($userObject.ReadinessStatus)) {
'0' { 'Green' }
'1' { 'Yellow' }
'2' { 'Red' }
'3' { 'Red' }
'4' { 'Cyan' }
'5' { 'Magenta' }
}
$JSON = @"
{
"$extAttribute": {
"$extensionAttributeValue": "$($userObject.RiskState)"
}
}
"@
}
# Multiple devices for a user
else {
$userObject = $user.Group
# All user devices at Windows 11
if (($userObject.ReadinessStatus | Measure-Object -Sum).Sum / $user.count -eq 4) {
# Only need one device object as they're all Windows 11
$userObject = $user.Group | Select-Object -First 1
$riskColour = 'Cyan'
$JSON = @"
{
"$extAttribute": {
"$extensionAttributeValue": "$($userObject.RiskState)"
}
}
"@
}
else {
# Gets readiness state where not updated to Windows 11, selects highest risk number
$highestRisk = ($user.Group | Where-Object { $_.ReadinessStatus -ne 4 } | Measure-Object -Property ReadinessStatus -Maximum).Maximum
$userObject = ($user.Group | Where-Object { $_.ReadinessStatus -eq $highestRisk } | Select-Object -First 1)
$riskColour = switch ($($userObject.ReadinessStatus)) {
'0' { 'Green' }
'1' { 'Yellow' }
'2' { 'Red' }
'3' { 'Red' }
'4' { 'Cyan' }
'5' { 'Magenta' }
}
$JSON = @"
{
"$extAttribute": {
"$extensionAttributeValue": "$($userObject.RiskState)"
}
}
"@
}
}
If ($deploy) {
Add-ObjectAttribute -object User -Id $($userObject.userObjectID) -JSON $JSON
}
if ($($user.Group.ReadinessStatus) -eq 4) {
Write-Host "$($userObject.userPrincipalName) $extensionAttributeValue risk tag removed as already updated to Windows 11 $featureUpdateBuild" -ForegroundColor $riskColour
}
else {
Write-Host "$($userObject.userPrincipalName) assigned risk tag $($userObject.RiskState) to $extensionAttributeValue for Windows 11 $featureUpdateBuild" -ForegroundColor $riskColour
}
}
}
# devices
else {
Foreach ($device in $reportArray) {
$JSON = @"
{
"$extAttribute": {
"$extensionAttributeValue": "$($device.RiskState)"
}
}
"@
# Sleep to stop throttling issues
Start-Sleep -Seconds $rndWait
If ($deploy) {
Add-ObjectAttribute -object Device -Id $device.deviceObjectID -JSON $JSON
}
$riskColour = switch ($($device.ReadinessStatus)) {
'0' { 'Green' }
'1' { 'Yellow' }
'2' { 'Red' }
'3' { 'Red' }
'4' { 'Cyan' }
'5' { 'Magenta' }
}
if ($($device.ReadinessStatus) -eq 4) {
Write-Host "$($device.DeviceName) $extensionAttributeValue risk tag removed as already updated to Windows 11 $featureUpdateBuild" -ForegroundColor $riskColour
}
else {
Write-Host "$($device.DeviceName) assigned risk tag $($device.RiskState) to $extensionAttributeValue for Windows 11 $featureUpdateBuild" -ForegroundColor $riskColour
}
}
}
Write-Host
Write-Host "Completed the assignment of risk based extension attributes to $extensionAttributeValue" -ForegroundColor Green
Write-Host
#endregion Attributes
#Disconnect-MgGraph