I'm trying to automate the creation and deletion of CName records in our internal DNS. We are running Windows Server 2012 R2 and are using the PowerShell DNS Server cmdlets.
The querying and creation of CNames are not a problem, this line creates the web.test.dev.contoso.com CName and links it to the dev01.contoso.com. AName entry
Add-DnsServerResourceRecordCName -ZoneName "contoso.com" -HostNameAlias "dev01.contoso.com." -Name "web.test.dev"
This line retrieves the web.test.dev.contoso.com CName that is linked to the dev01.contoso.com. AName
Get-DnsServerResourceRecord -RRType CName -ZoneName "contoso.com" | ? {$_.RecordData.HostNameAlias -eq "dev01.contoso.com." -and $_.HostName -eq "web.test.dev"
But deleting the CName record is the issue, I can retrieve the CName and pass it to the Remove-DnsServerResourceRecord cmdlet likeso:
Get-DnsServerResourceRecord -RRType CName -ZoneName "contoso.com" | ? {$_.RecordData.HostNameAlias -eq "dev01.contoso.com." -and $_.HostName -eq "web.test.dev" | Remove-DnsServerResourceRecord -ZoneName $zoneName -RRType "CName"
But I get this error:
The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take
pipeline input.
+ CategoryInfo : InvalidArgument: (DnsServerResourceRecord:PSObject) [Remove-DnsServerResourceRecord], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Remove-DnsServerResourceRecord
Has anyone been able to delete a CName record using the Remove-DnsServerResourceRecord cmdlet based on the entry's values or does it just delete all CNames with a certain name?
EDIT: As per Frode F's answer, the final command is:
Get-DnsServerResourceRecord -RRType CName -ZoneName "contoso.com" | ? {$_.RecordData.HostNameAlias -eq "dev01.contoso.com." -and $_.HostName -eq "web.test.dev" | Remove-DnsServerResourceRecord -ZoneName $zoneName -Force