我需要生成用于 Databricks 使用的令牌(它将用于生成 Databricks 令牌)
在 Azure CLI 中
az account get-access-token --resource '2ff814a6-3304-4ab8-85cb-cd0e6f879c1d' --out tsv --query '[accessToken]'
工作得非常好
我知道 Azure PowerShell Az 模块中没有其他选择,因此我进行了研究并发现了以下内容:
$context = Get-AzContext
$profile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($profile)
$token = $profileClient.AcquireAccessToken($context.Subscription.TenantId)
$token.AccessToken
它确实有效,但生成的令牌具有
https://management.core.windows.net/
声明,而不是 Databricks 所需的 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d
有什么想法如何使用 Az 模块在 Azure PowerShell 中运行
az account get-access-token --resource '2ff814a6-3304-4ab8-85cb-cd0e6f879c1d'
的替代方案吗?
我的服务主体具有受密码保护的证书身份验证,并且无法使用 az cli / python /etc,只能使用 Azure PowerShell Az 模块
如果您想使用Azure Powershell调用Azure Databricks REST API,请参考以下脚本
$teantId
$subId="the id of the subscription which contains the databrick"
Connect-AzAccount -Subscription $subId -Tenant $teantId
$context= Get-AzContext
$resource="2ff814a6-3304-4ab8-85cb-cd0e6f879c1d"
$token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account,
$context.Environment,
$context.Tenant.Id.ToString(),
$null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $resource).AccessToken
$groupName="the databrick resource group name"
$workSpaceName="the databrick workspace name"
$headers=@{
"Authorization"= "Bearer " + $token;
"X-Databricks-Azure-Workspace-Resource-Id" = "/subscriptions/$($subId)/resourceGroups/$($groupName)/providers/Microsoft.Databricks/workspaces/$($workSpaceName)"
}
$databricksInstance="" # such as adb-976301816870846.6.azuredatabricks.net
$url="https://$($databricksInstance)/api/2.0/clusters/list"
$result=Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ContentType "application/json" -UseBasicParsing
$result| ConvertTo-Json
有关如何调用Azure Databricks REST API的更多详细信息,请参阅文档
$resourceId="2ff814a6-3304-4ab8-85cb-cd0e6f879c1d"
$tokenInfoFromAzure = Get-AzAccessToken -ResourceUrl $resourceId