Search code examples
powershellazureazure-devopsazure-pipelines-build-taskazure-automation

Azure PowerShell Start-AzureAutomationRunbook No default subscription has been designated


Azure PowerShell v2.* on VSTS is able to execute

Get-AzureRmResourceGroup

successfully and returns the right results... Good! This hopefully proves that the authentication between VSTS and the Azure account is working as expected.

When I try the command below:

Start-AzureAutomationRunbook -AutomationAccountName "automationaccountname" -Name "RunbookName"

I am getting this:

No default subscription has been designated. Use Select-AzureSubscription -Default <subscriptionName> to set the default subscription.

However, VSTS is already, automatically, executing these commands before:

Add-AzureRMAccount -ServicePrincipal -Tenant ******** -Credential System.Management.Automation.PSCredential -EnvironmentName AzureCloud
Select-AzureRMSubscription -SubscriptionId 12345678-1234-1234-1234-1234567890AB -TenantId ********

I am not sure what I need to execute as the subscription is already selected by the previous command. Any clue?


Solution

  • So here's the thing. There are two Azure modules for PowerShell at this point. There is Azure, and AzureRM. The Azure module addresses 'Classic' model resources, while the AzureRM module works with ARM (Azure Resource Management) resources. Each of them more or less operates independently of each other, with a small overlap here and there. The key here is that anything that starts with *-AzureRM* is related to the AzureRM module. Those cmdlets use the login info from the Add-AzureRMAccount cmdlet and Set-AzureRMContext (for which Select-AzureRMSubscription is an alias). If the command you are running does not start with '*-AzureRM' then more likely than not you will need to login to the classic module via the Add-AzureAccount cmdlet to be able to execute the command, or if you have already done that you can simply specify the subscription you want to work with using the Select-AzureSubscription cmdlet.

    Most likely fix: Double check that this is a Classic runbook. If it doesn't say Classic, try the AzureRM command instead:

    Start-AzureRmAutomationRunbook -AutomationAccountName "automationaccountname" -Name "RunbookName"
    

    If that doesn't work, then you will need to make sure that you are logged in to the classic module, and can see your subscription:

    Get-AzureSubscription -SubscriptionId 12345678-1234-1234-1234-1234567890AB
    

    If you can see the subscription you can run the same thing and simply pipe it to Select-AzureSubscription.

    Get-AzureSubscription -SubscriptionId 12345678-1234-1234-1234-1234567890AB | Select-AzureSubscription
    

    If you do not see your subscription listed you can login to Azure with the account that is associated with that subscription using the Add-AzureAccount cmdlet, and then repeat the Get/Select-Subscription cmdlets as needed.