Powershell Get-ADOrganizationalUnit Regex

时间:2016-02-22 17:07:53

标签: regex powershell active-directory

有人可以解释我是如何在 Get-ADOrganizationalUnit cmdlet中使用正则表达式来匹配OU名称的吗?

$targetPath = "OU=Some OU, DC=some.domain"

# Capture date in yyyymmdd format and I checked it is .Net valid 
$regex = [regex] "^((20\d{2}))((0?[1-9]|1[012]))(0[1-9]|[12][0-9]|3[01])$"

# I need the correct syntax to use below cmdlet
Get-ADOrganizationalUnit -SearchBase $targetPath`
    -Filter {(Name -match $regex)}

结果: Get-ADOrganizationalUnit:解析查询时出错:'(名称-match $ regex)'错误消息:'运算符不支持:-match'在位置:' 7'

然而这样做是成功的,所以正则表达式很好

$regex.Match("20151112")

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

-Filter中的

ActiveDirectory - cmdlet仅支持运算符子集,而-match不是其中之一。经验法则是,如果可以在LDAP过滤器中完成,可以使用powershell-operators在-Filter中完成。

  

支持的运营商       下表显示了常用的搜索过滤器运算符。

                                                 LDAP
Operator         Description                     Equivalent
--------------- ------------------------------ ---------------------
-eq              Equal to. This will             =
                 not support wild card
                 search.
-ne              Not equal to. This will         !x = y
                 not support wild card
                 search.
-approx          Approximately equal to          ~=
-le              Lexicographically less than     <=
                 or equal to
-lt              Lexicographically less than     !x >= y
-ge              Lexicographically greater       >=
                 than or equal to
-gt              Lexicographically greater than  !x <= y

-and             AND                             &
-or              OR                              |
-not             NOT                             !
-bor             Bitwise OR                      :1.2.840.113556.1.4.804:=
-band            Bitwise AND                     :1.2.840.113556.1.4.803:=
-recursivematch  Use LDAP_MATCHING_RULE_IN_CHAIN :1.2.840.113556.1.4.1941:=
                 (Note: This control only works
                  with Windows 2008 and later.)
-like            Similar to -eq and supports     =
                 wildcard comparison. The only
                 wildcard character
                 supported is: *
-notlike         Not like. Supports wild         !x = y
                 card comparison.

来源:{{3}}