【问题标题】:How to use a variable in an AD filter string?如何在 AD 过滤器字符串中使用变量?
【发布时间】:2015-07-28 17:40:15
【问题描述】:

我有以下 powershell 脚本来获取 LDAP 属性。我想将 BadgeID 作为变量传递,而不是对其进行硬编码。 LDAP 查询正在使用硬编码的 BadgeID,但看起来如果我用变量替换硬编码的值,那么它不会返回任何内容。

$BadgeID = $item["BadgeID"]
$SearchBase = 'OU=Sales-Users,DC=mayoclinic,DC=com';
$LdapServer = 'MAYOCLNDC413.MAYOCLINIC.COM';

Get-ADUser -SearchBase $searchbase -SearchScope 'subtree' -Server $ldapserver -filter 'BadgeID -like "*74049660*"' -Properties * |
    Select -Property Name, AccountExpires, AccountExpirationDate, BadgeID

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    反转过滤器表达式的引号:

    Get-ADUser -Filter "BadgeID -like '*$BadgeID*'" ...
    

    变量在双引号字符串中展开,但不是在单引号字符串中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多