Regex Powershell获得ADOrganizationalUnit正则表达式

Regex Powershell获得ADOrganizationalUnit正则表达式,regex,powershell,active-directory,Regex,Powershell,Active Directory,有人能解释一下我如何在get-ADOrganizationalUnitcmdlet中正确使用语法,以便使用正则表达式匹配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])$"

有人能解释一下我如何在get-ADOrganizationalUnitcmdlet中正确使用语法,以便使用正则表达式匹配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:分析查询时出错:“(名称-匹配$regex)”错误消息:“运算符不受支持:-匹配”位于位置:“7”

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

$regex.Match("20151112")

非常感谢您的帮助。

-Filter
ActiveDirectory
-cmdlet中只支持一部分操作符,而
-match
不是其中之一。经验法则是,如果可以在LDAP筛选器中完成,则可以使用powershell操作符在
-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.
LDAP
操作员描述等价物
--------------- ------------------------------ ---------------------
-eq等于。这将=
不支持通配符
搜索
-不等于。这会!x=y
不支持通配符
搜索
-大约等于~=
-le按字典顺序小于=y
-ge词典编纂更大>=
大于或等于

-gt字典大于!x错误消息告诉您问题:不支持运算符(
-match
)。换句话说,您不能在该cmdlet的筛选器中使用正则表达式匹配项。请尝试
-like
或获取更多结果并在返回时对其进行筛选。get-ADOrganizationalUnit-SearchBase$targetPath`-filter{(Name-like$regex)}在语法上是正确的,但不返回任何结果,即使执行-filter{Name-like“2015*”}显示了应该符合条件的匹配项