Azure powershell script cannot set default subscription

I have a working project that deploys a web app to Azure. My subscription is already linked and working in Octopus so I know the azure account configuration is fine.

I now have a need to stop the web app before I deploy (b/c I have to overwrite files that are locked) and I’ve added a step to run an azure powershell script. The first thing I tried was the example script from http://help.octopusdeploy.com/discussions/questions/5641-whats-the-best-way-to-stop-and-start-and-azure-web-site-when-deploying

When the step runs, it gives a powershell error that I don’t have a default subscription selected, so my next step was to add the Select-AzureSubscription call before the Stop-AzureWebSite call but I get an error that the subscription does not exist (even though it logs that exact subscription in the log). I’ve tried by both subscription name and id, confirmed both from the azure portal and have had no luck getting it to work.

What am I missing?

Hi Wes,

Thanks for getting in touch.

It’s strange that your deployment works but the PowerShell doesn’t. Would you mind attaching the Octopus task log logs for your attempts?

Do you know if you are using a Service Principal or Management Certificate account? There is a toggle on the accounts page that shows which is being used.

Cheers,
Shane

Hi Shane, I think I’m using a Service Principal account. I do not see a toggle on the accounts page but my azure subscription is listed under the heading “Service Principals” so I think that is the right answer.

Attached is the log.

Thanks

Wes

ServerTasks-1889.log.txt (5 KB)

Hi Wes,

It does look like you are using a Service Principal. Could you try the following instead of Select-AzureSubscription:

Get-AzureRmSubscription –SubscriptionName "Microsoft Azure Sponsorship" | Select-AzureRmSubscription

Cheers,
Shane

That was the missing piece of information that I didn’t realize. Since I’m using a Service Principal I have to use the Azure Resource Manager Cmdlets

I’ve now got it working (thanks to you!) with the following:

Get-AzureRmSubscription –SubscriptionName "Microsoft Azure Sponsorship" | Select-AzureRmSubscription
Stop-AzureRmWebApp -ResourceGroupName "<my resource group>" –Name "<my web app name>"
... (other steps)
Start-AzureRmWebApp -ResourceGroupName "<my resource group>" –Name "<my web app name>"