I’m pulling a group membership list from an AD account into the variable $groups
, and I get a list of SIDs. When I get an individual SID from $groups
into a variable $Lgroup
, it includes other information and looks like this:
@{SID=S-1-5-21-1771855492-4138186766-173940457-513}
In an attempt to get just the SID into $group
I can manually put the above value into $LGroup
and use this “Pattern”:
$pattern = '(?<=\=).+?(?=\})'
Then run this line:
$group = [regex]::Match($Lgroup, $pattern).value
the result is $group
contains just the SID as I would expect:
S-1-5-21-1771855492-4138186766-173940457-513
However, if I try to do this same thing on each item in $groups
through a ForEach
loop within a script, I get an error. Here’s my code:
$SrcUser = "WilsoFC"
$TgtUser = "SkeltRD"
$Groups=(get-adprincipalgroupmembership $SrcUser | select SID)
$pattern = '(?<=\=).+?(?=\})'
foreach ($Lgroup in $groups) {
Write-Host " Checking $Lgroup "
$group = [regex]::Match($Lgroup, $pattern).value
Write-Host " Truncated is $group"
}
The error is
Cannot find an overload for "Match" and the argument count: "2".
At C:\utils\GroupTest.ps1:26 char:4
+ $group = [regex]::Matches($Lgroup, $pattern).value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Line 26 is:
$group = [regex]::Match($Lgroup, $pattern).value
Again, I can enter this line at the PS prompt and I get the proper SID into $group
without error.
I’m confused.
Can someone un-confuse me?
—K