Link to home
Start Free TrialLog in
Avatar of P S
P S

asked on

Powershell script to find name of group which server is part of

There are close to 15 security groups which start with "XY_*". I have couple of windows server in a CSV file and each server is member of 1 group out of those 15 groups. I need to find which server is member of which group and it's name.

can someone help me with a powershell script?

Thanks
PS
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of P S
P S

ASKER

Thanks oBdA. as usual you've been very helpful and script worked perfectly. Just need a small favour, would you mind explaining the regex you used and also if there is any web page that can explain the powershell regex would be very useful.

Thanks again
^CN= - match "CN=" at the beginning of the string (^)
(?<Group> - open a named group 'Group'
$($prefix).*? - match anything .* non-greedy (shortest match possible), starting with the prefix selected ...
) - [close the named group - $Matches['Group'] will contain the actual group name extracted from the DN]
,(CN|DC|OU)=" - ... until a comma followed by either CN, or DC, or OU, and an = sign.

There are hundreds of websites explaining regular expressions; they're not limited to PowerShell. There may be slight differences for the more complex constructs, but the basics apply to most of the languages that have RegEx support (just don't use findstr.exe - its RegEx support is just horrible ...)
Regular Expression Language - Quick Reference
https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Avatar of P S

ASKER

Thanks obDA. you're the best.
Avatar of P S

ASKER

ObDA gave a perfect script. please close this question