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
