Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure: How to check storage account exists in Azure with Get-AzureStorageAccount

I am building a power shell script to automate the setup of a website environment in Azure. This web uses an account storage. I want to the script not to create the account storage if exists.

I thought that using Get-AzureStorageAccount this way may work but it does not:

Write-Verbose "[Start] creating $Name storage account $Location location"

$storageAcct = Get-AzureStorageAccount –StorageAccountName $Name
if (!$storageAcct)
{   
    $storageAcct = New-AzureStorageAccount -StorageAccountName $Name -Location $Location -Verbose
    if ($storageAcct)
    {
        Write-Verbose "[Finish] creating $Name storage account in $Location location"
    }
    else
    {
        throw "Failed to create a Windows Azure storage account. Failure in New-AzureStorage.ps1"
    }
}
else
{
    Write-Verbose "$Name storage account in $Location location already exists, skipping creation"
}

The issue is I don't know how to handle the return of Get-AzureStorageAccount.

Thank you very much in advance!

like image 665
Julen Avatar asked Jun 23 '14 11:06

Julen


2 Answers

I would suggest using the Test-AzureName cmdlet to determine if it exists. So, something like this.

if (!(Test-AzureName -Storage $Name))
{  
    Write-Host "Creating Storage Account $Name"
    New-AzureStorageAccount -StorageAccountName $Name -Location $Location 
}

You can use Test-AzureName for other services too, such as Cloud Services, WebSites, and ServiceBus. It returns True if it exists, False otherwise.

like image 169
Rick Rainey Avatar answered Oct 17 '22 05:10

Rick Rainey


Get-AzureRmStorageAccountNameAvailability -Name "accountname"
like image 24
user891818 Avatar answered Oct 17 '22 04:10

user891818