简体   繁体   English

Powershell - Get-aduser使用$ var返回任何值

[英]Powershell - Get-aduser return no value using a $var

i'm using this command to query AD for usernames:: 我正在使用此命令来查询AD的用户名::

get-aduser -filter 'SamAccountName -like "trembto*"

The AD return me some result that comply with that search. AD给我一些符合该搜索的结果。

when I try to apply this line with a $var inside, I get no result: 当我尝试在内部使用$ var来应用这一行时,我得不到任何结果:

$userloop = "trembto"
get-aduser -filter 'SamAccountName -like "$($userloop)*"'

I should get the same result but it always returning me nothing, no error message I tried may alternative for the var part but in vain. 我应该得到相同的结果,但它始终没有给我任何回报,没有我尝试的错误信息可能替代var部分但是徒劳无功。

Thank for helping me 谢谢你的帮助

Variable expansion will not happen when single quotes are used to create a string. 使用单引号创建字符串时,不会发生变量扩展。 You must use double quotes to create a string for variable expansion to occur. 必须使用双引号来创建一个字符串,以便进行变量扩展。 In your case, you need to use double quotes to create the filter string, and use single quotes around the expanded variable. 在您的情况下,您需要使用双引号来创建过滤器字符串,并在扩展变量周围使用单引号。

Change to this: 改为:

$userloop = "trembto"
get-aduser -filter "SamAccountName -like '$($userloop)*'"

You can see this behavior by inspecting the string you use for your filter parameter. 您可以通过检查用于filter参数的字符串来查看此行为。

Test: 测试:

$userLoop = "trembto"
$filter = 'SamAccountName -like "$($userLoop)*"'

Output of $filter: $ filter的输出:

SamAccountName -like "$($userLoop)*"

Changed to: 变成:

$userLoop = "trembto"
$filter = "SamAccountName -like '$($userLoop)*'"

Outputs: 输出:

SamAccountName -like 'trembto*'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM