PowerShell的对象不一样

如果我pipe道到where-object { $_.someProperty -notlike "*someValue*" } ,不-notlike似乎过滤不仅仅是匹配,而且someProperty是空的对象。

来自TechNet的where-object -notlike

Specifies the Not-Like operator, which gets objects when the property value does not match a value that includes wildcard characters.

根据定义,为什么不-notlike不返回someProperty为空的对象呢? 因为,没有find匹配?

只是为了澄清:我正在执行Get-ADComputer -Filter * -Property MemberOf,someotherstuff,someotherstuff | where-object { $_.memberof -notlike "*somepartialdn*" } Get-ADComputer -Filter * -Property MemberOf,someotherstuff,someotherstuff | where-object { $_.memberof -notlike "*somepartialdn*" }

期望是返回计算机对象,因为没有匹配,即使属性值为空。

这取决于你的“空”的定义。

如果someProperty没有值,那么有效值是$null 。 您的string比较不适用于$null

如果someProperty是一个空string( ""[String]::Empty ),则应用string比较。

 $values = "indonesia","turkmenistan",$null,"columbia" $values |Where-Object {$_ -notlike "*istan"} # Results in @("indonesia","columbia") $values = "indonesia","turkmenistan",[String]::Empty,"columbia" $values |Where-Object {$_ -notlike "*istan"} # Results in @("indonesia","","columbia") 

这只是一个猜测,但您可以通过执行以下操作来避开它:

where-object { $_.someProperty -ne "" -and $_.someProperty -notlike "*someValue*" }

我很确定你正在经历的是由于数据被作为一个stringpipe道…所以空值被视为""