0

I have a powershell function that gets information from Active directory. It will send an email and the body of the email will contain whatever group is associated with that email address. As you can see below [email protected] is listed twice. Rather sending 2 emails to [email protected], I would rather send one e-mail with both groups in the body. How can I achieve this? Thanks

email               group
[email protected]      number1
[email protected]      number2
[email protected]     number1
[email protected]     number3

(Get-ADUser -Identity lbono –Properties MemberOf | Select MemberOf).MemberOf | Get-ADGroup -Properties ManagedBy | Select Name, ManagedBy, Distinguishedname, GroupCategory |

Where-Object {
$_.Distinguishedname -notlike "*Unity*" -and $_.Distinguishedname -notlike "*DynastyGroups*" -and $_.name -notlike "*Technical Library*" }|
ForEach-Object {
If ($_.ManagedBy) {
$result = New-Object PSObject
Add-Member -input $result NoteProperty 'Group Name' $_.Name
Add-Member -input $result NoteProperty 'Managed By' ((Get-ADUser -Identity $_.ManagedBy).givenName + ' ' + ((Get-ADUser -Identity $_.ManagedBy).surName))
Add-Member -input $result NoteProperty 'Email' (Get-ADUser -Identity $_.ManagedBy -Properties mail).Mail
Add-Member -input $result NoteProperty 'Group Type' $_.GroupCategory
Write-Output $result
}

} | select 'Group Name','Managed By','Email','Group Type' | sort 'Managed By'
3
  • 2
    What format is the information returned in? Is that just a pure String object? Commented Feb 14, 2013 at 22:04
  • the format is just your typical sorted Powershell output. Commented Feb 14, 2013 at 22:07
  • 1
    Please supply the script(remove sensitive information if any). Commented Feb 14, 2013 at 22:07

2 Answers 2

1

I think this is perfect use-case for Group-Object cmdlet. Snippet that shows how you can take advantage of it:

@'
email,group
[email protected],number1
[email protected],number2
[email protected],number1
[email protected],number3
'@ | ConvertFrom-Csv | Group-Object email | foreach {
    "To: {0} Body: {1}" -f 
        $_.Name, 
        (($_.Group | foreach { $_.group }) -join ', ')
}

Alternative: use hashtables:

$mails = @{}
@'
email,group
[email protected],number1
[email protected],number2
[email protected],number1
[email protected],number3
'@ | ConvertFrom-Csv | ForEach-Object {
    if ($mails.ContainsKey($_.email)) {
        $mails."$($_.email)" += ", $($_.group)"
    } else {
        $mails."$($_.email)" = $_.group
    }
}

$mails.GetEnumerator() | ForEach-Object {
    "To: {0} Body: {1}" -f $_.Key, $_.Value
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since nothing is specified I assume that your function My-Function returns an array of objects, with one object per email+group combination. Also, I assume that Email and Group is stored in properties called email and group.

My-Function | Group-Object Email | % { 
    New-Object psobject -Property @{
        Email = $_.Name
        Groups = ($_.Group | select -ExpandProperty Group)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.