Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

parse a String inside object

$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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading