Azure: How to check storage account exists in Azure with Get-AzureStorageAccount
Solution 1
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.
Solution 2
Get-AzureRmStorageAccountNameAvailability -Name "accountname"
Solution 3
Try this:
$Name = "myStorageAccount"
$Location = "myLocation"
Write-Host "[Start] creating $Name storage account $Location location"
try{
Get-AzureStorageAccount –StorageAccountName $Name -ErrorAction Stop | Out-Null
Write-Host "$Name storage account in $Location location already exists, skipping creation"
}
catch{
Write-Host "[Finish] creating $Name storage account in $Location location"
New-AzureStorageAccount -StorageAccountName $Name -Location $Location -Verbose
}
Solution 4
You should use the latest Powershell module Az.
if ($(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName) -eq $null)
{
# does not exist
}
Solution 5
With the current Az module for PowerShell Version 7, the Get-AzStorageAccountNameAvailability cmdlet might offer a more efficient solution as it was designed specifically for this task. Here is an example:
# ... declare variables and specify values ...
$checkNameAvail = (Get-AzStorageAccountNameAvailability -Name $storageAccountName) | `
Select-Object NameAvailable
if ($checkNameAvail.NameAvailable)
{
Write-Host 'Account name available! Please wait while your resource is being created'
# Create account. Variables used in this example would have been declared earlier in the script.
$storageAccount = (New-AzStorageAccount -ResourceGroupName $resourceGroupName `
-AccountName $storageAccountName `
-Location $location `
-SkuName $skuType `
-AllowBlobPublicAccess $false -EnableHttpsTrafficOnly $true)
# ...
}
else
{
# This section of the script executes if the name is not available
Write-Host "The name <$storageAccountName> is not available. Suggest a new globally unique name!"
}
The condition above will return False
, and execute the else
statement because the boolean value returned by the cmdlet is in [0]
as shown in the PowerShell command-line test below. The availability information (boolean) can thus be stripped from the object returned by the cmdlet and (as in this example) used as a condition in the rest of the script.
PS C:\> Get-AzStorageAccountNameAvailability -Name testaccount1
NameAvailable Reason Message
------------- ------ -------
False AlreadyExists The storage account named testaccount1 is already taken.
Julen
Programmer with long experienced in .NET Framework. Focused on web development with ASP.NET and C# under MVC pattern. Fluent in JavaScript, HTML and CSS. Other experience includes desktop development with C# and C++ under UNIX environment. Agile values enthusiast and a Scrum practitioner.
Updated on June 22, 2022Comments
-
Julen almost 2 years
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!
-
aljoshare about 4 yearsThanks for your answer. Please consider to put your code in a code environment next time :-)