使用PowerShell ARM Cmdlet授予AKS对ACR的访问权限

问题描述 投票:-1回答:1

我正在使用PowerShell通过遵循此link从Azure Kubernetes服务中使用Azure容器注册表进行身份验证。

这是我在PowerShell中运行的代码。

#Sign in using Interactive Mode using your login credentials
az login

#Set the current azure subscription
az account set --subscription 'XXXXXXXXXXXXXXXXXXXXXXX'

#See your current azure subscription
#az account show

#Get the id of the service principal configured for AKS
$AKS_RESOURCE_GROUP = "XXXX-AKSRES-SB-DEV-RGP-01"
$AKS_CLUSTER_NAME = "XXXX-AKSRES-SB-DEV-AKS-01"
$CLIENT_ID=$(az aks show  --name $AKS_CLUSTER_NAME --resource-group       $AKS_RESOURCE_GROUP --query "servicePrincipalProfile.clientId" --output tsv)

# Get the ACR registry resource id
$ACR_NAME = "XXWEAKSRESSBDEVACR01"
$ACR_RESOURCE_GROUP = "XXWE-AKSRES-SB-DEV-RGP-01"
$ACR_ID=$(az acr show --name $ACR_NAME --resource-group $ACR_RESOURCE_GROUP --query "id" --output tsv)

#Create role assignment
az role assignment create --assignee $CLIENT_ID --role Reader --scope $ACR_ID

上面的代码包含Azure CLI命令,但我想使用PowerShell ARM cmdlet而不是Azure CLI命令。

azure powershell azure-resource-manager azure-container-service azure-container-registry
1个回答
2
投票

您可以尝试下面的命令,它在我身边工作正常。

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionId "xxxxxxxxxxxxxxxxx"
#Get the id of the service principal configured for AKS
$aks = Get-AzureRmResource -ResourceGroupName "<ResourceGroupName>" -ResourceType Microsoft.ContainerService/managedClusters -ResourceName "<aksname>" -ApiVersion 2018-03-31
$clientid = $aks.properties.servicePrincipalProfile.clientId
#Get the ACR registry resource id
$acr = Get-AzureRmContainerRegistry -ResourceGroupName "<ResourceGroupName>" -Name "<ACRregistryname>" 
$resourceid = $acr.id
#Create role assignment
New-AzureRmRoleAssignment -ApplicationId $clientid -RoleDefinitionName "Reader" -Scope $resourceid

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.