Ashish-BeJovial
Ashish-BeJovial

Reputation: 1867

unable to get subscription details when logging in with service principal id in azure using PowerShell

I am trying to login in with service principal id using PowerShell. By doing this I want to connect with my Azure Data Factory and stop triggers. But at the initial phase of execution of code it gives an error. I paste a piece of code and results.

$resourceGroupName = 'my-resource-group'
$dataFactoryName = 'my-azure-data-factory-name'
$applicationid = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'
$secretKey = 'my-secret-key'
$tenantID = 'my-tenant-id'

$password = ConvertTo-SecureString -String $secretKey -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($applicationid,$password)
$x= Login-AzureRmAccount -ServicePrincipal -Credential $cred -Tenant $tenantID

#Gather a list of triggers to stop them
$allTriggers = Get-AzureRmDataFactoryV2Trigger -ResourceGroupName $resourceGroupName -DataFactoryName $dataFactoryName

Result:

enter image description here enter image description here

Upvotes: 0

Views: 1048

Answers (1)

Joy Wang
Joy Wang

Reputation: 42143

Well, I can reproduce your issue, this was caused by your service principal who did not have a role in your data factory/subscription.

enter image description here

To fix the issue, navigate to the ADF or subscription in the portal -> Access control (IAM) -> add your service principal as a role, e.g. Data Factory Contributor/ Contributor. To add the role, your user account which logged in the portal needs to be the Owner role of your ADF/subscription.

enter image description here

After adding the role, run the command to login again, then it will work fine. (I test with the new Az module, for your AzureRm module, it is the same logic)

enter image description here

Upvotes: 1

Related Questions