处理Azure Powershell特定错误和响应时出错
尝试在powershell中设置一些最终将进入Azure自动化的内容。具体而言,尝试使用以下命令添加新的DNS记录:处理Azure Powershell特定错误和响应时出错,azure,powershell,Azure,Powershell,尝试在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
$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"
}
我想根据错误响应得到它,我可以打印一个特定的输出。
例如,第一个错误:记录集$Hostname已经存在,因此无法再次创建。我希望能够接受这一点,而只是向用户输出:复制主机名;请再试试
我正在努力解决如何将特定错误放入“捕获”中的问题
任何帮助都将不胜感激您可以使用以下语法
try {
}
catch [YourExceptionType.. e.g. Microsoft.Rest.Azure.CloudException]
{
}
或使用
$_.Exception
在挡块里面
您甚至可以有多个捕捉块。
以下关于页面中描述了确切的语法:
get-help about_try_catch_finally -ShowWindow
您的powershell代码中存在问题。对于字符串,应该使用.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" }
}
但如果我理解正确的话,它们是相同的“例外类型”。例如,无论是重复的主机名还是无效的IP,异常类型似乎是相同的?然后您可以在catch块内查询$\异常.exception.Message。。。要获取上一个错误的详细信息,还可以查询$error[0]或$error[0]。GetTypeSo是这样的吗?请尝试{New AzureRMDNSRecordSet-Name$Hostname-RecordType A-ZoneName$Zone-ResourceGroupName$RG-ttl 3600-DnsRecords New AzureRMDNSRecordConfig-IPv4address$IP-ErrorAction Stop}catch[$\.Exception.Message包含提供的IP地址$IP无效{写入输出IP无效}catch$\.Exception.Message contains已存在,因此{写入输出主机名重复;请选择另一个}是否正确?catch{if$\.Exception.Message-包含提供的IP地址$IP无效{写入输出IP无效}if$\.Exception.Message-已存在,因此{写入输出主机名重复;请选择另一个}}您可能希望检查除消息之外的其他属性,以帮助您。如果您执行$u2;.Exception | Get Member,则可以获得完整列表。如果确实是该异常的代码,则可能存在包含有用信息的Body属性: