Powershell Regular Expressions

Hi All,

I have this line of code in my powershell script:

#Only for Marie Curie users
if ($user.email -like "*@asda.com") {

     
    $GetADUser = {Get-ADUser -Filter "EmailAddress -eq '$($user.email)'" -Properties * -ErrorAction Stop}

.....................
........
..........
}

problem is that if there is an comma in the email, e.g testacco'unt1@asda.com, the script fails, how can i validate the script so there isn't a comma?
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
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.

SunilTechnical Specialist - Wintel Operations.Commented:
Do you mean , (comma) or ' (quote)?
0
SunilTechnical Specialist - Wintel Operations.Commented:
Refer this link to use the escape character in the powershell
http://www.rlmueller.net/PowerShellEscape.htm
0
footechCommented:
A quick note - nothing in the code is making any use of regular expressions.  "-eq" does a direct match, and "-like" enables the use of the wildcard "*".
0
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

Kelly GarciaSenior Systems AdministratorAuthor Commented:
apologies i meant quote
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You'll indeed have to escape single quotes here, as you are building a string containing a string expression. It is probably much easier to build the filter string differently:
    $GetADUser = {Get-ADUser -Filter "EmailAddress -eq '" + $user.email.Replace("'", "''") + "'" -Properties * -ErrorAction Stop}

Open in new window

or use a scriptblock:
    $GetADUser = {Get-ADUser -Filter {EmailAddress -eq "$($user.email)"} -Properties * -ErrorAction Stop}

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
Sajen JoseCommented:
You can probably try this regex

If ($user.email -match "^[a-zA-Z0-9.!£#$%&'^_`{}~-]+@asda.com")

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sajen, how should that help? The AD query still would have the quote. and the original IF condition is sufficient to catch email addresses for that domain (which is the intent).
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.