Search code examples
powershellazure-devopsazure-pipelinesazure-powershellazure-pipelines-release-pipeline

Authenticate powershell script using AccessToken


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 

Solution

  • 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