用问号通配符过滤Get-Adgroup似乎不起作用
我正在尝试获取一个以“用户-### - ”开头的广告组列表(#是数字0-9)。
我尝试使用Get-Adgroup -filter {name-like“ users- [0-9] [0-9] [0-9] [0-9] [0-9] - *”}
and and <代码> get-adgroup -filter {name-like“用户 - ???? - *”} ,但没有结果。
我当然可以使用get-adgroup -filter {name-like“用户 - *”}
,但这还包括所有在用户后有四个字符以外的东西的组。
然后,我决定尝试使用object和该代码返回预期组
get-adgroup -filter * | wher-object {$_。Name-like“用户 - [0-9] [0-9] [0-9] [0-9] [0-9] - *”
} “ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about/about/about_wildcards?view=powershell-7.2”我尝试了应该工作,但实际上没有。
有人知道我在做什么错,或者这只是ADGroup过滤的工作方式的错误?
I'm trying to get a list of AD groups that have a name that starts with "Users-####-" (# is a number 0-9).
I've tried using Get-ADGroup -Filter {name -like "Users-[0-9][0-9][0-9][0-9]-*"}
and Get-ADGroup -Filter {name -like "Users-????-*"}
, but got no results.
I can of course use Get-ADGroup -Filter {name -like "Users-*"}
, but this will also include all the groups that have something else than four characters after Users-.
I then decided to try using Where-Object and the this code returned the expected groups
Get-ADGroup -Filter * | Where-Object {$_.Name -like "Users-[0-9][0-9][0-9][0-9]-*"}
According to Microsoft documentation about wildcards, both ways I tried should work, but they actually don't.
Anybody have an idea what I'm doing wrong or is this just a bug in how ADGroup filtering works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个合理的假设,但是
-filter
参数由activedirectory
模块中的某些cmdlet展示的参数是欺骗性构造 - 它是设计为的看起来像 powershell的本机操作员语法,但是“在引擎盖下” CMDLET将过滤器表达式转换为有效的LDAP查询过滤器:因为LDAP无法识别Wildcard范围构造
[0-9] ,它最终会向目录存储询问名称字面意思以
用户开头的对象 - [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] -
-?
也是如此。由于
*
是LDAP接受的唯一通配符,因此您可以获得的最接近:然后使用
where-object
在客户端上进一步过滤结果(在这种情况下,我们'回到Powershell进行比较,我们可以再次使用所有通配符):That's a reasonable assumption, but the
-Filter
parameter exposed by some cmdlets in theActiveDirectory
module is a deceptive construct - it's designed to look like PowerShell's native operator syntax, but "underneath the hood" the cmdlet translates the filter expression to a valid LDAP query filter:Since LDAP doesn't recognize the wildcard range construct
[0-9]
, it ends up querying the directory store for objects where the name literally starts withUsers-[0-9][0-9][0-9][0-9]-
- same goes for?
.Since
*
is the only wildcard accepted by LDAP, the closest you can get is:And then filter the results further on the client with
Where-Object
(in which case we're back to PowerShell performing the comparison and we can use all the wildcards again):如::
在这种情况下,您可以将
-LDAPFILTER
与组合在一起,where-object
可以保持查询兼容和高效:As stated in about_ActiveDirectory_Filter:
In this case, you can combine
-LDAPFilter
withWhere-Object
to keep your query compatible and efficient:您可以在这种情况下使用
-filter
作为预滤波器,因此,至少您只能获得以用户开头的名称的组 -
。然后,在您可以进一步指定的目标子句中,在这种情况下,我将使用Regex
-Match
,例如:PS
-filter
应该是 String < /strong>,而不是脚本块You can use
-Filter
in this case as pre-filter, so at least you will get only groups with names starting withUsers-
.Then in a further Where-Object clause you can specify further and in this case I would use regex
-match
there like:P.S.
-Filter
should be a string, not a scriptblockPowerShell Active Directory模块中的滤波器具有奇数行为。
过滤器
powershell滤波器语法
The filters in the Powershell Active Directory module have odd behaviors.
Filter or Where Clause
PowerShell Filter Syntax