PowerShell for Azure cmdlets: Subscription was all Wacky

I was working on some HDInsight scripts in PowerShell and doing lots of experimenting. I’m not sure what exactly I did but all of a sudden everything stopped working. With lots of interruptions from meetings and chats and lunch…. I couldn’t retrace my steps. Everything seemed to fail on the Azure subscription information so I tried to get really basic – what did Get-AzureSubscription|%{$_.SubscriptionName} return? As it turns out, wacky garbage:

set-azuresubscription ?
!?
!
help
sqlcatwoman
?
Sqlcatwomanwestus

What I expected to see was my single subscription:

sqlcatwoman

So what happened? The Azure portal only shows one subscription. Obviously those other lines are not valid subscriptions – they look like the output of a help command or an error. Reinstalling the cmdlets, rebooting, and reimporting certificates didn’t help. I turned to my AzureCAT coworkers for help and @elcid98 pointed out this blog post that talks about how subscriptions are used in PowerShell:

Azure Subscriptions in PowerShell demystified https://yossidahan.wordpress.com/2012/12/12/azure-subscriptions-in-powershell-demystified/

This caught my attention: “The second file – DefaultSubscriptionData.xml – also lists the available subscriptions and the associated certificates’ thumbprints“. Ok, where is that file? A search finds it in

C:\Users\%username%\AppData\Roaming\Windows Azure Powershell

I checked and sure enough, where I would expect just one entry I see multiple – and they’re named the same thing as the garbage in my output! I cleared out all but one entry to end up with this:

<?xml version="1.0" encoding="utf-8"?>
< Subscriptions xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" version="0" xmlns="urn:Microsoft.WindowsAzure.Management:WaPSCmdlets">
< Subscription name="sqlcatwoman">
  < SubscriptionId>You don’t get to see the real info!</SubscriptionId>
  <Thumbprint>Not here either!</Thumbprint>
  < ServiceEndpoint>https://management.core.windows.net</ServiceEndpoint>
< /Subscription>
< /Subscriptions>

Hmmmm…. I still got an error from Get-AzureSubscription. Back to C:\Users\%username%\AppData\Roaming\Windows Azure PowerShell. What’s this? WindowsAzureProfile.xml also has all the same junk! I cleared out all the extras to end up with this:

<?xml version="1.0" encoding="utf-8"?>
< ProfileData xmlns:i="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://schemas.datacontract.org/2004/07/Microsoft.WindowsAzure.Commands.Utilities.Common">
  < DefaultEnvironmentName>AzureCloud</DefaultEnvironmentName>
  <Environments />
  <Subscriptions>
    < AzureSubscriptionData>
      < ActiveDirectoryEndpoint>https://login.windows.net/</ActiveDirectoryEndpoint>
      < ActiveDirectoryTenantId>More secrets!</ActiveDirectoryTenantId>
      < ActiveDirectoryUserId>cgross@microsoft.com</ActiveDirectoryUserId>
      < CloudStorageAccount i:nil="true" />
      < IsDefault>true</IsDefault>
      < LoginType i:nil="true" />
      < ManagementCertificate>Hiding this one too!</ManagementCertificate>
      < ManagementEndpoint>https://management.core.windows.net/</ManagementEndpoint>
      <Name>sqlcatwoman</Name>
      <RegisteredResourceProviders xmlns:d4p1="https://schemas.microsoft.com/2003/10/Serialization/Arrays" />
      < SubscriptionId>And more secrets</SubscriptionId>
    </AzureSubscriptionData>
  </Subscriptions>
< /ProfileData>

Success! Get-AzureSubscription now returns just my single, valid subscription. All my other Azure cmdlets magically started working again. I don’t know how it got that way, but at least now I know where the subscription information is stored. I hope this helps someone else with their Azure subscription PowerShell scripting!