Sign in to follow this  
Followers 0
haze1434

Powershell - match, like, contains not working with @ symbol

4 posts in this topic

Hi guys,

I have the following PowerShell script, which finds the list of groups a user is a member of, formats them to name only, and then is supposed to list yes or no to which groups begin with the '@' symbol;

$CurrentGroups = Get-ADuser JOEBLOGGS -property MemberOf | % {$_.MemberOf | Get-ADGroup | select Name | sort name}

foreach($Group in $CurrentGroups) {
if("$Group" -match "`@") { echo Yep $Group }
else { echo Nope $Group }}

 

User JOEBLOGGS is a member of;

  • @Testone
  • @Testtwo
  • Testthree
  • Testfour

 

On running this script, I would expect the output to be;

Yep
@Testone
Yep
@Testtwo
Nope
Testthree
Nope
Testfour

However, it instead states 'Yep' to all of them, not just the groups beginning with an '@' symbol.

 

I have tried -like, -match, -contains as well as "\@", "@*" and "@". None work correctly.

 

If I run -match "@Test", this works fine, but not all of the groups in our AD that begin with an @ symbol follow with the same digits, so I need this to work with just searching for all groups that begin with an @ symbol.

Thank you.

0

Share this post


Link to post
Share on other sites

Try "`@*" maybe?

0

Share this post


Link to post
Share on other sites
On ‎03‎/‎02‎/‎2017 at 4:56 PM, digip said:

Try "`@*" maybe?

Thanks. Still no luck :sad: This still returns all as a positive match ('Yep'), when the last 2 should return negative.

Weird!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.