本文介绍了如何在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筛选器字符串中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!