Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Manesh-R
Last active September 24, 2015 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Manesh-R/9ba344e08ebf7e6743a9 to your computer and use it in GitHub Desktop.
Save Manesh-R/9ba344e08ebf7e6743a9 to your computer and use it in GitHub Desktop.
PowerShell script to initialize Azure subscription and storage account. http://manesh.me/2015/02/05/getting-started-with-microsoft-azure-powershell/
# Ref: http://manesh.me/2015/02/05/getting-started-with-microsoft-azure-powershell/
# Step 0: Define all variable values
$publishSettingsFile = "C:\Users\Manesh\Documents\AzureSubscriptions.publishsettings";
$location = "West US";
$subscriptionName = "Visual Studio Premium with MSDN";
# Sometimes, you might have multiple MSDN subscriptions
# If someone else add you as a co-admin on their MSDN subscription etc.
# If so, choose the subscription Id option
$subscriptionId = $null;
# Sometimes, this account is created by someone else.
# So if the script fails, you might want to change this.
$storageAccountName = "manesh";
# Step 1: Import Azure Publish Settings File
Import-AzurePublishSettingsFile $publishSettingsFile
# Step 2: Set current subscription
Get-AzureSubscription
if ($subscriptionId -eq $null) {
Select-AzureSubscription -SubscriptionName $subscriptionName -Current
} else {
Select-AzureSubscription -SubscriptionId $subscriptionId -Current
}
# Display only current subscription
Get-AzureSubscription -Current
# Step 3: Set current storage account
$isStorageGood = $false;
try {
Get-AzureStorageAccount –StorageAccountName $storageAccountName -ErrorAction Stop | Out-Null
if ((Get-AzureStorageAccount –StorageAccountName $storageAccountName).Location -eq $location) {
$isStorageGood = $true;
Write-Host "'$storageAccountName' storage account already exists, skipping creation"
} else {
Write-Host "'$storageAccountName' storage account already exists, but in a different location. Try another storage."
}
}
catch {
if (!(Test-AzureName -Storage $storageAccountName)) {
Write-Host "Creating Storage Account $storageAccountName"
New-AzureStorageAccount -StorageAccountName $storageAccountName -Location $location
$isStorageGood = $true;
}
else {
Write-Host "'$storageAccountName' storage account already exists and is owned by some other subscription. Try a different name."
}
}
if ($isStorageGood) {
if ($subscriptionId -eq $null) {
Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccountName $storageAccountName
} else {
Set-AzureSubscription -SubscriptionId $subscriptionId -CurrentStorageAccountName $storageAccountName
}
# You should see the provided subscription and storage account name while executing following command
Get-AzureSubscription -Current
Write-Host "All is well"
} else {
Write-Host "Try a different storage account name and re-run the script"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment