Cacophony777
asked on
Using Variable in Get-ADUser Advanced Filter Search
Why doesn't this work?
It works fine if i type in something specific instead of using the variable. Here is the error it throws out:
Anybody have any ideas?
Thanks,
Cacophony
$SearchExpression = "SamAccountName -like `"*smith*`""
Get-ADUser -F {$SearchExpression}
It works fine if i type in something specific instead of using the variable. Here is the error it throws out:
Get-ADUser : Error parsing query: '$SearchExpression' Error Message: 'syntax error' at position: '1'.
At line:1 char:11
+ Get-ADUser <<<< -F {$SearchExpression}
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: '$SearchExpression' Error Message: 'syntax error' at position: '1'.
,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Anybody have any ideas?
Thanks,
Cacophony
Of course you could take it even further with splatting.
$searchExpression = @{ filter = "SamAccountName -like '*smith*'"
searchbase = "DC=some-ou,DC=domain,DC=com"
properties = @("passwordlastset","title")
}
Get-ADuser @searchExpression
my guess is that you can't do it that way because "-like" is an operator.
you have to define like this:
$type='SamAccountName'
$matching='smith'
then your script becomes
get-ADuser -f {$type -like $matching}
This way you preserve the operator.
you have to define like this:
$type='SamAccountName'
$matching='smith'
then your script becomes
get-ADuser -f {$type -like $matching}
This way you preserve the operator.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Didn't realize the brackets weren't necessary. Huh. Thanks for the help! ...I'll also have to look into this splatting thing... looks quite useful.
Open in new window