尝试在Powershell中进行设置,最终将这些内容引入Azure自动化。具体来说,尝试使用以下命令添加新的DNS记录:
$Hostname = "testhostname"
$Zone = "subdomain.domain.com"
$IP = "1.2.3.4"
$RG = "testresourcegroup"
New-AzureRMDNSRecordSet -Name "$Hostname" -RecordType A -ZoneName $Zone -ResourceGroupName $RG -ttl 3600 -DnsRecords (New-AzureRMDNSRecordConfig -IPv4address "$IP") -ErrorAction Stop
该命令将返回以下内容之一:
1)如果记录已经存在:
New-AzureRMDNSRecordSet : The Record set testhostname exists already and hence cannot be created again.
At line:1 char:1
+ New-AzureRMDNSRecordSet -Name "$Hostname" -RecordType A -ZoneName $Zo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmDnsRecordSet], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Dns.NewAzureDnsRecordSet
2)如果IP无效:
New-AzureRMDNSRecordSet : The provided ip address '3333.4.21.11' is not valid.
At line:1 char:1
+ New-AzureRMDNSRecordSet -Name "$Hostname" -RecordType A -ZoneName $Zo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmDnsRecordSet], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Dns.NewAzureDnsRecordSet
3)成功:
Id : /subscriptions/(subscriptionID)/resourceGroups/testresourcegroup/providers/Microsoft.Network/dnszones/subdomain.domain.com/A/testhostname
Name : testhostname
ZoneName : subdomain.domain.com
ResourceGroupName : testresourcegroup
Ttl : 3600
Etag : (removed)
RecordType : A
TargetResourceId :
Records : {1.2.3.4}
Metadata :
ProvisioningState : Succeeded
我试图使用“ try”“ catch”来获取错误,但是没有成功。这是我尝试过的:
try {
New-AzureRMDNSRecordSet -Name "$Hostname" -RecordType A -ZoneName $Zone -ResourceGroupName $RG -ttl 3600 -DnsRecords (New-AzureRMDNSRecordConfig -IPv4address "$IP") -ErrorAction Stop #-ErrorVariable badIP -ErrorAction SilentlyContinue | Out-null
}
catch {
Write-Host "There was an error"
}
我想根据错误RESPONSE来获取它,我可以打印特定的输出。 例如,第一个错误:“记录集$ Hostname已经存在,因此无法再次创建”。我希望能够接受它,而是将其输出给用户:“重复的主机名;请尝试另一个”。
我正在努力将特定错误放入“捕获”中
任何帮助将不胜感激
答案 0 :(得分:0)
您可以使用语法
try {
}
catch [YourExceptionType.. e.g. Microsoft.Rest.Azure.CloudException]
{
}
或使用
$_.Exception
在挡块内。
您甚至可以拥有多个捕获块。 确切的语法在以下about_page中进行了描述:
get-help about_try_catch_finally -ShowWindow
答案 1 :(得分:0)
您的Powershell代码中有问题。对于string
,应该使用.Contains()
而不是-contains
。
我没有您的环境,所以我只写了类似的代码来处理错误:
try
{
Copy-Item -Path d:\t2.txt -Destination d:\333\ -ErrorAction Stop
}
catch
{
$message = $_.Exception.message
if($message.Contains("Cannot find path"))
{
Write-Output "the source path is not correct"
}
if($message.Contains("The filename, directory name"))
{
Write-Output "the destination path is not correct"
}
}
因此,对于您的情况,脚本应如下所示:
try
{
New-AzureRMDNSRecordSet -Name "$Hostname" -RecordType A -ZoneName $Zone -ResourceGroupName $RG -ttl 3600 -DnsRecords (New-AzureRMDNSRecordConfig -IPv4address "$IP") -ErrorAction Stop
}
catch
{
if ($_.Exception.Message.Contains("The provided IP address $IP is invalid"))
{ Write-Output "The IP is invalid" }
if ($_.Exception.Message.Contains("exists already and hence"))
{ Write-Output "The hostname is duplicated; please pick another" }
}