SE の雑記

SQL Server の情報をメインに Microsoft 製品の勉強内容を日々投稿

PowerShell で Azure REST API にアクセスする際の覚書

leave a comment

PowerShell で Azure の REST API を実行しようと思った際に、良く忘れてしまうのでメモとして。

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)
Share

Written by Masayuki.Ozawa

3月 12th, 2020 at 11:56 pm

Posted in Azure,Microsoft Azure

Tagged with ,

Leave a Reply