1

I have a release pipeline which is calling a PowerShell script and it is having 'Select-AzureSubscription' statement (In a loop I have to assign different subscriptions as current and do some process). When it runs, I am getting the below error as I have not added 'Add-AzureAccount'.

Select-AzureSubscription : The subscription name doesn't exist.

Is there any way to authenticate or execute Add-AzureAccount using the Pipeline access token.

Code:-

Select-AzureSubscription -SubscriptionName $Subscription -Current 
CloudServices = Get-AzureService | select ServiceName 
5
  • If you care to lookup Add-AzureAccount, you will find that none of the parameters accept pipeline input, so the answer is No. Commented Jun 21, 2020 at 9:14
  • Is there any workaround? to authenticate Select-AzureSubscription Commented Jun 21, 2020 at 13:48
  • What azure resources will you access? Could you provide your script snippet? Maybe I can give a better solution. Commented Jun 22, 2020 at 1:16
  • Hi Joy, Edited the question and added the code. Here I want to get all the list of cloud services. For executing the command 'Get-AzureService', we need to set a default subscription. But here, Select-AzureSubscription requires an authentication. How to proceed this authentication though a release pipeline. Commented Jun 22, 2020 at 3:38
  • @Tinz Do you use Azure PowerShell task? You need an Azure Resource Manager service connection before using Azure resource: learn.microsoft.com/en-us/azure/devops/pipelines/library/…. Commented Jun 22, 2020 at 7:00

1 Answer 1

1

You could try the steps below(I could not test it for you as I don't have a user account without MFA-enabled).

1.In devops, navigate to the Project Settings -> Service connections -> New service connection -> Azure Classic.

enter image description here

Then input the information, you could get the subscription name and id in the azure portal -> Subscriptions.

Note: The Username and Password need to be the user account without MFA-enabled.

enter image description here

enter image description here

2.After creating the service connection, in the pipline, create a Azure powershell task with Task version 3.*, select the Azure Connection Type with Azure Classic, select Azure Classic Subscription with the service connection which was created in step 1.

Then use the script:

Select-AzureSubscription -SubscriptionName <subscription-name> -Current 
CloudServices = Get-AzureService | select ServiceName 

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Hey Joy, In this case, do we need a separate Service Connection for each Subscription?
Hey Joy, In PowerShell I am having a subscription loop and getting Azure services of each subscription. If I can use only one subscription in a pipeline, then I cannot go for this solution. :(
@Tinz Not sure, you can have a try. If you just use one service connection, see the Select-AzureSubscription if works or not. If it does not work, you may need to use different connections. The problem is I don't have the account without MFA, so I cannot test for you.-(
@Tinz Please have a try, maybe it can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.