0

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

0

2 Answers 2

2

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: enter image description here

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" } 

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

8 Comments

But they are the same 'exception type' if i'm understanding correctly. for instance, whether it's a duplicate hostname or an invalid IP, the exception type appears to be the same?
Then you can query the $_.Exception.Message inside the catch block... To get the details for the last error you might also query $Error[0] or $Error[0].GetType()
So would that look like this? try { New-AzureRMDNSRecordSet -Name "$Hostname" -RecordType A -ZoneName $Zone -ResourceGroupName $RG -ttl 3600 -DnsRecords (New-AzureRMDNSRecordConfig -IPv4address "$IP") -ErrorAction Stop } catch [$_.Exception.Message contains "The provided IP address $IP is invalid" { Write-Output "The IP is invalid" } catch $_.Exception.Message contains "exists already and hence " { Write-Output "The hostname is duplicated; please pick another" } Would that be right?
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" } }
You may want to check for properties other than the message that may help you. If you do $_.Exception | Get-Member you can get a full list. There may be potentially a Body property with useful information, if this is indeed the code for that exception: github.com/Azure/azure-sdk-for-net/blob/psSdkJson6/src/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.