如何在 AD 过滤器字符串中使用变量? [英] How to use a variable in an AD filter string?

查看:21
本文介绍了如何在 AD 过滤器字符串中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have following powershell script to get LDAP properties. I would like to pass the BadgeID as variable instead of hard coding it. The LDAP query is working with hard-coded BadgeID but it looks like if I replace the hard-coded value with a variable then it doesn't return anything.

$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

推荐答案

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

Reverse the quotes of your filter expression:

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

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

Variables are expanded in double-quoted strings, but not in single-quoted strings.

这篇关于如何在 AD 过滤器字符串中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆