PowerShell で Azure の REST API を実行しようと思った際に、良く忘れてしまうのでメモとして。
- Azure REST API Reference
- A straightforward post to invoke Azure REST API via simple HTTP calls
- Calling Azure REST API via curl
- アクセス トークンを要求する
- Microsoft ID プラットフォームと OAuth 2.0 クライアント資格情報フロー
- チュートリアル:Windows VM のシステム割り当てマネージド ID を使用して Azure SQL にアクセスする
Contents
1. Get-AzContext 内のトークンを使用するパターン
事前に [Connect-Account] や、Cloud Shell 等で Azure Context を保持しているパターン
2022 年時点では、「Get-AzAccessToken」でアクセストークンが取得できるようになり、Get-AzContext では取得できなくなっています。
$tenantId = "xxxxxxx"
$subscriptionId = "xxxxxxx"
# $context = Get-AzContext
# $accessToken = $context.TokenCache.ReadItems() | ? TenantId -eq $tenantId
# $token = "Bearer {0}" -f $accessToken.AccessToken
$accessToken = Get-AzAccessToken | ? TenantId -eq $tenantId
$token = "Bearer {0}" -f $accessToken.Token
$uri = "https://management.azure.com/subscriptions/{0}/providers/Microsoft.Advisor/configurations?api-version=2017-04-19" -f $subscriptionId
$header = @{
"Authorization" = $token
"Content-Type" = "application/json"
}
$response = Invoke-WebRequest -Method "GET" -Uri $uri -Headers $header
Write-host ("{0} : {1}" -f $response.StatusCode, $response.content)
2. Client Id / Client Secret を使用するパターン
Azure AD にアプリケーションを登録して、その情報を使用するパターン
$tenantId = "xxxxxxx"
$subscriptionId = "xxxxxxx"
$clientId = "xxxxxxx"
$clientSecret = "xxxxxxx"
$tokenRequestUri = "https://login.microsoftonline.com/{0}/oauth2/token" -f $tenantId
$header = @{
"Content-Type" = "application/x-www-form-urlencoded"
}
$body = @{
"grant_type" = "client_credentials"
"client_id" = $clientId
"client_secret" = $clientSecret
"resource" = "https://management.azure.com"
}
$accessToken = (Invoke-WebRequest -Method "POST" -Uri $tokenRequestUri -Headers $header -Body $body).Content | ConvertFrom-Json
$token = "Bearer {0}" -f $accessToken.access_token
$uri = "https://management.azure.com/subscriptions/{0}/providers/Microsoft.Advisor/configurations?api-version=2017-04-19" -f $subscriptionId
$header = @{
"Authorization" = $token
"Content-Type" = "application/json"
}
$response = Invoke-WebRequest -Method "GET" -Uri $uri -Headers $header
Write-host ("{0} : {1}" -f $response.StatusCode, $response.content)
3. 仮想マシンからマネージド ID を使用するパターン
仮想マシンでマネージド ID を有効にして、該当のマネージド ID にロールを割り当てて実行するパターン
$subscriptionId = "xxxxxxx"
Add-Type -AssemblyName System.Web
$response = Invoke-WebRequest -Uri "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=$([System.Web.HttpUtility]::UrlEncode('https://management.azure.com/'))" -Method GET -Headers @{Metadata="true"}
$accessToken = ($response.Content | ConvertFrom-Json).access_token
$token = "Bearer {0}" -f $accessToken
$uri = "https://management.azure.com/subscriptions/{0}/providers/Microsoft.Advisor/configurations?api-version=2017-04-19" -f $subscriptionId
$method = "GET"
$header = @{
"Authorization" = $token
"Content-Type" = "application/json"
}
$response = Invoke-WebRequest -Uri $uri -Headers $header
Write-host ("{0} : {1}" -f $response.StatusCode, $response.content)