Skip to content

Instantly share code, notes, and snippets.

@JayDoubleu
Created July 24, 2023 18:27
Show Gist options
  • Save JayDoubleu/e702fba0ae8560d036d5ab174191132e to your computer and use it in GitHub Desktop.
Save JayDoubleu/e702fba0ae8560d036d5ab174191132e to your computer and use it in GitHub Desktop.
List storage keys by authenticating via managed identity
$url = 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/'
$accessToken = Invoke-RestMethod -Uri $url -Headers @{Metadata = 'true'} -Method Get -UseBasicParsing | Select-Object -ExpandProperty access_token
Write-Host "Access token: $accessToken"
$storageAccountResourceId = '/subscriptions/1c0c1a70-a036-40b3-a022-77db654391c4/resourceGroups/rg-kns-testing-prd-a-uks-01/providers/Microsoft.Storage/storageAccounts/stgknstestingprdauks01'
$url = 'https://management.azure.com/'+ $storageAccountResourceId + '/listKeys?api-version=2022-09-01&$expand=kerb'
$headers = @{
'Accept' = 'application/json'
'Authorization' = 'Bearer ' + $accessToken
}
$body = ''
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method POST -Body $body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment