Azure PowerShell Az 模块:为 Databricks 生成不记名令牌

Azure PowerShell Az module: generate bearer token for Databricks

我需要为 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 RESTAPI,请参考下面的脚本

$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 的更多详细信息,请参阅 document