-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostMigrate.ps1
440 lines (401 loc) · 10.8 KB
/
postMigrate.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
<# POSTMIGRATE.PS1
Synopsis
PostMigrate.ps1 is run after the migration reboots have completed and the user signs into the PC.
DESCRIPTION
This script is used to update the device group tag in Entra ID and set the primary user in Intune and migrate the bitlocker recovery key.
USE
.\postMigrate.ps1
.OWNER
Steve Weiner
.CONTRIBUTORS
Logan Lautt
Jesse Weimer
#>
$ErrorActionPreference = "SilentlyContinue"
# CMDLET FUNCTIONS
# set log function
function log()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$message
)
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss tt"
Write-Output "$ts $message"
}
# CMDLET FUNCTIONS
# START SCRIPT FUNCTIONS
# get json settings
function getSettingsJSON()
{
param(
[string]$json = "settings.json"
)
$global:settings = Get-Content -Path "$($PSScriptRoot)\$($json)" | ConvertFrom-Json
return $settings
}
# initialize script
function initializeScript()
{
Param(
[string]$logPath = $settings.logPath,
[string]$logName = "postMigrate.log",
[string]$localPath = $settings.localPath
)
Start-Transcript -Path "$logPath\$logName" -Verbose
log "Initializing script..."
if(!(Test-Path $localPath))
{
mkdir $localPath
log "Local path created: $localPath"
}
else
{
log "Local path already exists: $localPath"
}
$global:localPath = $localPath
$context = whoami
log "Running as $($context)"
log "Script initialized"
return $localPath
}
# disable post migrate task
function disablePostMigrateTask()
{
Param(
[string]$taskName = "postMigrate"
)
log "Disabling postMigrate task..."
Disable-ScheduledTask -TaskName $taskName -ErrorAction Stop
log "postMigrate task disabled"
}
# get device info
function getDeviceInfo()
{
Param(
[string]$hostname = $env:COMPUTERNAME,
[string]$serialNumber = (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber
)
$global:deviceInfo = @{
"hostname" = $hostname
"serialNumber" = $serialNumber
}
foreach($key in $deviceInfo.Keys)
{
log "$($key): $($deviceInfo[$key])"
}
}
# authenticate to MS Graph
function msGraphAuthenticate()
{
Param(
[string]$tenant = $settings.targetTenant.tenantName,
[string]$clientId = $settings.targetTenant.clientId,
[string]$clientSecret = $settings.targetTenant.clientSecret
)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "grant_type=client_credentials&scope=https://graph.microsoft.com/.default"
$body += -join ("&client_id=" , $clientId, "&client_secret=", $clientSecret)
$response = Invoke-RestMethod "https://login.microsoftonline.com/$tenant/oauth2/v2.0/token" -Method 'POST' -Headers $headers -Body $body
#Get Token form OAuth.
$token = -join ("Bearer ", $response.access_token)
#Reinstantiate headers.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $token)
$headers.Add("Content-Type", "application/json")
log "MS Graph Authenticated"
$global:headers = $headers
}
# get user graph info
function getGraphInfo()
{
Param(
[string]$regPath = $settings.regPath,
[string]$regKey = "Registry::$regPath",
[string]$serialNumber = $deviceInfo.serialNumber,
[string]$intuneUri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices",
[string]$newUserSID = (Get-ItemPropertyValue -Path $regKey -Name "NewUserSID"),
[string]$userUri = "https://graph.microsoft.com/beta/users",
[string]$upn = (Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\IdentityStore\Cache\$($newUserSID)\IdentityCache\$($newUserSID)" -Name "UserName")
)
log "Getting graph info..."
$intuneObject = Invoke-RestMethod -Uri "$($intuneUri)?`$filter=contains(serialNumber,'$($serialNumber)')" -Headers $headers -Method Get
if(($intuneObject.'@odata.count') -eq 1)
{
$global:intuneID = $intuneObject.value.id
$global:aadDeviceID = $intuneObject.value.azureADDeviceId
log "Intune Device ID: $intuneID, Azure AD Device ID: $aadDeviceID, User ID: $userID"
}
else
{
log "Intune object not found"
}
$userObject = Invoke-RestMethod -Uri "$userUri/$upn" -Headers $headers -Method Get
if(![string]::IsNullOrEmpty($userObject.id))
{
$global:userID = $userObject.id
log "User ID: $userID"
}
else
{
log "User object not found"
}
}
# set primary user
function setPrimaryUser()
{
Param(
[string]$intuneID = $intuneID,
[string]$userID = $userID,
[string]$userUri = "https://graph.microsoft.com/beta/users/$userID",
[string]$intuneDeviceRefUri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$intuneID/users/`$ref"
)
log "Setting primary user..."
$id = "@odata.id"
$JSON = @{ $id="$userUri" } | ConvertTo-Json
Invoke-RestMethod -Uri $intuneDeviceRefUri -Headers $headers -Method Post -Body $JSON
log "Primary user for $intuneID set to $userID"
}
# update device group tag
function updateGroupTag()
{
Param(
[string]$regPath = $settings.regPath,
[string]$regKey = "Registry::$regPath",
[string]$groupTag = (Get-ItemPropertyValue -Path $regKey -Name "GroupTag" -ErrorAction Ignore),
[string]$aadDeviceID = $aadDeviceID,
[string]$deviceUri = "https://graph.microsoft.com/beta/devices"
)
log "Updating device group tag..."
if([string]::IsNullOrEmpty($groupTag))
{
log "Group tag not found- will not be used."
}
else
{
$aadObject = Invoke-RestMethod -Method Get -Uri "$($deviceUri)?`$filter=deviceId eq '$($aadDeviceId)'" -Headers $headers
$physicalIds = $aadObject.value.physicalIds
$deviceId = $aadObject.value.id
$groupTag = "[OrderID]:$($groupTag)"
$physicalIds += $groupTag
$body = @{
physicalIds = $physicalIds
} | ConvertTo-Json
Invoke-RestMethod -Uri "$deviceUri/$deviceId" -Method Patch -Headers $headers -Body $body
log "Device group tag updated to $groupTag"
}
}
# migrate bitlocker function
function migrateBitlockerKey()
{
Param(
[string]$mountPoint = "C:",
[PSCustomObject]$bitLockerVolume = (Get-BitLockerVolume -MountPoint $mountPoint),
[string]$keyProtectorId = ($bitLockerVolume.KeyProtector | Where-Object {$_.KeyProtectorType -eq "RecoveryPassword"}).KeyProtectorId
)
log "Migrating Bitlocker key..."
if($bitLockerVolume.KeyProtector.count -gt 0)
{
BackupToAAD-BitLockerKeyProtector -MountPoint $mountPoint -KeyProtectorId $keyProtectorId
log "Bitlocker key migrated"
}
else
{
log "Bitlocker key not migrated"
}
}
# decrypt drive
function decryptDrive()
{
Param(
[string]$mountPoint = "C:"
)
Disable-BitLocker -MountPoint $mountPoint
log "Drive $mountPoint decrypted"
}
# manage bitlocker
function manageBitlocker()
{
Param(
[string]$bitlockerMethod = $settings.bitlockerMethod
)
log "Getting bitlocker action..."
if($bitlockerMethod -eq "Migrate")
{
migrateBitlockerKey
}
elseif($bitlockerMethod -eq "Decrypt")
{
decryptDrive
}
else
{
log "Bitlocker method not set. Skipping..."
}
}
# reset legal notice policy
function resetLockScreenCaption()
{
Param(
[string]$lockScreenRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System",
[string]$lockScreenCaption = "legalnoticecaption",
[string]$lockScreenText = "legalnoticetext"
)
log "Resetting lock screen caption..."
Remove-ItemProperty -Path $lockScreenRegPath -Name $lockScreenCaption -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $lockScreenRegPath -Name $lockScreenText -ErrorAction SilentlyContinue
log "Lock screen caption reset"
}
# remove migration user
function removeMigrationUser()
{
Param(
[string]$migrationUser = "MigrationInProgress"
)
Remove-LocalUser -Name $migrationUser -ErrorAction Stop
log "Migration user removed"
}
# END SCRIPT FUNCTIONS
# START SCRIPT
# get settings
try
{
getSettingsJSON
log "Retrieved settings"
}
catch
{
$message = $_.Exception.Message
log "Settings not loaded: $message"
log "Exiting script"
Exit 1
}
# initialize script
try
{
initializeScript
log "Script initialized"
}
catch
{
$message = $_.Exception.Message
log "Script not initialized: $message"
log "Exiting script"
Exit 1
}
# disable post migrate task
try
{
disablePostMigrateTask
log "Post migrate task disabled"
}
catch
{
$message = $_.Exception.Message
log "Post migrate task not disabled: $message"
log "Exiting script"
Exit 1
}
# get device info
try
{
getDeviceInfo
log "Device info retrieved"
}
catch
{
$message = $_.Exception.Message
log "Device info not retrieved: $message"
log "Exiting script"
Exit 1
}
# authenticate to MS Graph
try
{
msGraphAuthenticate
log "MS Graph authenticated"
}
catch
{
$message = $_.Exception.Message
log "MS Graph not authenticated: $message"
log "Exiting script"
Exit 1
}
# get graph info
try
{
getGraphInfo
log "Graph info retrieved"
}
catch
{
$message = $_.Exception.Message
log "Graph info not retrieved: $message"
log "Exiting script"
Exit 1
}
# set primary user
try
{
setPrimaryUser
log "Primary user set"
}
catch
{
$message = $_.Exception.Message
log "Primary user not set: $message"
log "WARNING: Primary user not set- try manually setting in Intune"
}
# update device group tag
try
{
updateGroupTag
log "Device group tag updated if applicable"
}
catch
{
$message = $_.Exception.Message
log "Device group tag not updated: $message"
log "WARNING: Device group tag not updated- try manually updating in Intune"
}
# manage bitlocker
try
{
manageBitlocker
log "Bitlocker managed"
}
catch
{
$message = $_.Exception.Message
log "Bitlocker not managed: $message"
log "WARNING: Bitlocker not managed- try setting policy manually in Intune"
}
# reset lock screen caption
try
{
resetLockScreenCaption
log "Lock screen caption reset"
}
catch
{
$message = $_.Exception.Message
log "Lock screen caption not reset: $message"
log "WARNING: Lock screen caption not reset- try setting manually"
}
# remove migration user
try
{
removeMigrationUser
log "Migration user removed"
}
catch
{
$message = $_.Exception.Message
log "Migration user not removed: $message"
log "WARNING: Migration user not removed- try removing manually"
}
# END SCRIPT
Stop-Transcript