尝试将变量传递到[adsisearcher]时出现问题 [英] Issue with trying to pass variable into [adsisearcher]

查看:81
本文介绍了尝试将变量传递到[adsisearcher]时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这很简单,我会提前道歉,但是我已经研究了几天,无法独自解决或寻求其他探索途径.

I apologize in advance if this is something simple, but I've been researching for a few days now and unable to resolve on my own or find another route to explore.

基本上,我有下面的作品可以正常工作并返回我需要的东西:

Essentially I have the below which works and returns exactly what I need:

([adsisearcher]'(&(objectClass=user (samaccountname='*dland*'))').FindOne().Properties['samaccountname']    

返回其中包含dland的用户名.我想用变量($ usrNme1)替换 dland ,但在表达式或语句中出现意外令牌'$ usrNme1'))"错误."

which returns the username containing dland in it. I want to replace the dland with a variable ($usrNme1), but it errors out with "Unexpected token '$usrNme1'))'' in expression or statement."

我可以使用ActiveDirectory模块通过以下方式使其在本地运行:

I was able to get it working locally with using the ActiveDirectory module using the following:

Get-ADUser -Filter {SAMAccountName -like $usrNme1} | select-object -Property SAMAccountName    

但是不幸的是,在这种环境下,我无法将其用于其他计算机,而不得不寻找另一种方式,这是我找到替代品的最大途径.

But unfortunately I cannot use that against other computers in this environment and have to find another way and this is as far as I've gotten to finding a replacement.

这里的任何帮助将不胜感激:)对我来说,这是难题的最后一部分,它如此紧密而又无法弄清楚,实在令人沮丧!预先感谢您抽出宝贵的时间:)

Any assistance here would be greatly appreciated :) This is the last piece of the puzzle for me and its frustrating being so close and not being able to figure it out! Thanks in advance for taking the time :)

忘记评论了,该脚本将被推出并在Windows 7计算机上本地运行,这就是为什么我不能使用Get-ADUser的部分原因.

Forgot to comment, this script is going to be pushed out and run locally on windows 7 machines, which is part of the reason why I can't use Get-ADUser.

谢谢

大卫

推荐答案

您的查询有点畸形,因为它在 user 之后缺少括号,但是您可以像在下面的例子.放在 double 引号内的变量可以很好地扩展*(大多数情况下.对象参数需要子表达式).

Your query is a little malformed as it is missing a bracket after user but you can put variables in the string easily like in the following example. Variables placed inside double quotes will expand just fine* (Most of the time. Object parameters require subexpressions).

$accountname = "mcame*"
$query = "(&(objectClass=user)(samaccountname=$accountname))"
([adsisearcher]$query).FindOne().Properties['samaccountname']

注意:如果您查看此问题,,您将在进行通配符搜索时遇到问题.如果您的组织很大,则可能需要重新考虑使用前导和尾随的星号或任何复数形式.

Note: if you look at this question you will see issues doing the wildcard search that your are. If you have a large organization you might need to reconsider using leading and trailing asterices or whatever the plural is.

您的原始查询

除了括号之外,它不起作用的原因是因为您使用的是单引号.如果您查看此资源,它将继续显示说

Aside from the bracket the reason it was not working was since you were using the single quotes. If you look at this resource it goes on to say

比较字符串不会出现在引号中.过滤器displayName'Philipp Foeckeler'的内容如下:(displayName = Philipp Foeckeler).

Comparative strings do NOT appear in quotation marks. A filter for the displayName 'Philipp Foeckeler' would read as follows: (displayName=Philipp Foeckeler).

查询应该在没有那些内部引号的情况下工作.

Query should have worked without those inner quotes.

这篇关于尝试将变量传递到[adsisearcher]时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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