Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Created April 8, 2016 02:37
Show Gist options
  • Save rayterrill/cd7ef7332cfbcd40e39482ca998c366f to your computer and use it in GitHub Desktop.
Save rayterrill/cd7ef7332cfbcd40e39482ca998c366f to your computer and use it in GitHub Desktop.
$resourceGroupName = "TESTSERVERS"
$location = "West US"
$storageType = "Standard_GRS"
function createStorageAccount($storageAccountPrefix) {
$storageAccountCreated = $False
#storage account names need to be globally unique
#loop until we find a storage account name that works
while ($storageAccountCreated -eq $False) {
$randomNumber = Get-Random
$storageName = $storageAccountPrefix + $randomNumber
try {
$storageAccount = New-AzureRMStorageAccount -ResourceGroupName $resourceGroupName -Name $storageName -Type $storageType -Location $location
$storageAccountCreated = $True
} catch {
#turn on debug mode to see failed attempts to create storage accounts
Write-Debug "Storage account $($storageName) was taken. Trying again..."
}
}
return $storageAccount
}
$storageAccount = createStorageAccount -storagePrefix "TESTSERVERSTORE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment