0

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"
2
  • 3
    Awesome, Can you please post some code? And let us know where you are stuck? Commented Sep 17, 2013 at 20:48
  • Which of the columns identifies the user? I.e. what is the unique identifier for a particular user? Commented Sep 17, 2013 at 21:23

1 Answer 1

1

You can import the csv, extract each unique manager name, and then compile a subset of the list by using the ManagedBy values you've already extracted:

$Users = Import-Csv C:\filename.csv
$Managers = $Users | Sort-Object -Property ManagedBy -Unique

for($Manager in $Managers)
{
    $Employees = $Users |Where-Object {$_.ManagedBy -eq $Manager.ManagedBy}
    // send your email here with the contents of $Employees
}
Sign up to request clarification or add additional context in comments.

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.