如何在 PowerShell Active Directory 过滤器中转义 \ 反斜杠字符

发布于 2025-01-11 21:51:38 字数 507 浏览 0 评论 0原文

我正在尝试筛选 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-01-18 21:51:38

使用 -LDAPFilter 参数,然后用转义序列 \5C 替换所有文字反斜杠:

Get-ADObject -LDAPFilter '(info=*\5C\5Cserver\5Cshare*)'

您可以编写一个通用函数来处理转义字符串值:

function ConvertTo-LDAPFilterString
{
  param(
    [string]$Value
  )

  $charsToEscape = "\*()`0".ToCharArray()

  foreach($char in $charsToEscape){
    $escapeSequence = '\{0:X2}' -f +$char
    $Value = $Value.Replace("$char", $escapeSequence)
  }

  return $Value
}

现在您可以这样做:

$escapedPath = ConvertTo-LDAPFilterString '\\server\share'
Get-ADObject -LDAPFilter "(info=*$escapedPath*)"

至于为什么,这不是Active Directory特有的,而是LDAPv3设计的一部分。

RFC 4515 § 3 描述了 LDAPv3 过滤字符串的正确语法,其中包括所谓的“值编码规则”:

[...] 代表的八位字节
ASCII 字符“*”(ASCII 0x2a)、“(”(ASCII 0x28)、“)”(ASCII
0x29)、“\”(ASCII 0x5c) 和 NUL (ASCII 0x00) 表示为
反斜杠“\”(ASCII 0x5c)后跟两个十六进制数字
表示编码八位字节的值。

这种狭窄且特殊的编码规则的原因是允许对过滤器字符串进行明确仅前向解析 - 中的 \子句的值部分总是固定长度转义序列的开始,因为文字\被编码为固定长度-width 转义序列:)

Use the -LDAPFilter parameter, then substitute all literal backslashes with the escape sequence \5C:

Get-ADObject -LDAPFilter '(info=*\5C\5Cserver\5Cshare*)'

You could write a generic function to take care of escaping string values:

function ConvertTo-LDAPFilterString
{
  param(
    [string]$Value
  )

  $charsToEscape = "\*()`0".ToCharArray()

  foreach($char in $charsToEscape){
    $escapeSequence = '\{0:X2}' -f +$char
    $Value = $Value.Replace("$char", $escapeSequence)
  }

  return $Value
}

Now you can do:

$escapedPath = ConvertTo-LDAPFilterString '\\server\share'
Get-ADObject -LDAPFilter "(info=*$escapedPath*)"

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 octets that represent the
ASCII characters "*" (ASCII 0x2a), "(" (ASCII 0x28), ")" (ASCII
0x29), "\" (ASCII 0x5c), and NUL (ASCII 0x00) are represented as a
backslash "\" (ASCII 0x5c) followed by the two hexadecimal digits
representing the value of the encoded octet.

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文