-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-WindowsAutoPilotInfo.ps1
465 lines (417 loc) · 17.6 KB
/
Get-WindowsAutoPilotInfo.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
<#PSScriptInfo
.VERSION 3.9
.GUID ebf446a3-3362-4774-83c0-b7299410b63f
.AUTHOR Windows Autopilot
.COMPANYNAME Microsoft
.COPYRIGHT
.TAGS Windows AutoPilot
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
Version 1.0: Original published version.
Version 1.1: Added -Append switch.
Version 1.2: Added -Credential switch.
Version 1.3: Added -Partner switch.
Version 1.4: Switched from Get-WMIObject to Get-CimInstance.
Version 1.5: Added -GroupTag parameter.
Version 1.6: Bumped version number (no other change).
Version 2.0: Added -Online parameter.
Version 2.1: Bug fix.
Version 2.3: Updated comments.
Version 2.4: Updated "online" import logic to wait for the device to sync, added new parameter.
Version 2.5: Added AssignedUser for Intune importing, and AssignedComputerName for online Intune importing.
Version 2.6: Added support for app-based authentication via Connect-MSGraphApp.
Version 2.7: Added new Reboot option for use with -Online -Assign.
Version 2.8: Fixed up parameter sets.
Version 2.9: Fixed typo installing AzureAD module.
Version 3.0: Fixed typo for app-based auth, added logic to explicitly install NuGet (silently).
Version 3.2: Fixed logic to explicitly install NuGet (silently).
Version 3.3: Added more logging and error handling for group membership.
Version 3.4: Added logic to verify that devices were added successfully. Fixed a bug that could cause all Autopilot devices to be added to the specified AAD group.
Version 3.5: Added logic to display the serial number of the gathered device.
Version 3.6: Switch from MSGraph to MgGraph.
Version 3.7: Switching back to using Write-Host.
Version 3.8: AddToGroup dependencies fixed.
Version 3.9: Expand MgGraph scopes and enforce minimum version for WindowsAutoPilotIntune module.
#>
<#
.SYNOPSIS
Retrieves the Windows AutoPilot deployment details from one or more computers
MIT LICENSE
Copyright (c) 2023 Microsoft
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.DESCRIPTION
This script uses WMI to retrieve properties needed for a customer to register a device with Windows Autopilot. Note that it is normal for the resulting CSV file to not collect a Windows Product ID (PKID) value since this is not required to register a device. Only the serial number and hardware hash will be populated.
.PARAMETER Name
The names of the computers. These can be provided via the pipeline (property name Name or one of the available aliases, DNSHostName, ComputerName, and Computer).
.PARAMETER OutputFile
The name of the CSV file to be created with the details for the computers. If not specified, the details will be returned to the PowerShell
pipeline.
.PARAMETER Append
Switch to specify that new computer details should be appended to the specified output file, instead of overwriting the existing file.
.PARAMETER Credential
Credentials that should be used when connecting to a remote computer (not supported when gathering details from the local computer).
.PARAMETER Partner
Switch to specify that the created CSV file should use the schema for Partner Center (using serial number, make, and model).
.PARAMETER GroupTag
An optional tag value that should be included in a CSV file that is intended to be uploaded via Intune (not supported by Partner Center or Microsoft Store for Business).
.PARAMETER AssignedUser
An optional value specifying the UPN of the user to be assigned to the device. This can only be specified for Intune (not supported by Partner Center or Microsoft Store for Business).
.PARAMETER Online
Add computers to Windows Autopilot via the Intune Graph API
.PARAMETER AssignedComputerName
An optional value specifying the computer name to be assigned to the device. This can only be specified with the -Online switch and only works with AAD join scenarios.
.PARAMETER AddToGroup
Specifies the name of the Azure AD group that the new device should be added to.
.PARAMETER Assign
Wait for the Autopilot profile assignment. (This can take a while for dynamic groups.)
.PARAMETER Reboot
Reboot the device after the Autopilot profile has been assigned (necessary to download the profile and apply the computer name, if specified).
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv -GroupTag Kiosk
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv -GroupTag Kiosk -AssignedUser [email protected]
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv -Append
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER1,MYCOMPUTER2 -OutputFile .\MyComputers.csv
.EXAMPLE
Get-ADComputer -Filter * | .\GetWindowsAutoPilotInfo.ps1 -OutputFile .\MyComputers.csv
.EXAMPLE
Get-CMCollectionMember -CollectionName "All Systems" | .\GetWindowsAutoPilotInfo.ps1 -OutputFile .\MyComputers.csv
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER1,MYCOMPUTER2 -OutputFile .\MyComputers.csv -Partner
.EXAMPLE
.\GetWindowsAutoPilotInfo.ps1 -Online
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
param(
[Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Position=0)][alias("DNSHostName","ComputerName","Computer")] [String[]] $Name = @("localhost"),
[Parameter(Mandatory=$False)] [String] $OutputFile = "",
[Parameter(Mandatory=$False)] [String] $GroupTag = "",
[Parameter(Mandatory=$False)] [String] $AssignedUser = "",
[Parameter(Mandatory=$False)] [Switch] $Append = $false,
[Parameter(Mandatory=$False)] [System.Management.Automation.PSCredential] $Credential = $null,
[Parameter(Mandatory=$False)] [Switch] $Partner = $false,
[Parameter(Mandatory=$False)] [Switch] $Force = $false,
[Parameter(Mandatory=$True,ParameterSetName = 'Online')] [Switch] $Online = $false,
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [String] $TenantId = "",
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [String] $AppId = "",
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [String] $AppSecret = "",
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [String] $AddToGroup = "",
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [String] $AssignedComputerName = "",
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [Switch] $Assign = $false,
[Parameter(Mandatory=$False,ParameterSetName = 'Online')] [Switch] $Reboot = $false
)
Begin
{
# Initialize empty list
$computers = @()
# If online, make sure we are able to authenticate
if ($Online) {
# Get NuGet
$provider = Get-PackageProvider NuGet -ErrorAction Ignore
if (-not $provider) {
Write-Host "Installing provider NuGet"
Find-PackageProvider -Name NuGet -ForceBootstrap -IncludeDependencies
}
# Get WindowsAutopilotIntune module (and dependencies)
$module = Import-Module WindowsAutopilotIntune -MinimumVersion 5.4.0 -PassThru -ErrorAction Ignore
if (-not $module) {
Write-Host "Installing module WindowsAutopilotIntune"
Install-Module WindowsAutopilotIntune -Force
}
Import-Module WindowsAutopilotIntune -Scope Global
# Get Graph Authentication module (and dependencies)
$module = Import-Module microsoft.graph.authentication -PassThru -ErrorAction Ignore
if (-not $module) {
Write-Host "Installing module microsoft.graph.authentication"
Install-Module microsoft.graph.authentication -Force
}
Import-Module microsoft.graph.authentication -Scope Global
# Get required modules for AddToGroup switch
if ($AddToGroup)
{
$module = Import-Module Microsoft.Graph.Groups -PassThru -ErrorAction Ignore
if (-not $module)
{
Write-Host "Installing module Microsoft.Graph.Groups"
Install-Module Microsoft.Graph.Groups -Force
}
$module = Import-Module Microsoft.Graph.Identity.DirectoryManagement -PassThru -ErrorAction Ignore
if (-not $module)
{
Write-Host "Installing module Microsoft.Graph.Identity.DirectoryManagement"
Install-Module Microsoft.Graph.Identity.DirectoryManagement -Force
}
}
# Connect
if ($AppId -ne "")
{
$graph = Connect-MSGraphApp -Tenant $TenantId -AppId $AppId -AppSecret $AppSecret
Write-Host "Connected to Intune tenant $TenantId using app-based authentication (Azure AD authentication not supported)"
}
else {
Connect-MgGraph -Scopes "DeviceManagementServiceConfig.ReadWrite.All", "DeviceManagementManagedDevices.ReadWrite.All", "Device.ReadWrite.All", "Group.ReadWrite.All", "GroupMember.ReadWrite.All"
$graph = Get-MgContext
Write-Host "Connected to Intune tenant" $graph.TenantId
}
# Force the output to a file
if ($OutputFile -eq "")
{
$OutputFile = "$($env:TEMP)\autopilot.csv"
}
}
}
Process
{
foreach ($comp in $Name)
{
$bad = $false
# Get a CIM session
if ($comp -eq "localhost") {
$session = New-CimSession
}
else
{
$session = New-CimSession -ComputerName $comp -Credential $Credential
}
# Get the common properties.
Write-Verbose "Checking $comp"
$serial = (Get-CimInstance -CimSession $session -Class Win32_BIOS).SerialNumber
# Get the hash (if available)
$devDetail = (Get-CimInstance -CimSession $session -Namespace root/cimv2/mdm/dmmap -Class MDM_DevDetail_Ext01 -Filter "InstanceID='Ext' AND ParentID='./DevDetail'")
if ($devDetail -and (-not $Force))
{
$hash = $devDetail.DeviceHardwareData
}
else
{
$bad = $true
$hash = ""
}
# If the hash isn't available, get the make and model
if ($bad -or $Force)
{
$cs = Get-CimInstance -CimSession $session -Class Win32_ComputerSystem
$make = $cs.Manufacturer.Trim()
$model = $cs.Model.Trim()
if ($Partner)
{
$bad = $false
}
}
else
{
$make = ""
$model = ""
}
# Getting the PKID is generally problematic for anyone other than OEMs, so let's skip it here
$product = ""
# Depending on the format requested, create the necessary object
if ($Partner)
{
# Create a pipeline object
$c = New-Object psobject -Property @{
"Device Serial Number" = $serial
"Windows Product ID" = $product
"Hardware Hash" = $hash
"Manufacturer name" = $make
"Device model" = $model
}
# From spec:
# "Manufacturer Name" = $make
# "Device Name" = $model
}
else
{
# Create a pipeline object
$c = New-Object psobject -Property @{
"Device Serial Number" = $serial
"Windows Product ID" = $product
"Hardware Hash" = $hash
}
if ($GroupTag -ne "")
{
Add-Member -InputObject $c -NotePropertyName "Group Tag" -NotePropertyValue $GroupTag
}
if ($AssignedUser -ne "")
{
Add-Member -InputObject $c -NotePropertyName "Assigned User" -NotePropertyValue $AssignedUser
}
}
# Write the object to the pipeline or array
if ($bad)
{
# Report an error when the hash isn't available
Write-Error -Message "Unable to retrieve device hardware data (hash) from computer $comp" -Category DeviceError
}
elseif ($OutputFile -eq "")
{
$c
}
else
{
$computers += $c
Write-Host "Gathered details for device with serial number: $serial"
}
Remove-CimSession $session
}
}
End
{
if ($OutputFile -ne "")
{
if ($Append)
{
if (Test-Path $OutputFile)
{
$computers += Import-CSV -Path $OutputFile
}
}
if ($Partner)
{
$computers | Select-Object "Device Serial Number", "Windows Product ID", "Hardware Hash", "Manufacturer name", "Device model" | ConvertTo-CSV -NoTypeInformation | ForEach-Object {$_ -replace '"',''} | Out-File $OutputFile
}
elseif ($AssignedUser -ne "")
{
$computers | Select-Object "Device Serial Number", "Windows Product ID", "Hardware Hash", "Group Tag", "Assigned User" | ConvertTo-CSV -NoTypeInformation | ForEach-Object {$_ -replace '"',''} | Out-File $OutputFile
}
elseif ($GroupTag -ne "")
{
$computers | Select-Object "Device Serial Number", "Windows Product ID", "Hardware Hash", "Group Tag" | ConvertTo-CSV -NoTypeInformation | ForEach-Object {$_ -replace '"',''} | Out-File $OutputFile
}
else
{
$computers | Select-Object "Device Serial Number", "Windows Product ID", "Hardware Hash" | ConvertTo-CSV -NoTypeInformation | ForEach-Object {$_ -replace '"',''} | Out-File $OutputFile
}
}
if ($Online)
{
# Add the devices
$importStart = Get-Date
$imported = @()
$computers | ForEach-Object {
$imported += Add-AutopilotImportedDevice -serialNumber $_.'Device Serial Number' -hardwareIdentifier $_.'Hardware Hash' -groupTag $_.'Group Tag' -assignedUser $_.'Assigned User'
}
# Wait until the devices have been imported
$processingCount = 1
while ($processingCount -gt 0)
{
$current = @()
$processingCount = 0
$imported | ForEach-Object {
$device = Get-AutopilotImportedDevice -id $_.id
if ($device.state.deviceImportStatus -eq "unknown") {
$processingCount = $processingCount + 1
}
$current += $device
}
$deviceCount = $imported.Length
Write-Host "Waiting for $processingCount of $deviceCount to be imported"
if ($processingCount -gt 0){
Start-Sleep 30
}
}
$importDuration = (Get-Date) - $importStart
$importSeconds = [Math]::Ceiling($importDuration.TotalSeconds)
$successCount = 0
$current | ForEach-Object {
Write-Host "$($device.serialNumber): $($device.state.deviceImportStatus) $($device.state.deviceErrorCode) $($device.state.deviceErrorName)"
if ($device.state.deviceImportStatus -eq "complete") {
$successCount = $successCount + 1
}
}
Write-Host "$successCount devices imported successfully. Elapsed time to complete import: $importSeconds seconds"
# Wait until the devices can be found in Intune (should sync automatically)
$syncStart = Get-Date
$processingCount = 1
while ($processingCount -gt 0)
{
$autopilotDevices = @()
$processingCount = 0
$current | ForEach-Object {
if ($device.state.deviceImportStatus -eq "complete") {
$device = Get-AutopilotDevice -id $_.state.deviceRegistrationId
if (-not $device) {
$processingCount = $processingCount + 1
}
$autopilotDevices += $device
}
}
$deviceCount = $autopilotDevices.Length
Write-Host "Waiting for $processingCount of $deviceCount to be synced"
if ($processingCount -gt 0){
Start-Sleep 30
}
}
$syncDuration = (Get-Date) - $syncStart
$syncSeconds = [Math]::Ceiling($syncDuration.TotalSeconds)
Write-Host "All devices synced. Elapsed time to complete sync: $syncSeconds seconds"
# Add the device to the specified AAD group
if ($AddToGroup)
{
$aadGroup = Get-MgGroup -Filter "DisplayName eq '$AddToGroup'"
if ($aadGroup)
{
$autopilotDevices | ForEach-Object {
$aadDevice = Get-MgDevice -Search "deviceId:$($_.azureActiveDirectoryDeviceId)" -ConsistencyLevel eventual
if ($aadDevice) {
Write-Host "Adding device $($_.serialNumber) to group $AddToGroup"
New-MgGroupMember -GroupId $($aadGroup.Id) -DirectoryObjectId $($aadDevice.Id)
Write-Host "Added devices to group '$AddToGroup' $($aadGroup.Id)"
}
else {
Write-Error "Unable to find Azure AD device with ID $($_.azureActiveDirectoryDeviceId)"
}
}
}
else {
Write-Error "Unable to find group $AddToGroup"
}
}
# Assign the computer name
if ($AssignedComputerName -ne "")
{
$autopilotDevices | ForEach-Object {
Set-AutopilotDevice -id $_.id -displayName $AssignedComputerName
}
}
# Wait for assignment (if specified)
if ($Assign)
{
$assignStart = Get-Date
$processingCount = 1
while ($processingCount -gt 0)
{
$processingCount = 0
$autopilotDevices | ForEach-Object {
$device = Get-AutopilotDevice -id $_.id -Expand
if (-not ($device.deploymentProfileAssignmentStatus.StartsWith("assigned"))) {
$processingCount = $processingCount + 1
}
}
$deviceCount = $autopilotDevices.Length
Write-Host "Waiting for $processingCount of $deviceCount to be assigned"
if ($processingCount -gt 0){
Start-Sleep 30
}
}
$assignDuration = (Get-Date) - $assignStart
$assignSeconds = [Math]::Ceiling($assignDuration.TotalSeconds)
Write-Host "Profiles assigned to all devices. Elapsed time to complete assignment: $assignSeconds seconds"
if ($Reboot)
{
Restart-Computer -Force
}
}
}
}