troubleshooting Question

Powershell Get-AdObejct with -filter option containing variable

Avatar of WurthAdmin
WurthAdminFlag for Afghanistan asked on
Powershell
2 Comments1 Solution178 ViewsLast Modified:
Hi,
When i execute this powershell command line i have some result :
Get-ADObject -Properties mail, proxyAddresses -Filter {mail -like "*pierre.w*" -or proxyAddresses -like "*pierre.w*"} |ft

but when i execute those lines i've got a error :
$emailportion="pierre.w"
$filtre="{mail -like ""*$emailportion*"" -or proxyAddresses -like ""*$emailportion*""}"
Get-ADObject -Properties mail, proxyAddresses -Filter $filtre

Get-ADObject : Error parsing query: '{mail -like "*pierre.w*" -or proxyAddresses -like "*pierre.w*"}' Error Message: 'syntax error' at position: '1'.
At line:1 char:1
+ Get-ADObject -Properties mail, proxyAddresses -Filter $filtre
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Get-ADObject], ADFilterParsingException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADObject

The variable $filtre contain : {mail -like "*pierre.w*" -or proxyAddresses -like "*pierre.w*"}

The help on get-ADObject show the -filter option as a string
SYNTAX
    Get-ADObject [-AuthType {Negotiate | Basic}] [-Credential <PSCredential>] [-IncludeDeletedObjects] [-Properties <String[]>] [-ResultPageSize <Int32>] [-ResultSetSize <Int32>] [-SearchBase <String>]
    [-SearchScope {Base | OneLevel | Subtree}] [-Server <String>] -Filter <String> [<CommonParameters>]
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros