如何在 PowerShell Active Directory 过滤器中转义 \ 反斜杠字符
我正在尝试筛选 Active Directory 中“信息”字段中具有 UNC 的组,但很难找到如何转义 UNZ 路径中的反斜杠字符。以下
命令不返回结果: Get-ADGroup -filter 'Info -like "*\\server\share*"' -Properties Name,Info | Select Name,Info
而以下内容确实会返回一些结果,但如果两个组具有相似的 UNC 路径,则结果不准确:
Get-ADGroup -filter 'Info -like "*server*share*"' -属性名称、信息 |选择名称,信息
我尝试过使用 \5c
、\u5c
、[char]0x25
、`u {5c}
,*\\\\server\\path*
,*`\`\server`\path*
都没有成功,并且已运行出于想法和搜索结果进行尝试。
I'm trying to filter groups in Active Directory that have a UNC in the Info field, but am having great difficulty finding how to escape the backslash character in the UNZ path. The following returns no results:
Get-ADGroup -filter 'Info -like "*\\server\share*"' -Properties Name,Info | Select Name,Info
Whereas the following does return some results, but would not be accurate if two groups had similar UNC paths:
Get-ADGroup -filter 'Info -like "*server*share*"' -Properties Name,Info | Select Name,Info
I've tried using \5c
, \u5c
, [char]0x25
, `u{5c}
, *\\\\server\\path*
, *`\`\server`\path*
all without success, and have run out of ideas and search results to try.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
-LDAPFilter
参数,然后用转义序列\5C
替换所有文字反斜杠:您可以编写一个通用函数来处理转义字符串值:
现在您可以这样做:
至于为什么,这不是Active Directory特有的,而是LDAPv3设计的一部分。
RFC 4515 § 3 描述了 LDAPv3 过滤字符串的正确语法,其中包括所谓的“值编码规则”:
这种狭窄且特殊的编码规则的原因是允许对过滤器字符串进行明确仅前向解析 - 中的
\
子句的值部分总是固定长度转义序列的开始,因为文字\
也也被编码为固定长度-width 转义序列:)Use the
-LDAPFilter
parameter, then substitute all literal backslashes with the escape sequence\5C
:You could write a generic function to take care of escaping string values:
Now you can do:
As to why that is, it's not specific to Active Directory, but part of the design of LDAPv3.
RFC 4515 § 3 describes the correct grammar of LDAPv3 filter strings, which includes something called "the value-encoding rule":
The reason for this narrow and perculiar encoding rule is to allow unambiguous forward-only parsing of filter strings - a
\
in the value part of a clause is always the start of a fixed-length escape sequence because a literal\
would also have been encoded as a fixed-width escape sequence :)