简体   繁体   English

PowerShell Active Directory - 无法在 Get-ADUser 过滤器中使用数组值

[英]PowerShell Active Directory - Can't use array value in filter for Get-ADUser

I have two variables;我有两个变量; $test and $accounts[0].upn $test$accounts[0].upn

Both are Strings, both contain the exact same value.两者都是字符串,都包含完全相同的值。

When I run:当我跑步时:

Get-ADUser -Filter "UserPrincipalName -eq '$test'"

I get the result I want, however when I run:我得到了我想要的结果,但是当我运行时:

Get-ADUser -Filter "UserPrincipalName -eq '$accounts[0].upn'"

It does not return any result.它不返回任何结果。

I get that $accounts is an array, but I thought I should get the same result given the variables are both of same type and value.我知道 $accounts 是一个数组,但我认为我应该得到相同的结果,因为变量的类型和值都相同。

See my screenshot below a better understanding of what I am trying to achieve, I only made the $test variable for debugging and I would like the command to work with $accounts[0].upn .请参阅下面的屏幕截图,以更好地了解我要实现的目标,我只制作了$test变量用于调试,我希望该命令与$accounts[0].upn Apologies I had to black out some personal information for privacy reasons, it shouldn't hinder understanding the screenshot.抱歉,出于隐私原因,我不得不删除一些个人信息,这不应该妨碍理解屏幕截图。

Demonstration Screenshot演示截图

Any help is much appreciated!任何帮助深表感谢!

The issue here is that powershell doesnt know when to stop interpreting the variable within the string (its called expression expansion ).这里的问题是 powershell 不知道何时停止解释字符串中的变量(它称为表达式扩展)。

so to fix this you should wrap any variable with properties in a sub expression so powershell knows when to stop expanding it.所以要解决这个问题,您应该将任何具有属性的变量包装在子表达式中,以便 powershell 知道何时停止扩展它。

Get-ADUser -Filter "UserPrincipalName -eq '$accounts[0].upn'"

Should become应该成为

Get-ADUser -Filter "UserPrincipalName -eq '$($accounts[0].upn)'"

Note the $() with your variable inside it.注意里面有你的变量的$()

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

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