As I’ve worked with Microsoft Azure, PowerShell has been a tool that has made my life easier. When I just managed one subscription, PowerShell was easy because I was able to do a few quick Bing searches and find the type of PowerShell cmdlet’s I needed to accomplish my task. Once I added additional subscriptions to my account, my old ways didn’t work any longer so I had to figure out how to manage multiple Azure accounts via PowerShell. I want to share a few of my favorite PowerShell cmdlet’s so you can see how I can manage multiple Microsoft Azure subscriptions with PowerShell.
First be sure to install PowerShell for Microsoft Azure here. The complete SDK download page is here if you want to download the other tools too.
Next, you’ll need to run the Azure PowerShell.
Once you’ve started PowerShell, your screen should look something like this.
Now let me show you the cmdlets that allow me to connect to a specific Azure account and then upload virtual machines from on-premises to Azure.
The Get-AzurePublishSettingsFile cmdlet opens your default browser, signs into your Microsoft Azure account, and automatically downloads a .publishsettings file that contains information and a certificate for your Windows Azure subscription. Go ahead and save the .publishsettings file, you will need it for the next cmdlet.
The next cmdlet we’re going to use is the Import-AzurePublishSettingsFile cmdlet, it will leverage the .publishsettings file we created with the Get-AzurePublishSettingsFile.
1: Import-AzurePublishSettingsFile “MyAccountCredentials.publishsettings”
After you execute the Import-AzurePublishSettingsFile cmdlet, you’ve now connected your PowerShell session to your Microsoft Azure account(s).
The two steps above have always worked for me, but once I setup additional subscriptions, I could no longer just upload to a storage account. With multiple subscriptions, you need to select which subscription you are going to work with. The cmdlet Get-AzureSubscription will show all of the subscriptions I have access to, then the cmdlet Select-AzureSubscription allows me to select the appropriate Azure subscription.
3: Select-AzureSubscription -SubscriptionName "Account 1"
Now I’ve identified my Azure Account and Storage Account that I want to upload my Virtual Machine to, now I just need to do the upload. I use the cmdlet Add-AzureVhd to do the actual upload, but I use variables to set everything up first.
1: $dest = "https://<YourStorageAccount>.blob.core.windows.net/vhds/<VMName>.vhd"
2: $source = "d:\vhd\<Your Local VMName>.vhd"
4: add-azurevhd -localfilepath $source -destination $dest -NumberofUploaderThreads 3
The three lines above will start the upload of your local VHD to your Azure storage account.
Keep in mind that you can only upload VHD’s, not VHDx’s and the VHDs must be fixed size, not dynamically expanding. If you use the Add-AzureVhd cmdlet, it will actually convert your dynamically expanding VHD to fixed size so you don’t have to worry about that. If you are using VHDx’s, Hyper-V manager can convert your VHDx’s to a VHD’s.
In Summary, here are the cmdlet’s I talked about in the order I use them.
3: Import-AzurePublishSettingsFile "d:\vhd-upload\<My Account>.publishsettings"
4: # PowerShell is now "connected" to your Azure account(s)
7: # Get-AzureSubscription will show you a list of all your the subscriptions you are able to access
9: Select-AzureSubscription -SubscriptionName "Account 1"
10: # I've chosen "Account 1" to manage
13: # Get-AzureStorageAccount will list all of the storage accounts available
15: $dest = "https://<YourStorageAccount>.blob.core.windows.net/vhds/<VMName>.vhd"
16: $source = "d:\vhd\<Your Local VMName>.vhd"
18: Add-AzureVhd -localfilepath $source -destination $dest -NumberofUploaderThreads 3
19: # Add-AzureVhd uploads the VHD to the Azure storage account
Until next time,