Get-Aduser -Filter will not accept a variable

前端 未结 7 807
挽巷
挽巷 2020-11-22 02:09

I\'d like to check if a user account already exists in the system.

$SamAc = Read-Host \'What is your username?\'
$User = Get-ADUser -Filter {sAMAccountName -         


        
7条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 02:35

    I have to comment on this because it really aggravated me to sort this out.

    Joseph Alcorn has the right idea. The filter parameter takes a string and then evaluates that in order to process the filter. What trips people up with this is that you are given the option to use curly brackets instead {}, and this doesn't work as you'd expect if you were using Where... it still has to be treated like a string.

    $SamAc = Read-Host 'What is your username?'
    $User = Get-ADUser -Filter "sAMAccountName -eq '$SamAc'"
    

    I recommend sticking to quotes to make it more clear/readable for yourself and others and to avoid potential syntax errors, or stick to Where{} in the pipeline. When doing so, I find it best to use double-quotes on the outside & single-quotes on the inside so you still get intellisense detection on the variable.

提交回复
热议问题