Skip to content

Instantly share code, notes, and snippets.

@markekraus
Last active June 6, 2018 12:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/d57b12bfa28e51decc9f83c9eebf8f23 to your computer and use it in GitHub Desktop.
Save markekraus/d57b12bfa28e51decc9f83c9eebf8f23 to your computer and use it in GitHub Desktop.
Get BitLocker Recovery Key from Azure AD
#Requires -Modules AzureRM.Profile, AzureAD
$DeviceObjectId = '' # Set an objectID for a device
$SubScriptionID = '' # change with your subscription ID
Add-AzureRmAccount
$Subscription = Select-AzureRmSubscription -Subscription $SubScriptionID
$currentAzureContext = Get-AzureRmContext -Name $Subscription.Name
$TenantId = $currentAzureContext.Tenant.Id
$tokenCache = $currentAzureContext.TokenCache
$Cache = $tokenCache.ReadItems().Where({$_.ExpiresOn -gt (Get-Date) -and $_.TenantId -eq $TenantId })[0]
$refreshToken = $Cache.RefreshToken
$body = "grant_type=refresh_token&resource=74658136-14ec-4630-ad9b-26e160ff0fc6&refresh_token=$([System.Web.HttpUtility]::UrlEncode($refreshToken))"
$tokresponse = Invoke-RestMethod "https://login.windows.net/$tenantid/oauth2/token" -Method POST -Body $body -ContentType 'application/x-www-form-urlencoded'
$AccessToken = $tokresponse.access_token
$BaseUri = 'https://main.iam.ad.ext.azure.com/api'
$ApiPath = 'Device'
$GetReqId = (New-Guid).ToString()
$Params = @{
Headers = @{
'Authorization' = 'Bearer {0}' -f $AccessToken
'x-ms-client-request-id' = $GetReqId
}
Uri = '{0}/{1}/{2}' -f @(
$BaseUri
$ApiPath
$DeviceObjectId
)
Method = 'GET'
ContentType = 'application/json'
}
$ApiResponse = Invoke-RestMethod @Params
$ApiResponse.bitLockerKey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment