HTML Email - Multiple Commands

Hi all,

Let me preface this by saying I don't know much about PowerShell. However, we needed a couple projects tasks done and I took them on to use the opportunity to gain some insight and skills. So my knowledge consists of the one completed project I completed earlier in the day, in combination with the 5-6 hours I've spent searching the Web for help and information.

What I want to do is list the members of the Administrators, Domain Admins, and Enterprise Admins groups. I would like to format the information into three tables in a single email, and use task scheduler to run this and send the report once per week.

I've been successful in displaying the list of one group in a formatted table and I know how to schedule the task. But, I have not been able to get the remaining two groups to output into a table in the same email (they get outputted in the powershell window).

Here's my code:

import-module ActiveDirectory
$smtpServer = "smptServer.com"
$smtpFrom = "youraccount@yourdomain.com"
$smtpTo = "theiraccount@theirdomain.com"
$messageSubject = "Subject Goes Here"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true


$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>"

$message.Body = Get-ADGroupMember -Identity "Administrators" | Select-Object Name,SamAccountName | ConvertTo-Html -Head $style	

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

Basically, for Get-ADGroupMember -Identity "Administrators" I want the same thing but for "Domain Admins" and "Enterprise Admins" as well.

Can anyone help me out? Any help is much appreciated.


  • Edited by KSIIT 13 hours 47 minutes ago
April 21st, 2014 4:40pm

If you look at the help for Get-ADGroupMember then it does support piping in ADGroup Objects in it:
So just use this to create the Message body:

$message.Body = Get-ADGroup -Filter 'Name -eq "Administrators" -or Name -eq "Domain Admins"' | Get-ADGroupMember| Select-Object Name,SamAccountName | ConvertTo-Html -Head $style

Note have skipped the "Enterprise Admins" in the filter here ...You get the idea ;)

Look the the help of the two cmdlets to dig a little deeper.

Hope this helps.

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2014 2:28am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics