Skip to content

Instantly share code, notes, and snippets.

@rndazurescript
Last active February 22, 2021 14:39
Show Gist options
  • Save rndazurescript/bcdf195f9a8e7b0bf80d9e8882713b9f to your computer and use it in GitHub Desktop.
Save rndazurescript/bcdf195f9a8e7b0bf80d9e8882713b9f to your computer and use it in GitHub Desktop.
Access storage account using managed identity on a VM
$stgaccount="ctenteststg"
$uri="http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://"+$stgaccount+".blob.core.windows.net"
$token=Invoke-RestMethod -Method GET -Uri $uri -Headers @{"Metadata"="True"}
$listContentUrl="https://"+$stgaccount+".blob.core.windows.net/testdata"
$Parameters = @{
restype = 'container'
comp = 'list'
}
$result=Invoke-RestMethod -Method GET -Uri $listContentUrl -Headers @{"Authorization"="Bearer " + $token.access_token; "x-ms-version"="2017-11-09"} -Body $Parameters
$stgaccount="ctenteststg"
$uri="http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://"+$stgaccount+".dfs.core.windows.net"
$token=Invoke-RestMethod -Method GET -Uri $uri -Headers @{"Metadata"="True"}
# REST API DOCS
# https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/list
$listContentUrl="https://"+$stgaccount+".dfs.core.windows.net/testdata"
$Parameters = @{
recursive = 'true';
resource = 'filesystem'
}
$headers = @{
"Authorization"="Bearer " + $token.access_token;
}
$result=Invoke-RestMethod -Method GET -Uri $listContentUrl -Headers $headers -Body $Parameters
$result | ConvertTo-Json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment