Skip to content

Instantly share code, notes, and snippets.

@anwather
Last active July 22, 2024 04:44
Show Gist options
  • Select an option

  • Save anwather/7f6f8fd599fb7ad3267ac76e3f3dc84a to your computer and use it in GitHub Desktop.

Select an option

Save anwather/7f6f8fd599fb7ad3267ac76e3f3dc84a to your computer and use it in GitHub Desktop.
function Get-EntraBitLockerKeys {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, HelpMessage = "Device name to retrieve the BitLocker keys from Microsoft Entra ID", ValueFromPipeline = $true)]
[string]$DeviceName
[Parameter(Mandatory = $false, HelpMessage = "Device Id to retrieve the BitLocker keys from Microsoft Entra ID", ValueFromPipeline = $true)]
[string]$DeviceId
)
Process {
if ($DeviceId -ne $null) {
# Assume the device ID is already provided in params....
}
else {
$DeviceID = (Get-MGDevice -filter "displayName eq '$DeviceName'").DeviceId
}
if ($DeviceID) {
$KeyIds = (Get-MgInformationProtectionBitlockerRecoveryKey -Filter "deviceId eq '$DeviceId'").Id
if ($keyIds) {
#Write-Host -ForegroundColor Yellow "Device name: $devicename"
foreach ($keyId in $keyIds) {
$recoveryKey = (Get-MgInformationProtectionBitlockerRecoveryKey -BitlockerRecoveryKeyId $keyId -Select "key").key
# Write-Host -ForegroundColor White " Key id: $keyid"
# Write-Host -ForegroundColor Cyan " BitLocker recovery key: $recoveryKey"
$obj = [PSCustomObject]@{
DeviceName = $DeviceName
KeyId = $keyId
RecoveryKey = $recoveryKey
}
return $obj
}
}
else {
return "No BitLocker recovery keys found for device $DeviceName"
}
}
else {
return "Device $DeviceName not found"
}
}
}
# Install-Module Microsoft.Graph.Identity.SignIns -Scope CurrentUser -Force
# Import-Module Microsoft.Graph.Identity.SignIns
Connect-MgGraph -Scopes @('Device.Read.All', 'BitlockerKey.Read.All') -NoWelcome
$deviceNameList = @(
"deviceA",
"deviceB",
"deviceC"
)
$deviceNameList | Get-EntraBitLockerKeys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment