为什么此powershell或筛选器不工作?
我将一个对象保存到一个包含大量记录的变量中。我想根据两列筛选对象结果。两列中都可以出现相同的值。过滤器在为什么此powershell或筛选器不工作?,powershell,Powershell,我将一个对象保存到一个包含大量记录的变量中。我想根据两列筛选对象结果。两列中都可以出现相同的值。过滤器在HostName列上正常工作。但是在RecordData列上没有正确过滤。那里有一个结果,但筛选器没有检索它 下面是一个缺少结果的示例。所有4行都应出现在第一个查询中。我找不到我做错了什么。有人能帮忙吗 PS H:\> $DNSRecord | where { ($_.HostName -like "*gcod049*") -or ($_.RecordData -like "*gcod0
HostName
列上正常工作。但是在RecordData
列上没有正确过滤。那里有一个结果,但筛选器没有检索它
下面是一个缺少结果的示例。所有4行都应出现在第一个查询中。我找不到我做错了什么。有人能帮忙吗
PS H:\> $DNSRecord | where { ($_.HostName -like "*gcod049*") -or ($_.RecordData -like "*gcod049*") }
HostName RecordType Type Timestamp TimeToLive RecordData
-------- ---------- ---- --------- ---------- ----------
gcod049.domainname.com A 1 0 00:20:00 10.26.101.49
gcod049 A 1 0 00:20:00 10.26.101.49
PS H:\> $DNSRecord | where { ($_.hostname -like "*ssrsdev*") }
HostName RecordType Type Timestamp TimeToLive RecordData
-------- ---------- ---- --------- ---------- ----------
ssrsdev.domainname.com CNAME 5 0 01:00:00 gcod049.domainname.com.
ssrsdev CNAME 5 0 01:00:00 gcod049.domainname.com.
**更新:使用Select或Select Object with.ToString()时,值完全不同
PS H:\> $DNSRecord |
Select HostName, RecordData, @{Name="RecordDataString";Expression={$_.RecordData.ToString()}} |
where { ($_.HostName -like "*gcod049*") -or ($_.RecordData -like "*gcod049*") }
HostName RecordData RecordDataString
-------- ---------- ----------------
gcod049.domainname.com DnsServerResourceRecordA DnsServerResourceRecordA
gcod049 DnsServerResourceRecordA DnsServerResourceRecordA
PS H:\> $DNSRecord |
Select-Object -Property HostName, RecordData, @{Name="RecordDataString";Expression={$_.RecordData.ToString()}} |
where { ($_.HostName -like "*ssrsdev*") -or ($_.RecordData -like "*ssrsdev*") }
HostName RecordData RecordDataString
-------- ---------- ----------------
ssrsdev.domainname.com DnsServerResourceRecordCName DnsServerResourceRecordCName
ssrsdev DnsServerResourceRecordCName DnsServerResourceRecordCName
在此对象获取成员属性…
$DNSRecord | gm
TypeName: Microsoft.Management.Infrastructure.CimInstance#root/Microsoft/Windows/DNS/DnsServerResourceRecord
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object ICloneable.Clone()
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetCimSessionComputerName Method string GetCimSessionComputerName()
GetCimSessionInstanceId Method guid GetCimSessionInstanceId()
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializab...
GetType Method type GetType()
ToString Method string ToString()
DistinguishedName Property string DistinguishedName {get;}
HostName Property string HostName {get;}
PSComputerName Property string PSComputerName {get;}
RecordClass Property string RecordClass {get;}
RecordData Property CimInstance#Instance RecordData {get;set;}
RecordType Property string RecordType {get;}
Timestamp Property CimInstance#DateTime Timestamp {get;}
TimeToLive Property CimInstance#DateTime TimeToLive {get;set;}
Type Property uint16 Type {get;}
您是否尝试过强制powershell首先对
-或进行求值,并在的{…}
中插入括号,如下所示:
$DNSRecord | where { (($_.HostName -like "*gcod049*") -or ($_.RecordData -like "*gcod049*")) }
这来自DnsServerResourceRecord类型的RecordData属性的表视图,来自文件夹C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\DnsServer中的文件DnsServerPsProvider.Format.ps1xml。该属性有一长串if/else语句。在windows 10中安装rsat时,我有dnsserver模块,但我不知道如何使dnsserverresourcerecord工作
但您可以参考$\.RecordData.HostNameAlias了解CNAME类型。具有讽刺意味的是,正名意味着“真实姓名”
或者有时通过管道查找TR/i来查找某些内容更容易。类
操作符对$\uRecordData.ToString()的计算结果进行操作,这可能与默认显示输出显示的结果不同。RecordData是一个对象,而不是字符串。-如果必须的话,like会将左侧转换为字符串。$\ux.recorddata.tostring()说什么?可能有某个子属性有这个名称。我没有这个命令,你可以在xml格式文件中查找对象类型,看看它们如何显示主机名,并使用相同的代码。表示命令中的所有括号都是多余的。
elseif( $_.RecordType -eq "CNAME")
{
$_.RecordData.HostNameAlias
}
$DNSRecord | where { $_.HostName -match 'gcod049' -or
$_.RecordData.HostNameAlias -match 'gcod049' }