-
Notifications
You must be signed in to change notification settings - Fork 4
/
import-autoPilotDevice.ps1
174 lines (157 loc) · 6.09 KB
/
import-autoPilotDevice.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
<#
.DESCRIPTION
Imports the device this script runs on into Intune Autopilot automatically and triggers a sync.
.NOTES
filename: import-autoPilotDevice.ps1
author: Jos Lieben (Lieben Consultancy)
created: 16/06/2021
last updated: 16/06/2021
copyright: 2021, Jos Lieben, Lieben Consultancy, free to use and modify, not for resale
#>
$clientId = "d1ddf0e4-d672-4dae-b554-9d5bdfd93547"
$userUPN = Read-Host -Prompt "Please type your username"
$tenantId = (Invoke-RestMethod "https://login.windows.net/$($userUPN.Split("@")[1])/.well-known/openid-configuration" -Method GET).userinfo_endpoint.Split("/")[3]
$response = Invoke-RestMethod -Method POST -UseBasicParsing -Uri "https://login.microsoftonline.com/$tenantId/oauth2/devicecode" -ContentType "application/x-www-form-urlencoded" -Body "resource=https%3A%2F%2Fgraph.microsoft.com&client_id=$clientId"
Write-Output $response.message
$waited = 0
while($true){
try{
$authResponse = Invoke-RestMethod -uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -ContentType "application/x-www-form-urlencoded" -Method POST -Body "grant_type=device_code&resource=https%3A%2F%2Fgraph.microsoft.com&code=$($response.device_code)&client_id=$clientId" -ErrorAction Stop
$refreshToken = $authResponse.refresh_token
break
}catch{
if($waited -gt 300){
Write-Verbose "No valid login detected within 5 minutes"
Throw
}
#try again
Start-Sleep -s 5
$waited += 5
}
}
Function Get-AutoPilotImportedDevice(){
[cmdletbinding()]
param([Parameter(Mandatory=$false)]$id)
if ($id) {
$uri = "https://graph.microsoft.com/v1.0/deviceManagement/importedWindowsAutopilotDeviceIdentities/$id"
}else {
$uri = "https://graph.microsoft.com/v1.0/deviceManagement/importedWindowsAutopilotDeviceIdentities"
}
try {
$devices = @()
$response = Invoke-RestMethod -Uri $uri -Headers @{"Authorization" = "Bearer $($authResponse.access_token)"} -Method Get
if ($id) {
$devices+=$response
}else {
$devices+=$response.value
while($response.'@odata.nextLink'){
$response = Invoke-RestMethod -Uri $response.'@odata.nextLink' -Headers @{"Authorization" = "Bearer $($authResponse.access_token)"} -Method Get
$devices+=$response.value
}
}
$devices
}catch {
$_
Exit
}
}
Function Get-AutoPilotDevice(){
[cmdletbinding()]
param([Parameter(Mandatory=$false)]$id)
if ($id) {
$uri = "https://graph.microsoft.com/v1.0/deviceManagement/windowsAutopilotDeviceIdentities/$id"
}else {
$uri = "https://graph.microsoft.com/v1.0/deviceManagement/windowsAutopilotDeviceIdentities"
}
try {
$devices = @()
$response = Invoke-RestMethod -Uri $uri -Headers @{"Authorization" = "Bearer $($authResponse.access_token)"} -Method Get
if ($id) {
$devices+=$response
}else {
$devices+=$response.value
while($response.'@odata.nextLink'){
$response = Invoke-RestMethod -Uri $response.'@odata.nextLink' -Headers @{"Authorization" = "Bearer $($authResponse.access_token)"} -Method Get
$devices+=$response.value
}
}
$devices
}catch {
$_
Exit
}
}
Function Add-AutoPilotImportedDevice(){
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)] $serialNumber,
[Parameter(Mandatory=$true)] $hardwareIdentifier
)
$json = @"
{
"@odata.type": "#microsoft.graph.importedWindowsAutopilotDeviceIdentity",
"orderIdentifier": "",
"serialNumber": "$serialNumber",
"productKey": "",
"hardwareIdentifier": "$hardwareIdentifier",
"state": {
"@odata.type": "microsoft.graph.importedWindowsAutopilotDeviceIdentityState",
"deviceImportStatus": "pending",
"deviceRegistrationId": "",
"deviceErrorCode": 0,
"deviceErrorName": ""
}
}
"@
try {
Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/deviceManagement/importedWindowsAutopilotDeviceIdentities" -Headers @{"Authorization" = "Bearer $($authResponse.access_token)"} -Method Post -Body $json -ContentType "application/json"
}catch {
$_
break
}
}
Function Invoke-AutopilotSync(){
[cmdletbinding()]
param()
$uri = "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotSettings/sync"
try {
$response = Invoke-RestMethod -Uri $uri -Headers @{"Authorization" = "Bearer $($authResponse.access_token)"} -Method Post
$response.Value
}catch {
$_
break
}
}
try{
$session = New-CimSession
$serial = (Get-CimInstance -CimSession $session -Class Win32_BIOS).SerialNumber
$devDetail = (Get-CimInstance -CimSession $session -Namespace root/cimv2/mdm/dmmap -Class MDM_DevDetail_Ext01 -Filter "InstanceID='Ext' AND ParentID='./DevDetail'")
if($devDetail){
$hash = $devDetail.DeviceHardwareData
}else{
Write-Error "Failed to get device hardware hash! Cannot continue" -ErrorAction Stop
}
Remove-CimSession $session
}catch{
Write-Host $_
Exit
}
$alreadyImportedDevices = Get-AutoPilotImportedDevice
if($alreadyImportedDevices.serialNumber -contains $serial){
Write-Host "This device has already been imported"
}else{
Add-AutoPilotImportedDevice -serialNumber $serial -hardwareIdentifier $hash
}
Invoke-AutopilotSync
Write-Host "Waiting 120 seconds for AutoPilot Sync"
Start-Sleep -Seconds 120
Write-Host "Sync completed"
$autopilotDevices = Get-AutoPilotDevice | Where-Object {$_.serialNumber -eq $serial}
if($autopilotDevices){
Write-Host "Your device has synced to Autopilot and is ready for its next user!"
foreach($device in $autopilotDevices){
Write-Host "$($device.serialNumber) $($device.model) $($device.manufacturer) $($device.systemFamily)"
}
}else{
Write-Error "The device was not detected in autopilot, the import may be delayed or have failed" -ErrorAction Stop
}