-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathAdminSerivce-GetBitlockerRecoveryKey.ps1
More file actions
77 lines (69 loc) · 2.71 KB
/
AdminSerivce-GetBitlockerRecoveryKey.ps1
File metadata and controls
77 lines (69 loc) · 2.71 KB
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
[cmdletbinding()]
param(
[int]$ResourceID,
[string]$SMSProvider
)
#region AdminService URLs
[string]$VersionedBaseUrl = "https://$($SMSProvider)/AdminService/v1.0"
[string]$DeviceClassURL = "$($VersionedBaseUrl)/Device"
#endregion
#AdminService
$asGetParams = @{
Method = "GET"
ContentType = "application/json"
ErrorAction = "SilentlyContinue"
UseDefaultCredentials = $True
}
$asPostParams = @{
Method = "POST"
ContentType = "application/json"
ErrorAction = "SilentlyContinue"
UseDefaultCredentials = $True
}
#endregion
#region Get ConfigMgr Device
try{
$asGetParams["URI"] = "$($DeviceClassURL)($($ResourceID))"
$CMDeviceResponse = Invoke-RestMethod @asGetParams
if ($CMDeviceResponse) {
$CMDevice = $CMDeviceResponse
Write-Output "Found ConfigMgr Device: $($CMDevice.Name)"
}
}
catch {
Write-Output "No CM Device found for device: $($ManagedDevice.deviceName)"
}
if ($CMDevice) {
try {
$asGetParams["URI"] = "$($DeviceClassURL)($($CMDevice.MachineId))/RecoveryKeys"
$RecoveryKeyResponse = Invoke-RestMethod @asGetParams
$RecoveryKeys = $RecoveryKeyResponse.value
if ($RecoveryKeys) {
foreach ($key in $RecoveryKeys) {
try {
$asPostParams["Body"] = @{
RecoveryKeyId = $Key.RecoveryKeyId
} | ConvertTo-Json
$asPostParams["URI"] = "$($DeviceClassURL)($($CMDevice.MachineId))/AdminService.GetRecoveryKeyValue"
$GetRecoveryKeyValueResponse = Invoke-RestMethod @asPostParams
$KeyObject = [PSCustomObject]@{
ResourceId = $CMDevice.MachineId
DeviceName = $CMDevice.Name
ItemKey = $key.ItemKey
RecoveryKeyId = $key.RecoveryKeyId
VolumeTypeId = $key.VolumeTypeId
RecoveryKey = if($GetRecoveryKeyValueResponse.value) {$GetRecoveryKeyValueResponse.value} else {$null}
}
$KeyObject
}
catch {
Throw $_
}
}
}
}
catch {
$RecoveryKeys = $null
}
}
#endregion