Azure Powershell - Check to see if resource exists
Solution 1
You should use Test-AzureName for this instead of Get-AzureStorageAccount.
if (!Test-AzureName -Storage $Name)
{
# create the storage account.
}
This will work for Cloud Services, Web Apps, and Service Bus namespaces too. For your database, you will have to resort back to your existing approach.
**
Added the following to address questions about v2 (ARM) resources:
**
For v2 resources (ARM), the story is mostly the same. For example, the DNS name for a v1 or v2 storage account will be the same, such as contoso.blob.core.windows.net
. The same holds for Azure Web Apps (formerly Azure Web Sites), where you would have a DNS name such as contoso.azurewebsites.net
. So, in other words, Test-AzureName
would work just as well for these resources in ARM.
One notable difference is the DNS name for virtual machines. In v1, virtual machines are contained in a cloud service and get a DNS name such as contoso.cloudapp.net
. For v2 virtual machines, the public DNS name is provided by the Public IP Address resource, for which the DNS name for a virtual machine in East US (for example) would be contoso.eastus.cloudapp.azure.com
. To test for the availability of this DNS name, you should use the Test-AzureRmDnsAvailability cmdlet. For example,
if (Test-AzureRmDnsAvailability -DomainNameLabel "contos0" -Location "East US")
{
# Assign DNS name to Public IP Address resource here.
}
Solution 2
It is my solution with new Azure PowerShell Az module
$StorageAccountName = "Storage account name"
$ResourceGroupName = "Resource group name"
$StorageAccount = Get-AzStorageAccount -Name $StorageAccountName -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue
if($StorageAccount -eq $null){
$storage = New-AzStorageAccount -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -Location "westeurope" -SkuName Standard_LRS -Kind StorageV2
}
else{
Write-Host "$StorageAccountName already exist"
}
Solution 3
Try this:
if(!(Get-AzureRmStorageAccountNameAvailability -Name $storageName))
{
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageName -SkuName Standard_LRS
}
Solution 4
I usually go for the following (works for pretty much any resource in Azure, just replace the "Get" module and parameters):
function Test-AzureStorageAccountExists {
Param(
[string]$resourceGroupName,
[string]$storageAccountName
)
$SA = Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent) {return $false}
}
Related videos on Youtube
user888734
Updated on June 22, 2022Comments
-
user888734 almost 2 years
I'm using Powershell to automate setting up my Azure environment - to create storage account, database, website, etc.
In development, I want to provision and a tear down a lot. Very often, I want to run my provisioning script and create a azure asset if it doesn't already exist
However, I haven't found an elegant way of doing this. Some of the "Get" cmdlets throw exceptions if the item doesn't exist, and catching it is a bit of a hack:
try { $storageAcct = Get-AzureStorageAccount -StorageAccountName $Name Write-Verbose "Storage Account already exists" } catch { $storageAcct = New-AzureStorageAccount -StorageAccountName $Name -Location $Location }
What's more, with some commands, I can't catch the exception at all and I don't know why:
try { $cache = Get-AzureRedisCache -ResourceGroupName $resourceGroupName -Name $cacheName } catch { //Even with an exception, never arrives here. }
Is there a better way to do this?
-
Loïc MICHEL about 9 yearscatch will catch only terminating errors, did you set
$errorActionPreference
to"stop"
prior to the try/catch ?
-
-
pulko almost 8 yearsIs there an Resource Manager equivalent ?
-
lindstromhenrik over 7 yearsThe if-check is 'reversed' as Get-AzureRmStorageAccountNameAvailability returns 'true' if the storage name is available. Apart from that the check is perfect if you are in ARM-mode.
-
Amruta almost 7 yearsIs there any RM commands available to check resources deployed globally? Test-AzureRmDnsAvailability accepts Location as parameter. But for TrafficManager resources which are deployed globally, this command never works out...
-
Joy George Kunjikkuru about 6 yearsTest-AzureName doesn't work as expected. Its asking to select-AzureSubscription and select-AzureSubscription says my working subscription name and id do not exist. Finally ended up using Test-AzureRmDnsAvailability for HDInsight creation. Any idea how to check globally for checking name exists?
-
Martin Schvartzman over 4 yearsFor most of the providers, there's an API called checkNameAvailability that you could use to see of the name is already taken or is globally unique. I've wrapped up some of these in a PowerShell function. See secureinfra.blog/2019/11/07/…
-
mslot over 4 years@Dan I have run the above (removing the if) and it doesn't fail if the resource isn't present. It just returns nothing.
-
csadam over 2 yearswhat if there was any error other than ResourceNotFound?