PowerShell - OR运算符

时间:2018-06-12 15:07:53

标签: powershell

我试图运行此命令来获取A和CNAME记录,但它似乎无法正常工作,我的语法错误。它只检索CNAME类型的DNS记录。

实际命令是: Get-DnsServerResourceRecord -RRType" CNAME"

$results = Get-DnsServerZone | % {
$zone = $_.zonename
Get-DnsServerResourceRecord $zone -filter {(RRType -like "CNAME") -or (RRType -like "A")}   | select @{n='ZoneName';e={$zone}}, HostName, RecordType, @{n='RecordData';e={if ($_.RecordData.IPv4Address.IPAddressToString) {$_.RecordData.IPv4Address.IPAddressToString} else {$_.RecordData.NameServer.ToUpper()}}}
}

$results | Export-Csv -NoTypeInformation c:\temp\DNSRecords10.csv -Append 

2 个答案:

答案 0 :(得分:0)

您应该使用Where-Object进行过滤,因为cmdlet没有自己的参数:

Get-DnsServerResourceRecord -ZoneName $zone |
    Where-Object { $_.RRType -in 'CNAME', 'A' } |
    Select-Object -Property @(
        @{ Name       = 'ZoneName'
           Expression = { $zone }
         }
        'HostName'
        'RecordType'
        @{ Name       = 'RecordData'
           Expression = {
                if ($_.RecordData.IPv4Address.IPAddressToString) {
                    $_.RecordData.IPv4Address.IPAddressToString
                } else {
                    $_.RecordData.NameServer.ToUpper()
                }
            }
         }
    )

作为脚注,如果您不使用通配符,则-like与使用-eq相同。

文档:

答案 1 :(得分:0)

the solution proposed by TheIncorrigible1的替代方法是简单地发出两个查询:

@(
  Get-DnsServerResourceRecord $zone -RRType CNAME
  Get-DnsServerResourceRecord $zone -RRType A
) | select @{n='ZoneName';e={$zone}}, HostName, RecordType, @{n='RecordData';e={if ($_.RecordData.IPv4Address.IPAddressToString) {$_.RecordData.IPv4Address.IPAddressToString} else {$_.RecordData.NameServer.ToUpper()}}}