Communication could not be established. This could be due to an invalid subscription ID

Today I’ve spent quite some time figuring out why my new Azure Subscription Settings file was not picked up by Octopus Deploy. And I was getting an obscure error message:

Get-AzureWebsite : Communication could not be established. This could be due to an invalid subscription ID. Note that subscription IDs are case sensitive.

Turned out that old Subscription Settings File was stuck in user cache and I had to “unstuck” it by executing this script from under the user account I was trying to execute PowerShell.

Remove-AzureSubscription 'Subscription Name' -Force

This does not actually do anything to the actual Azure subscription (I panicked about it first). Documentation says this only deletes subscription data file from so PowerShell can’t use it. Nothing to do with the actual Azure Subscription.

After removing old subscription data you can re-import new Subscription Settings File:

Import-AzurePublishSettingsFile 'C:/path/to/subscription.publishsettings'

Select-AzureSubscription -SubscriptionName "Subscription Name"

Hope this helps someone!

Capture Azure exception message

I’m fighting with Azure and publishing via PowerShell at the moment.
It seems that powershell is problematic and does not want to play. Especially with slow deployment times – you need to wait for it to throw errors, and that usually takes 3-15 minutes depending on your internet speed.

So you would want to get error message captured:

# try publish - here I get 500 server error
New-AzureDeployment -Slot Staging -Package $package -Configuration $configuration -ServiceName $service

# capture error stream
$sr = new-object System.IO.StreamReader($Error[0].Exception.InnerException.Response.GetResponseStream())  
$txt = $sr.ReadToEnd() 

# print out the message
$txt

Hopefully this will help a bit.
Info taken from this guy