I have a CSV file with a list of Users and who they are managed by with these headers:
DisplayName,Alias,CA5,Managedby
I want to import this list in powershell and be able to send one email to each manager with the list of accounts (DisplayName,Alias,CA5) that they are listed as the manager of in the managedby field. I can create the email with out issue I need to know how to compile all the lines of the CSV file that should all go to into the same email. With the sample CSV below I should end up with three emails (one each to ManagedByA, ManagedbyB and ManagedByC). The email to ManagedByA would have three lines containing:
DN1 A1
DN2 A2 DL
DN3 A3
My CSV look like this:
DisplayName,Alias,CA5,ManagedBy
DN1,A1,,ManagedByA
DN2,A2,DL,ManagedByA
DN3,A3,,ManagedByA
DN4,A4,,ManagedByB
DN5,A5,,ManagedByB
DN6,A6,DL,ManagedByB
DN7,A7,,ManagedByB
DN8,A8,,ManagedByC
DN9,A9,DL,ManagedByC
DN10,A10,,ManagedByC
My Code (from Mathias R. Jessen):
$Import = Import-CSV "C:\powershell\DL\Reports\DLReportEmail.csv"
### Find unique ManagedBy
$Managers = $Import | Sort-Object -Property ManagedBy -Unique
ForEach ($Manager in $Managers){
$Employees = $Import | Where-Object {$_.ManagedBy -eq $Manager.ManagedBy}
$Body = "DearXXXX, `r`n"
ForEach($Employee in $Employees){
$Alias = $Employee.Alias
$DisplayName = $Employee.DisplayName
$CA5 = $Employee.CA5
$Body += "$DisplayName : $Alias : $CA5 `r`n"
}
$Body += "`r`n"
$Body += "Thanks"