简体   繁体   English

$env:USERNAME 在 Get-ADuser 上过滤时未定义?

[英]$env:USERNAME not defined when filtering on Get-ADuser?

Can anyone explain why this fails:谁能解释为什么会失败:

PS C:\Windows\system32> Get-ADUser -Filter {SamAccountName -eq $env:USERNAME}
Get-ADUser : Variable: 'env' found in expression: $env is not defined.

But this succeeds:但这成功了:

PS C:\Windows\system32> $u=$env:USERNAME

PS C:\Windows\system32> Get-ADUser -Filter {SamAccountName -eq $u}

DistinguishedName : CN<snip>

Is the above the most elegant workaround or am I missing something?以上是最优雅的解决方法还是我错过了什么?

This works:这有效:

$username = $env:USERNAME
Get-ADUser -Filter 'SamAccountName -eq $username'

And this works too:这也有效:

Get-ADUser -Filter "SamAccountName -eq '$($env:USERNAME)'"

在此处输入图片说明

You know, this works too:你知道,这也有效:

Get-aduser "$samaccountname"

or或者

Get-aduser "$env:USERNAME"

Don't even need to bother with "-Filter" if you are passing the samaccountname如果您正在传递 samaccountname,甚至不需要理会“-Filter”

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

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