Reputation: 315
Trying to get something set up in powershell that will eventually go into Azure automation. Specifically, trying to add a new DNS record, using the following command:
$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
The command will return one of the following:
1) If the record already exists:
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) If the IP is invalid:
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) It's successful:
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
I'm trying to get errors, using the "try" "catch" but have not been successful. Here's what I've tried:
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"
}
I'd like to get it where based on the error RESPONSE, I can print a specific output. Example, the first error: "The record set $Hostname exists already and hence cannot be created again". I'd like to be able to take that and instead, just output to the user: "Duplicate hostname; please try another".
I'm struggling with how to put the specific errors in the 'catch'
Any help would be greatly appreciated
Upvotes: 0
Views: 8956
Reputation: 29995
There is an issue in your powershell code. For string
, you should use .Contains()
instead of -contains
.
I did not have your environment, so I just write a similar code for the error handling:
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"
}
}
And the test result is as below:
So for your case, the scripts should like below:
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" }
}
Upvotes: 2
Reputation: 2939
You can either use the syntax
try {
}
catch [YourExceptionType.. e.g. Microsoft.Rest.Azure.CloudException]
{
}
or use
$_.Exception
inside the catch block.
You can even have multiple catch blocks. The exact syntax is described in the following about_page:
get-help about_try_catch_finally -ShowWindow
Upvotes: 0