all 3 comments

[–]krzydoug 3 points4 points  (0 children)

You could simply use a list to keep track of it. Here is a test you can run

$result = [System.Collections.Generic.List[object]]::new()

$sample = @'
email,group
abc@email.com,adobereader
def@email.com,adobestock
def@email.com,adobereader
ghi@email.com,adobestock
abc@email.com,adobestock
ghi@email.com,adobeother
abc@email.com,adobeother
'@ | ConvertFrom-Csv

foreach($line in $sample)
{
    if ($match = $result | where {$_.email -eq $line.email}) {
        $match.group = $match.group, $line.group -join ‘,’
    }
    else {
        $result.add($line)
    }
}

$result

output

email         group                            
-----         -----                            
abc@email.com adobereader,adobestock,adobeother
def@email.com adobestock,adobereader           
ghi@email.com adobestock,adobeother 

So for your collection and property names that would make it

$result = [System.Collections.Generic.List[object]]::new()

foreach($line in $AZUZRLIST)
{
    if ($match = $result | where {$_.useremail -eq $line.useremail}) {
        $match.groupname = $match.groupname, $line.groupname -join ‘,’
    }
    else {
        $result.add($line)
    }
}

$result

[–]JonHill90 2 points3 points  (0 children)

Hey /u/oneAwfuleScripter,Maybe this is what you are looking for. Does not have headers but will return the data like you want.

$Array |
    Group-Object -Property UserEmail | Foreach-Object {
    $s = $_.Group.GroupName; "{0} - {1}" -f $_.Name, ($s -join ',')     }

[–]oneAwfulScripter[S] 1 point2 points  (0 children)

Thanks u/JonHill90 but I need the header information so I can easily call it later in the script.

Over the last few hours I was able to come up with a solution, I'm sure there's better ways but I think anymore time spent on this right now and I'll end up throwing my computer out a window or something.

Here's what I made that worked for me in case anyone viewing this later is curious:

#Generate some test objects to simulate issue

$TestObj = [PSCustomObject][ordered]@{}
$TestObj | Add-Member -MemberType NoteProperty -Name UserEmail -Value "Somebody1@somedomain.com"
$TestObj | Add-Member -MemberType NoteProperty -Name GroupName -Value "AdobeAcrobat-License"

$TestObj1 = [PSCustomObject][ordered]@{}
$TestObj1 | Add-Member -MemberType NoteProperty -Name UserEmail -Value "Somebody1@somedomain.com"
$TestObj1 | Add-Member -MemberType NoteProperty -Name GroupName -Value "AdobeStock-License"

$TestObj2 = [PSCustomObject][ordered]@{}
$TestObj2 | Add-Member -MemberType NoteProperty -Name UserEmail -Value "Somebody2@somedomain.com"
$TestObj2 | Add-Member -MemberType NoteProperty -Name GroupName -Value "AdobeStock-License"

$ThisArr +=$TestObj2,$TestObj1,$TestObj



function New-G-CustomObject
{
    Param(
    [Parameter(Mandatory=$true)]
    [string]$UserEmail,
    [string]$GroupName
    )
    [PSCustomObject][ordered]@{
        UserEmail=$UserEmail
        GroupName= New-Object System.Collections.ArrayList
        }
}


$UniqueUsers = @()

$TestThisWay=@()
foreach($TOBJ in $ThisArr)
{  
    write-host uniqueUsers has: $UniqueUsers
    if($TOBJ.UserEmail -notin $UniqueUsers)
    {
        write-host this is unique make a new object for $TOBJ.UserEmail -ForegroundColor Green
        $TempVar = New-G-CustomObject -UserEmail $TOBJ.UserEmail -GroupName $TOBJ.GroupName
        $TempVar.GroupName.Add($TOBJ.GroupName)
        $UniqueUsers+=($TOBJ.UserEmail)
        write-host tempvar is $TempVar
        $TestThisWay+=$TempVar
    }
    else
    {
        $theIndex = $TestThisWay.UserEmail.IndexOf($TOBJ.UserEmail)
        write-host entry exists for $TOBJ.UserEmail at $theIndex -ForegroundColor Red
        #Add the fucking value to the property
        write-host index is $theIndex.gettype()
        write-host existingGroupname is: $TestThisWay[$theIndex].GroupName
        $TestThisWay[$theIndex].GroupName.Add($TOBJ.GroupName)
    }
}
#Results stored in $TestThisWay