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?
33 Answers
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
Important to note the above syntax highlighting will make the sample above look wrong because it misses the tokens like $FilterBase and $Filter when there are inside single quotes inside double quotes. Remember that single quotes are just apostrophes when inside double quotes, therefore the tokens should be colored differently and not look like strings.
> "('$FilterBase' -like '$Filter')" ('department' -like 'IT')
Paste a sample like above and see what it resolves to - best way to figure it out.
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
2Thanks 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
ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobHBtYmWEd36OoJytZZGZwrSx0WauoqyYYrq2uNOip6WdXZu2rcDEq6pmrpGntqKuy56q