What am I missing here? I have been using this script for a while to dump a csv file to a samba share and it has worked flawlessly.
However I want to adjust it to email an HTML table weekly to a shared admin account for review. I have tried various ways to get the info to come into the table correctly but all have failed. It seems that the Properties or table titles work fine in csv format but do not come across correctly when using the Convert-ToHTML command.
I left the csv outfile part in the script below for troubleshooting and to keep a local server copy.
Thanks in advance for any help.
$alist = "Name`tAccountName`tDescription`twhenCreated`twhenChanged`tMemberof`n" $grouplist = Get-ADGroup -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,whenCreated,whenChanged,MemberOf | Sort-Object -Property Name $grouplist | ForEach-Object { $grps = $_.MemberOf | Get-ADGroup | ForEach-Object {$_.Name} | Sort-Object $arec = $_.Name,$_.SamAccountName,$_.Description,$_.whenCreated,$_.whenChanged $aline = ($arec -join "`t") + "`t" + ($grps -join "`t") + "`n" $alist += $aline } $alist | Out-File C:\email\Group-$((get-date).ToString("yyyy-MM-dd")).csv $csvfile = Import-Csv -Path C:\email\Group-$((get-date).ToString("yyyy-MM-dd")).csv $to = "admin@domain.com" $from = "adnotify@domain.com" $subject = Weekly AD User Audit $smtpServer = "smtprelay.domain.com" $tablebody = @" <style> TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;} TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;} </style> "@ $emailinfo = @" Weekly AD Group Audit for $((Get-Date).ToShortDateString())<BR> <BR> "@ $sigfile = "mailserver@domain.com" $bodytable = $csvfile | Select Name,SamAccountName,Description,whenCreated,whenChanged,MemberOf | ConvertTo-HTML -Head $tablebody -PreContent $emailinfo -PostContent $sigfile $message = New-Object System.Net.Mail.MailMessage $from, $to $message.Subject = $subject $message.IsBodyHTML = $true $message.Body = $bodytable $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $smtp.Send($message)