'Get-ADUser with multiple filters & variables

I'm trying to get AD users into a variable using multiple filters. However one of the filters has variables in it & I can't get it to work... I have searched for similar issues & tried applying those but nothing seems to work.

$FilterBase = "department"
$Filter = "IT"

$ADusers = Get-ADUser -ResultSetSize $null -SearchBase "OU=Users,DC=mydomain,DC=com" -Properties * -Filter {(Enabled -eq $True) -and ($FilterBase -like $Filter) -and (cn -notlike ""SMB_*"")} |
            Select-Object distinguishedName |
            Sort-Object distinguishedName

I'm trying to fill $ADusers with all enabled users whose commonname doesn't start with "SMB_" (don't ask) & where the department is IT. I used -like to prevent issues if the values in AD would have different casings (uppercase, lowercase, mixed case, ...).

The reason that I'm using variables for this is because in the end the script will be dynamic. At some point $FilterBase is going to be "company" instead of "department" and $Filter is going to be "HR" instead of "IT" etc...

But I just can't seem to get it to work:

 Get-ADUser : Error parsing query: '(Enabled -eq $True) -and ($FilterBase -like $Filter) -and (cn -notlike ""SMB_*"")' Error Message: 'syntax error' at position: '74'.
    At line:4 char:12

I have tried using quotes around the variables like "$Filter", "$($Filter)", ' $Filter ' but alas. And I know it's not best practice to use variables in Filter but I can't think of any other way to accomplish this.

Any suggestions?



Solution 1:[1]

its just simply syntax error.

$enabled = 'Enabled'
$EnabledTrueOrFalse = $true
$SN = 'Surname'
$surname = "Doe"
$OU = "OU=Users,DC=mydomain,DC=com"

Get-ADuser -filter{$enabled -eq $EnabledTrueOrFalse -and $SN -eq $surname} -SearchBase $OU -Properties * | Select-Object distinguishedName | Sort-Object distinguishedName

read more about it here

Solution 2:[2]

Thanks for the tips guys. I couldn't get it to work with multiple filters so I moved some filters to the where clause.

My current (working) code is now:

$FilterBase = "department"
$Filter = "IT"

$ADusers = Get-ADUser -ResultSetSize $null -SearchBase "OU=Users,DC=mydomain,DC=com" -Properties * -Filter "$FilterBase -like `"$Filter`"" |
Where {$_.Enabled -eq $True -and $_.CN -notlike "SMB_*"} |
Select-Object distinguishedName |
Sort-Object distinguishedName

Solution 3:[3]

the error has the key to the answer. I'm sure I'll find this again and use it myself because I look this up every year or so...

Error parsing query: '(Enabled -eq $True)...'

In this case the filter needs a simple string 'True' which the variable $True does equal.

Two options will work, either

Enabled -eq 'True' or Enabled -eq '$True'

but

Enabled -eq $True

will not.

This should work

  • Replaced the braces with double quotes so inside them the variables still parse
  • Put single quotes around all strings and variables that resolve into strings
  • '$True'
  • '$Filter'
  • 'SMB_*'

$FilterBase = "department"
$Filter = "IT"

$ADusers = Get-ADUser -ResultSetSize $null -SearchBase "OU=Users,DC=mydomain,DC=com" -Properties CN -Filter "(Enabled -eq '$True') -and ($FilterBase -like '$Filter') -and (CN -notlike 'SMB_*')" |
            Select-Object distinguishedName |
            Sort-Object distinguishedName

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 John Wolfe
Solution 3 Karl Fasick