在Azure Powershell中执行Get-AzureRmRoleAssignment作为服务主体

时间:2017-02-25 00:10:55

标签: powershell azure azure-powershell

在我的MSDN Azure订阅中,在执行Login-AzureRMAccount后登录,我可以毫无问题地执行Get-AzureRmRoleAssignment

我创建了一个应用程序和服务Principal,在Azure中使用powershell(New-AzureRmADApplication,New-AzureRmADServicePrincipal& New-AzureRmRoleAssignment),并在使用此powershell登录这些凭据之后:

$psCredential = New-Object System.Management.Automation.PSCredential("98349834-8494-4813-9282-4343434", (ConvertTo-SecureString "myPassword" -AsPlainText -Force))
Add-AzureRMAccount -ServicePrincipal -Tenant "123456-d5bb-44f8-a283-34534434" -Credential $psCredential

以下内容可以成功执行:

Get-AzureRmADApplication -IdentifierUri "http://SP.5656645-408c-4980-950e-898989"

但执行时

Get-AzureRmRoleAssignment -debug

我遇到以下异常:

  

Microsoft.Rest.Azure.CloudException:拒绝访问指定的API版本

似乎很多人都有这个例外,我已经阅读了许多解决方案,比如在天蓝色的门户网站中提供访问权限(但似乎没有任何效果): 在为应用程序提供以下权限之后,仍会引发异常:

  • Windows Azure Active Directory 6应用程序权限 - 6 委派权限(不能全部选择,只能在哪里 需要Admin = Yes)
  • Microsoft Graph - 20个应用程序权限 - 20个委派权限(也是 无法全部选择)

如何让服务主体执行Get-AzureRmRoleAssignment?

编辑: 该应用程序具有角色所有者。我使用以下ps脚本来创建应用程序,服务主体和角色。

param
(
    [Parameter(Mandatory=$true, HelpMessage="Enter Azure Subscription name. You need to be Subscription Admin to execute the script")]
    [string] $subscriptionName,

    [Parameter(Mandatory=$true, HelpMessage="Provide a password for SPN application that you would create")]
    [string] $password,    
Mandatory=$false, HelpMessage="Provide a SPN role assignment")]
    [string] $spnRole = "owner"
)

#Initialize
$ErrorActionPreference = "Stop"
$VerbosePreference = "SilentlyContinue"
$userName = $env:USERNAME
$newguid = [guid]::NewGuid()
$displayName = [String]::Format("SP.{0}.{1}", $resourceGroupName, $newguid)
$homePage = "http://" + $displayName
$identifierUri = $homePage


#Initialize subscription
$isAzureModulePresent = Get-Module -Name AzureRM* -ListAvailable
if ([String]::IsNullOrEmpty($isAzureModulePresent) -eq $true)
{
    Write-Output "Script requires AzureRM modules to be present. Obtain AzureRM from https://github.com/Azure/azure-powershell/releases. Please refer https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/DeployAzureResourceGroup/README.md for recommended AzureRM versions." -Verbose
    return
}

Import-Module -Name AzureRM.Profile
Write-Output "Provide your credentials to access Azure subscription $subscriptionName" -Verbose
Login-AzureRmAccount -SubscriptionName $subscriptionName
$azureSubscription = Get-AzureRmSubscription -SubscriptionName $subscriptionName
$connectionName = $azureSubscription.SubscriptionName
$tenantId = $azureSubscription.TenantId
$id = $azureSubscription.SubscriptionId


#Create a new AD Application
Write-Output "Creating a new Application in AAD (App URI - $identifierUri)" -Verbose
$azureAdApplication = New-AzureRmADApplication -DisplayName $displayName -HomePage $homePage -IdentifierUris $identifierUri -Password $password -Verbose
$appId = $azureAdApplication.ApplicationId
Write-Output "Azure AAD Application creation completed successfully (Application Id: $appId)" -Verbose


#Create new SPN
Write-Output "Creating a new SPN" -Verbose
$spn = New-AzureRmADServicePrincipal -ApplicationId $appId
$spnName = $spn.ServicePrincipalNames
Write-Output "SPN creation completed successfully (SPN Name: $spnName)" -Verbose


#Assign role to SPN
Write-Output "Waiting for SPN creation to reflect in Directory before Role assignment"
Start-Sleep 20
Write-Output "Assigning role ($spnRole) to SPN App ($appId)" -Verbose
New-AzureRmRoleAssignment -RoleDefinitionName $spnRole -ServicePrincipalName $appId 
Write-Output "SPN role assignment completed successfully" -Verbose


#Print the values
Write-Output "`nCopy and Paste below values for Service Connection" -Verbose
Write-Output "***************************************************************************"
Write-Output "Connection Name: $connectionName(SPN)"
Write-Output "Subscription Id: $id"
Write-Output "Subscription Name: $connectionName"
Write-Output "Service Principal Id: $appId"
Write-Output "Service Principal key: <Password that you typed in>"
Write-Output "Tenant Id: $tenantId"
Write-Output "***************************************************************************"

1 个答案:

答案 0 :(得分:1)

要做到这一点,你需要分配权限来做到这一点;) 该权限称为Microsoft.Authorization/roleAssignments/read,您可以使用Get-AzureRmProviderOperation Microsoft.Authorization/*获取权限列表,以分配创建自定义角色定义和分配权限所需的权限,或使用其中一个内置角色。

https://docs.microsoft.com/en-us/azure/active-directory/role-based-access-control-custom-roles

我可以确认这只发生在powershell上(最有趣的是 - Get-AzureRmRoleDefinition有效,而Get-AzureRmRoleAssignment没有),python代码可以在使用相同的服务主体时列出角色分配

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.authorization import AuthorizationManagementClient
tenant_id = 'tenant_guid'
application_id = 'application_guid'
application_secret = 'application_secret'
cred = ServicePrincipalCredentials(client_id=application_id, secret=application_secret, tenant=tenant_id)
client = AuthorizationManagementClient(cred, 'subscription_guid')
roles = client.role_assignments.list()
for role in roles:
    print(role)