Azure Powershell - check if resource exists
I am using Powershell to automate the setup of my Azure environment - to create a storage account, database, website, etc.
In development, I want to highlight and demolish a lot. Very often I want to run my provisioning script and create an azure object if it doesn't already exist
However, I have not found an elegant way to do 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?
source to share
To do this, use Test-AzureName instead of Get-AzureStorageAccount.
if (!Test-AzureName -Storage $Name)
{
# create the storage account.
}
This will work for cloud services, web applications, and Service Bus namespaces as well. For your database, you will have to revert to the existing approach.
**
Added the following to ask questions about v2 (ARM) resources:
**
For v2 (ARM) resources, the story is basically the same. For example, the DNS name for the v1 or v2 storage account will be the same as contoso.blob.core.windows.net
. The same goes for Azure Web Apps (formerly Azure Web Sites) where you will have a DNS name for example contoso.azurewebsites.net
. In other words, it Test-AzureName
will work just as well for these resources on ARM.
The notable difference is the DNS name for the virtual machines. In v1, virtual machines are hosted in a cloud service and given a DNS name, for example 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 the East America virtual machine (for example) will be contoso.eastus.cloudapp.azure.com
. To check for the existence of this DNS name, you must use the Test-AzureRmDnsAvailability cmdlet . For example,
if (Test-AzureRmDnsAvailability -DomainNameLabel "contos0" -Location "East US")
{
# Assign DNS name to Public IP Address resource here.
}
source to share
Test-AzureName did not work with our build agents, I selected this standard and checked if null, use -ErrorAction Ignore to stop it from being thrown
# Check for storage account and create if not found
$StorageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $StorageAccountRG -ErrorAction Ignore
if ($StorageAccount -eq $null)
{
New-AzureRmStorageAccount -Location "West Europe" -Name $StorageAccountName -ResourceGroupName $StorageAccountRG -SkuName Standard_LRS -Kind Storage
$StorageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $StorageAccountRG
}
source to share
I usually do the following (works for almost 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}
}
source to share
Perhaps you can use the Get-AzureRmResource cmdlet. If the resource exists, it returns information about the specified resource, including the resource type; If not, it returns $ null. eg.:
$MyRes=Get-AzureRmResource -ResourceName "MyResourceName" -ResourceGroupName "MyResourceGroupName" if ($null == $MyRes) { # Not existing }
source to share
I needed to check for a variable in my Azure Automation account using Get-AzureRmAutomaitonVariable before deciding whether to create it. user888734 the solution to use "catch" helped me work around this issue where I was blocked for 2 days :-)
try {
$existingVariable = Get-AzureRMAutomationVariable -ResourceGroupName $resourceGroup -AutomationAccountName $automationAccountName -Name $variable
} catch {
New-AzureRmAutomationVariable -ResourceGroupName $resourceGroup -AutomationAccountName $automationAccountName -Name $variable -Value $value -Encrypted $False
}
source to share