Home > Enterprise >  parse a String inside object
parse a String inside object

Time:02-26

$GroupMembersCount = $null
$GroupMembersCount = gam print groups domain <domain> members managers owners | ConvertFrom-Csv | Where-Object { [int]$_.ManagersCount -eq 0 -and [int]$_.OwnersCount -eq 0 -and [int]$_.MembersCount -le 3 }

This gives me an object one object member is group members and that is a string with spaces like EmailAddress EmailAddress2 EmailAddress3 and so on.

One Group email can have many members.

$Results = $null
$Results = $GroupMembersCount | ForEach-Object {
   [PSCustomObject]@{
       GroupEmail = $_.Email
       members = $_.members    #THIS IS THE STING WITH 1 OR MORE MEMBERS WITH A SPACE
   }
}

I have tried to nest a foreach and the string didn't parse.

I have tried to replace the space with a comma and I have tried to convert the string both with the same error as below

$GroupMembersCount.members = $GroupMembersCount.members | ConvertFrom-String

The property 'members' cannot be found on this object. Verify that the property exists and can be set.

How do I parse that string and not lose the group email address the members belong to ?

CodePudding user response:

Assuming it is safe to split by \s (any whitespace character), this should generate the export you're looking for:

$Results = $GroupMembersCount | ForEach-Object {
    foreach($member in $_.members -split '\s') {
        [PSCustomObject]@{
            GroupEmail = $_.Email
            Members = $member
        }
    }
}
  • Related