Home > Software engineering >  Sending a HTML email with ad user groups
Sending a HTML email with ad user groups

Time:11-23

im new to Powershell and i was wondering if someone can help me. Im trying to send html email with ad user groups to their manager.

My issue is:

The table contains only the AD group name column and the description. I would also like it to contain the user's full name and title.

My other problem, i would also like the script to validate if the user or manager email exists before sending it. If not, return an error message and try again.

If anyone is able to help me I would be very grateful. It will help me better understand and improve myself.

Thank you!!

$UserName = (Read-Host "Username").trim()
$EmailSuperior = (Read-Host "Email address of superior") #or if we can just type de samaccountname it will be perfect

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style   "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style   "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style   "TD{border: 1px solid black; padding: 5px; }"
$style = $style   "</style>"

# Get group memberships from reference user
$adUser = Get-ADUser -Filter "SamAccountName -eq '$($UserName)'" -Properties Name, Title, Manager, MemberOf

# Define parameters for mailing and send mail to IT-responsible person to review access
$MemberOf = Get-ADPrincipalGroupMembership -Identity $UserName| Get-ADGroup -Properties * | Select name, description | Sort-Object -Property name | ConvertTo-Html -Head $style
$SmtpServer = 'mail.contoso.com'
$FromSender = '[email protected]'
$Subject = 'Review Access: '   $UserName

# Email Body Set Here
$Body ="
Hello,<br>
<br>
We ask that you verify if these groups are to remain or if any need to be removed with their change of position.<br>
<br>
<br>
$MemberOf
<br>
"

Send-MailMessage -SmtpServer $SmtpServer -From $FromSender -To $EmailSuperior -Bcc $FromSender -Subject $Subject -Encoding "UTF8" -Body $Body -BodyAsHtml

CodePudding user response:

I like to create reports like this using pscustomobjects:

# Get group memberships from reference user
$adUser = Get-ADUser -Filter "SamAccountName -eq '$($UserName)'" -Properties Name, Title, Manager, MemberOf


# Define parameters for mailing and send mail to IT-responsible person to review access
$Report = $aduser.MemberOf | 
  Get-ADGroup -Properties description | Foreach {
    [pscustomobject]@{
      UserName    = $adUser.Name
      Title       = $adUser.Title
      GroupName   = $_.Name
      description = $_.description
    }
  } | 
  Sort-Object -Property GroupName | ConvertTo-Html -Head $style

# outputs like this if not converted to html:

UserName     Title       GroupName  description                                                       
--------     -----       ---------  -----------                                                       
John Smith   Employee    Apps_Full  Applications Full Access                                          
John Smith   Employee    Apps_Read  Applications Read Access                                     

And to get the manager email:

$input = Read-Host "Email address or username of superior"
$EmailSuperior = if ($input -notlike '*@*') { 
  Get-ADUser $input -Properties mail | select -ExpandProperty mail
}
  • Related