Azure Powershell-检查资源是否存在 [英] Azure Powershell - Check to see if resource exists
问题描述
我正在使用Powershell来自动设置Azure环境-创建存储帐户,数据库,网站等.
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
但是,我还没有找到一种优雅的方式来做到这一点.如果该项目不存在,则某些"Get" cmdlet会引发异常,并且捕获该项目有点hack:
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.
}
有更好的方法吗?
推荐答案
为此,您应使用 Test-AzureName 而不是Get-AzureStorageAccount.
You should use Test-AzureName for this instead of Get-AzureStorageAccount.
if (!Test-AzureName -Storage $Name)
{
# create the storage account.
}
这也将适用于Cloud Services,Web Apps和Service Bus命名空间.对于您的数据库,您将不得不采用现有方法.
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.
**
**
对于v2资源(ARM),情况大致相同.例如,v1或v2存储帐户的DNS名称将相同,例如contoso.blob.core.windows.net
.对于Azure Web应用程序(以前称为Azure网站)也是如此,在其中您将拥有一个DNS名称,例如contoso.azurewebsites.net
.因此,换句话说,Test-AzureName
对于ARM中的这些资源同样适用.
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.
一个显着的区别是虚拟机的DNS名称.在v1中,虚拟机包含在云服务中,并获得DNS名称,例如contoso.cloudapp.net
.对于v2虚拟机,公共DNS名称由公共IP地址资源提供,例如,美国东部的虚拟机的DNS名称为contoso.eastus.cloudapp.azure.com
.要测试此DNS名称的可用性,您应该使用 Test-AzureRmDnsAvailability cmdlet.例如,
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.
}
这篇关于Azure Powershell-检查资源是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!