-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathReset-IntuneDevice.ps1
234 lines (177 loc) · 8.45 KB
/
Reset-IntuneDevice.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
function Reset-IntuneDevice {
<#
IMPORTANT:
===========================================================================
This script is provided 'as is' without any warranty. Any issues stemming
from use is on the user.
===========================================================================
.DESCRIPTION
Resets an Intune managed device.
For more info on Wipe vs. Retire vs. Delete, see https://karstenkleinschmidt.de/2020/09/09/intune-what-is-retire-wipe-delete-fresh-start-autopilot-reset/
Things to change to deploy in your environment:
Line 78: replace x with clientID of your reigstered app. See https://bit.ly/3KApKhJ for more info.
===========================================================================
.PARAMETER Device
Required - Name of the device to reset.
.PARAMETER Wipe
Initiates Wipe on specified device at next sync. Use KeepUserData switch if you'd like to keep user data.
.PARAMETER KeepUserData
Use this switch when wiping a device to keep user data (i.e., for a reinstall of Windows)
.PARAMETER Retire
Initiates Retire on specified device - device will not be removed from Intune until next sync.
.PARAMETER AutoPilot
Initiates an AutoPilot reset on specified device.
===========================================================================
.EXAMPLE
Reset-IntuneDevice -Device DESKTOP-7XH8Z -Wipe -KeepUserData <--- Wipes DESKTOP-7XH8Z and keeps user data
Reset-IntuneDevice -Device DESKTOP-7XH8Z -Wipe <--- Wipes DESKTOP-7XH8Z and does NOT keep user data
Reset-IntuneDevice -Device DESKTOP-7XH8Z -Retire <--- Retires DESKTOP-7XH8Z at next sync
Reset-IntuneDevice -Device DESKTOP-7XH8Z -AutoPilot <--- AutoPilot resets DESKTOP-7XH8Z
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Device,
[Parameter()]
[Switch]$Wipe,
[Parameter()]
[Switch]$KeepUserData,
[Parameter()]
[Switch]$Retire,
[Parameter()]
[Switch]$Autopilot
)
function Get-IntuneDevice {
<#
IMPORTANT:
===========================================================================
This script is provided 'as is' without any warranty. Any issues stemming
from use is on the user.
===========================================================================
.DESCRIPTION
Retrieves Intune managed device.
===========================================================================
.PARAMETER Name
Required if not using All switch - Name of the device to retrieve.
.PARAMETER All
Retrieves all Intune managed apps.
===========================================================================
.EXAMPLE
Assign-IntuneDevice -Name DESKTOP-7XH8Z <--- Retrieves DESKTOP-7XH8Z if exist
Assign-IntuneDevice -All <--- Retrieves all Intune managed devices.
#>
[CmdletBinding()]
param (
[Parameter()]
[String]$Name,
[Parameter()]
[Switch]$All
)
$token = Get-MsalToken -clientid x -tenantid organizations
$global:header = @{'Authorization' = $token.createauthorizationHeader();'ConsistencyLevel' = 'eventual'}
If (!$All -and $Name){
$Uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$filter=deviceName%20eq%20'$Name'"
Try {
$Device = (Invoke-RestMethod -Uri $Uri -Method Get -Headers $Header).value
If ($Device -ne $Null) {
$device
}
else {
Write-Host "Device $Name not found." -f Red
}
}
catch{
$ResponseResult = $_.Exception.Response.GetResponseStream()
$ResponseReader = New-Object System.IO.StreamReader($ResponseResult)
$ResponseBody = $ResponseReader.ReadToEnd()
$ResponseBody
}
}
elseif (!$All -and !$Name){
Write-Host "Please specify a device using the -Name parameter or use the -All switch to see all devices." -f Red
}
elseif ($All -and !$Name){
$Uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$filter=(((deviceType%20eq%20%27desktop%27)%20or%20(deviceType%20eq%20%27windowsRT%27)%20or%20(deviceType%20eq%20%27winEmbedded%27)%20or%20(deviceType%20eq%20%27surfaceHub%27)))"
Try {
(Invoke-RestMethod -uri $uri -headers $header -method GET).value
}
catch{
$ResponseResult = $_.Exception.Response.GetResponseStream()
$ResponseReader = New-Object System.IO.StreamReader($ResponseResult)
$ResponseBody = $ResponseReader.ReadToEnd()
$ResponseBody
}
}
else{
Write-Host "Please specify a device using the -Name parameter or use the -All switch to see all devices." -f Red
}
}
#################################################################
If ($Wipe -and !$KeepUserData){
$IntuneDevice = Get-IntuneDevice -Name $Device
$Uri = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$($IntuneDevice.Id)/wipe"
$Body = @{
"keepUserData" = $False;
"keepEnrollmentData" = $False
}
$JSON = $Body | ConvertTo-Json
Try {
Invoke-RestMethod -Uri $Uri -Headers $Header -Method POST -Body $JSON -ContentType "application/Json"
}
catch{
$ResponseResult = $_.Exception.Response.GetResponseStream()
$ResponseReader = New-Object System.IO.StreamReader($ResponseResult)
$ResponseBody = $ResponseReader.ReadToEnd()
$ResponseBody
}
}
elseif ($Wipe -and $KeepUserData){
$IntuneDevice = Get-IntuneDevice -Name $Device
$Uri = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$($IntuneDevice.Id)/wipe"
$Body = @{
"keepUserData" = $true;
"keepEnrollmentData" = $true
}
$JSON = $Body | ConvertTo-Json
Try {
Invoke-RestMethod -Uri $Uri -Headers $Header -Method POST -Body $JSON -ContentType "application/Json"
}
catch{
$ResponseResult = $_.Exception.Response.GetResponseStream()
$ResponseReader = New-Object System.IO.StreamReader($ResponseResult)
$ResponseBody = $ResponseReader.ReadToEnd()
$ResponseBody
}
}
elseif ($Retire){
$IntuneDevice = Get-IntuneDevice -Name $Device
$Uri = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$($IntuneDevice.Id)/retire"
Try {
Invoke-RestMethod -Uri $Uri -Headers $Header -Method POST
}
catch{
$ResponseResult = $_.Exception.Response.GetResponseStream()
$ResponseReader = New-Object System.IO.StreamReader($ResponseResult)
$ResponseBody = $ResponseReader.ReadToEnd()
$ResponseBody
}
}
elseif ($Autopilot){
$IntuneDevice = Get-IntuneDevice -Name $Device
$Uri = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$($IntuneDevice.Id)/wipe"
$Body = @{
"keepUserData" = $False;
"keepEnrollmentData" = $True
}
$JSON = $Body | ConvertTo-Json
Try {
Invoke-RestMethod -Uri $Uri -Headers $Header -Method POST -Body $JSON -ContentType "application/Json"
}
catch{
$ResponseResult = $_.Exception.Response.GetResponseStream()
$ResponseReader = New-Object System.IO.StreamReader($ResponseResult)
$ResponseBody = $ResponseReader.ReadToEnd()
$ResponseBody
}
}
}