Powershell Get-AdObejct with -filter option containing variable

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>]
WurthAdminAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try it like this:
$emailportion="pierre.w"
$filtre = "(mail -like '*$emailportion*') -or (proxyAddresses -like '*$emailportion*')"
Get-ADObject -Properties mail, proxyAddresses -Filter $filtre

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WurthAdminAuthor Commented:
Thank you it works well.

Best Regard
JP
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.