删除特定的CName条目[PowerShell]

时间:2016-01-20 13:09:35

标签: powershell dns automation cname windows-server-2012-r2

我正在尝试在内部DNS中自动创建和删除CName记录。我们正在运行Windows Server 2012 R2并使用PowerShell DNS服务器cmdlet。

查询和创建CNames不是问题,此行创建web.test.dev.contoso.com CName并将其链接到dev01.contoso.com. AName条目

Add-DnsServerResourceRecordCName -ZoneName "contoso.com" -HostNameAlias "dev01.contoso.com." -Name "web.test.dev"

此行检索链接到web.test.dev.contoso.com AName的dev01.contoso.com. CName

Get-DnsServerResourceRecord -RRType CName -ZoneName "contoso.com" | ? {$_.RecordData.HostNameAlias -eq "dev01.contoso.com." -and $_.HostName -eq "web.test.dev"

但删除CName记录是个问题,我可以检索CName并将其传递给Remove-DnsServerResourceRecord cmdlet likeo:

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"

但是我收到了这个错误:

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

是否有人能够根据条目的值使用Remove-DnsServerResourceRecord cmdlet删除CName记录,还是只删除具有特定名称的所有CName?

编辑:根据Frode F的回答,最后的命令是:

Get-DnsServerResourceRecord -RRType CName -ZoneName "contoso.com" | ? {$_.RecordData.HostNameAlias -eq "dev01.contoso.com." -and $_.HostName -eq "web.test.dev" | Remove-DnsServerResourceRecord -ZoneName $zoneName -Force

1 个答案:

答案 0 :(得分:2)

您不能将-RRType "CName"与管道(输入对象)一起使用。删除它,它应该工作。

使用管道时,只有zonename和zonescope是有效的可选参数。

Syntax

  

参数集:InputObject
  Remove-DnsServerResourceRecord [ - ZoneName ]   <字符串> [-CimSession< CimSession []> ] [-ComputerName< String> ]   [-Force] [-InformationAction   < System.Management.Automation.ActionPreference> {SilentlyContinue |   停止|继续|查询|忽略|暂停}] [-InformationVariable   < System.String> ] [-PassThru] [-ThrottleLimit< Int32> ] [ - ZoneScope   < System.String> ] [-Confirm] [-WhatIf] [< CommonParameters>] [   < WorkflowParameters>]