2017-02-25 1 views
0

Login AzureRMAccount를 실행 한 후 MSDN Azure 구독에 로그인하면 문제없이 Get-AzureRmRoleAssignment을 실행할 수 있습니다.Azure Powershell에서 서비스 주체로 Get-AzureRmRoleAssignment 실행

나는 파워 쉘과 하늘빛의 역할을하는 응용 프로그램 및 서비스 사용자를 생성 (뉴 AzureRmADApplication은 New-AzureRmADServicePrincipal & 새로운-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 
0

Microsoft.Rest.Azure.CloudException :

나는 다음과 같은 예외를 받고 있어요 액세스

이 예외가 많은 사람처럼 보인다 지정된 API 버전으로 거부하고 내가 읽은 azure 포털에 액세스 권한을 부여하는 것과 같은 많은 솔루션 (그러나 아무 것도 작동하지 않는 것 같습니다) : 또한 응용 프로그램에 다음 권한을 부여한 후에도 예외는 여전히 발생합니다.

  • 윈도우 Azure Active Directory에 6 응용 프로그램 권한 - 6 위임 된 권한 ( 은 관리자가 필요합니다 경우에만, 그들 모두를 선택할 수 없습니다 = 예)
  • 마이크로 소프트 그래프 - 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 "***************************************************************************" 
+0

어떤 Azure PowerShell 버전을 사용하고 있습니까? Get-AzureRmRoleAssignment -debug를 문제없이 실행할 수 있습니다. – juvchan

+0

버전 5.1.14393.693. AzureRM 버전은 3.6 –

+0

입니다. 여러 개발 PC에서 성공하지 못했습니다. Service Principal로 로그인 한 후 Get-AzureRmRoleAssignment -debug를 실행할 수 있습니까? (위의 powershell처럼?) –

답변

0

그렇게하려면, 당신은 그렇게 할 권한을 할당해야합니다) 사용 권한이 Microsoft.Authorization/roleAssignments/read라고 , 당신은 Get-AzureRmProviderOperation Microsoft.Authorization/*과 권한의 목록을 얻을 수 있습니다, 사용자 정의 역할 정의를 만들 필요가 권한을 할당하고, 할당하거나 내장 된 역할 중 하나를 사용하십시오.

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

나는이 파워 쉘에서만 발생하는 것을 확인할 수 있습니다 (재미있는 일이다 - Get-AzureRmRoleDefinition 작품, Get-AzureRmRoleAssignment하지 않는 동안) 같은 서비스 주체

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) 
를 사용하는 경우, 파이썬 코드는 역할 할당을 나열 할 수 있습니다
+0

서비스 사용자가 소유자입니다.서비스 주체 생성 후 New-AzureRmRoleAssignment를 실행할 때 다음 매개 변수 New-AzureRmRoleAssignment -RoleDefinitionName "Owner"-ServicePrincipalName $ appId를 전달했습니다. –

+0

글쎄, 보트를 흔들어서 미안하지만, 당신이 보여준 스크립트가 자원 그룹에 소유자 권한을 명확히 지정하고 있지 않습니다. 서브넷 – 4c74356b41

+0

죄송합니다. 실행하기 전에 resourcegroup 부분을 제거했습니다. 나는 그것을 명확하게하기 위해 나의 지위를 편집 할 것이다. –