Powershell Loop within Loops - html

I have a PowerShell script that runs a disk usage report (From http://gallery.technet.microsoft.com/Disk-Space-HTML-Email-f8b6bbfe)
I'm trying to change it to display folder size as well, but have gotten stuck. I wrote this which works fine:
$volName = "D:\"
$folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer })
foreach ($folders in $folders)
{
$size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum)
write-host $folders.fullname ($size.sum / 1MB)
}
But it doesn't come out right when I try to insert it. This is what I have so far:
# Script will generate Disk Space Report for Exchange Servers (Daily, Weekly, Monthly)
$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition
$dir = Split-Path $scriptpath
#Variables to configure
$percentWarning = 25;
$percentCritcal = 15;
$smtpServer = "blah#blah.com"
$ReportSender = "blah#blah.com"
$users = "user1#mydomain.com", "user2#mydomain.com";
$MailSubject = "DiskSpace Report for $titledate"
#No change needed from here!!!
$reportPath = "$dir\Logs\"
$reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";
$diskReport = $reportPath + $reportName
$redColor = "#FF0000"
$orangeColor = "#FBB917"
$whiteColor = "#FFFFFF"
$greenColor = "#7FFF00"
$i = 0;
$computers = $env:COMPUTERNAME;
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";
If (Test-Path $diskReport)
{
Remove-Item $diskReport
}
$titleDate = get-date -uformat "%m-%d-%Y - %A"
$header = "
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title>DiskSpace Report</title>
<STYLE TYPE='text/css'>
<!--
table {
border: thin solid #666666;
}
td {
font-family: Tahoma;
font-size: 11px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;
table {
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='100%'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='25' align='center'>
<font face='tahoma' color='#003399' size='4'><strong>DiskSpace Report for $titledate</strong></font>
</td>
</tr>
</table>
"
Add-Content $diskReport $header
$tableHeader = "
<table width='100%'><tbody>
<tr bgcolor=#CCCCCC>
<td width='10%' align='center'>Server</td>
<td width='5%' align='center'>Drive</td>
<td width='25%' align='center'>Folder</td>
<td width='5%' align='center'>Folder Size</td>
<td width='10%' align='center'>Total Capacity(GB)</td>
<td width='5%' align='center'>Used Capacity(GB)</td>
<td width='5%' align='center'>Free Space(GB)</td>
<td width='5%' align='center'>Freespace %</td>
</tr>
"
Add-Content $diskReport $tableHeader
foreach($computer in $computers)
{
$disks = Get-WmiObject -ComputerName $computer -Class Win32_Volume -Filter "DriveType = 3" | Where-Object {$_.Label -ne "System Reserved" -and $_.DriveLetter -ne "C:"}
$computer = $computer.toupper()
foreach($disk in $disks)
{
$deviceID = $disk.Label;
$volName = $disk.Name;
$folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer });
foreach ($folders in $folders)
{
$size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum)
[float]$size = $disk.Capacity;
[float]$freespace = $disk.FreeSpace;
$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
$usedSpaceGB = [Math]::Round($sizeGB - $freeSpaceGB, 2);
$color = $greenColor;
if($percentFree -lt $percentWarning)
{
$color = $orangeColor
if($percentFree -lt $percentCritcal)
{
$color = $redColor
}
}
$dataRow = "
<tr>
<td width='10%'>$computer</td>
<td width='5%' align='center'>$volName</td>
<td width='25%' >$folders.fullname</td>
<td width='5%' >$size</td>
<td width='10%' align='center'>$sizeGB</td>
<td width='5%' align='center'>$usedSpaceGB</td>
<td width='5%' align='center'>$freeSpaceGB</td>
<td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td>
</tr>
"
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
$i++
}
}
}
$tableDescription = "
</table><br><table width='20%'>
<tr bgcolor='White'>
<td width='10%' align='center' bgcolor='#FBB917'>Warning less than $percentWarning% free space</td>
<td width='10%' align='center' bgcolor='#FF0000'>Critical less than $percentCritcal% free space</td>
</tr>
"
Add-Content $diskReport $tableDescription
Add-Content $diskReport "</body></html>"
if ($i -gt 0)
{
foreach ($user in $users)
{
Write-Host "Sending Email notification to $user"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = $ReportSender
$msg.Subject = $MailSubject
$msg.IsBodyHTML = $true
$msg.Body = get-content $diskReport
$smtp.Send($msg)
$body = ""
}
}
And it comes out looking like this:
http://oi40.tinypic.com/ru9sfb.jpg
Anyone know why the folder name and folder size variables aren't working?

I think the problem (or one of the problems) is this line: foreach ($folders in $folders). It should be foreach ($folder in $folders) and then everywhere in the for loop that you refer to the current folder you should use $folder (not $folders).
Give that a try and see how it looks.

Thanks for the help, turns out modifying the $folder (and created a few other) variables worked. Report with folders and folder sizes:
# Script will generate Disk Space Report for Exchange Servers (Daily, Weekly, Monthly)
$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition
$dir = Split-Path $scriptpath
#Variables to configure
$percentWarning = 25;
$percentCritcal = 15;
$smtpServer = "blah#blah.com"
$ReportSender = "blah#blah.com"
$users = "user1#mydomain.com", "user2#mydomain.com";
$MailSubject = "DiskSpace Report for $titledate"
#No change needed from here!!!
$reportPath = "$dir\Logs\"
$reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";
$diskReport = $reportPath + $reportName
$redColor = "#FF0000"
$orangeColor = "#FBB917"
$whiteColor = "#FFFFFF"
$greenColor = "#7FFF00"
$i = 0;
$computers = $env:COMPUTERNAME;
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";
If (Test-Path $diskReport)
{
Remove-Item $diskReport
}
$titleDate = get-date -uformat "%m-%d-%Y - %A"
$header = "
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title>DiskSpace Report</title>
<STYLE TYPE='text/css'>
<!--
table {
border: thin solid #666666;
}
td {
font-family: Tahoma;
font-size: 11px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;
table {
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='100%'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='25' align='center'>
<font face='tahoma' color='#003399' size='4'><strong>DiskSpace Report for $titledate</strong></font>
</td>
</tr>
</table>
"
Add-Content $diskReport $header
$tableHeader = "
<table width='100%'><tbody>
<tr bgcolor=#CCCCCC>
<td width='10%' align='center'>Server</td>
<td width='5%' align='center'>Drive</td>
<td width='25%' align='center'>Folder</td>
<td width='5%' align='center'>Folder Size (MB)</td>
<td width='10%' align='center'>Total Capacity(GB)</td>
<td width='5%' align='center'>Used Capacity(GB)</td>
<td width='5%' align='center'>Free Space(GB)</td>
<td width='5%' align='center'>Freespace %</td>
</tr>
"
Add-Content $diskReport $tableHeader
foreach($computer in $computers)
{
$disks = Get-WmiObject -ComputerName $computer -Class Win32_Volume -Filter "DriveType = 3" | Where-Object {$_.Label -ne "System Reserved" -and $_.DriveLetter -ne "C:"}
$computer = $computer.toupper()
foreach($disk in $disks)
{
$deviceID = $disk.Label;
$volName = $disk.Name;
$folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer });
foreach ($folder in $folders)
{
$sizefolder = (get-childitem $folder.fullname -recurse | measure-object -property length -sum)
$FolderPaths = $folder.fullname
[float]$FolderSize = $sizefolder.sum
$foldersizeMB = [Math]::Round($foldersize / 1048576, 2);
[float]$size = $disk.Capacity;
[float]$freespace = $disk.FreeSpace;
$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
$usedSpaceGB = [Math]::Round($sizeGB - $freeSpaceGB, 2);
$color = $greenColor;
if($percentFree -lt $percentWarning)
{
$color = $orangeColor
if($percentFree -lt $percentCritcal)
{
$color = $redColor
}
}
$dataRow = "
<tr>
<td width='10%'>$computer</td>
<td width='5%' align='center'>$volName</td>
<td width='25%' >$folderpaths</td>
<td width='5%' >$foldersizeMB</td>
<td width='10%' align='center'>$sizeGB</td>
<td width='5%' align='center'>$usedSpaceGB</td>
<td width='5%' align='center'>$freeSpaceGB</td>
<td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td>
</tr>
"
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
$i++
}
}
}
$tableDescription = "
</table><br><table width='20%'>
<tr bgcolor='White'>
<td width='10%' align='center' bgcolor='#FBB917'>Warning less than $percentWarning% free space</td>
<td width='10%' align='center' bgcolor='#FF0000'>Critical less than $percentCritcal% free space</td>
</tr>
"
Add-Content $diskReport $tableDescription
Add-Content $diskReport "</body></html>"
if ($i -gt 0)
{
foreach ($user in $users)
{
Write-Host "Sending Email notification to $user"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = $ReportSender
$msg.Subject = $MailSubject
$msg.IsBodyHTML = $true
$msg.Body = get-content $diskReport
$smtp.Send($msg)
$body = ""
}
}

Related

mPDF Japanese font configuration

I need to generate a PDF using mPDF and YuGothM.ttc font for a Japanese client. The page I need to export in PDF contains some text which in browser version (HTML) is rendered well, but in the PDF, the same text has some characters using a font and other characters some default font which is not approved by the client. I understand I have to define TTCfontID parameter in every font definition for .ttc files, but I just don't understand the meanings of those definitions and so how to properly configure fonts.
The script is used in a WordPress website using Polylang plugin for translations.
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$pdf = new \Mpdf\Mpdf([
//'mode' => '+aCJK',
'mode' => 'utf-8',
'format' => 'A3-L',
'margin_left' => 5,
'margin_right' => 5,
'margin_top' => 5,
'margin_bottom' => 5,
'fontDir' => array_merge($fontDirs, [
ASSETS_DIR ."fonts/Yugothib/",
]),
'fontdata' => $fontData + [
'yugoth' => [
'R' => 'YuGothR.ttc',
'TTCfontID' => [
'R' => 1,
]
],
],
'default_font' => 'yugoth',
'languageToFont' => new CustomLanguageToFontImplementation()
]);
$pdf->autoScriptToLang = true;
$pdf->autoLangToFont = true;
$pdf->showImageErrors = true;
$pdf->allow_charset_conversion = true;
$pdf->SetHTMLHeader();
$pdf->SetHTMLFooter();
$pdf->WriteHTML($this->getHtmlTemplate($stock, $userId, $loadedDate));
$pdf->Output($filepath, 'F');
And the HTML is also taken from a private function within the same class
function getHtmlTemplate($records, $userId, $loadedDate){
$user = $this->getUserInfo($userId);
$companyName = $user->client;
$html = "
<html lang=\"ja\">
<head>
<style>
.table {
font-family: 'yugoth', sans-serif;
font-size: 16px;
font-weight: normal;
}
.column-header {
text-align: center;
vertical-align: middle;
background-color: #777777;
color: #FFFFFF;
}
.cell{
border-left: 1px solid #efefef;
border-bottom: 1px solid #efefef;
}
.cell-last {
border-right: 1px solid #efefef;
}
.a-center {
text-align: center;
}
.w-80 {
width: 80px;
}
.w-quality {
width: 380px;
}
.w-150 {
width: 150px;
}
</style>
</head>
<body lang=\"ja\">
<table border=\"0\" style=\"width: 100%\">
<tr>
<td style=\"width:26%; text-align: left; font-size: 18px\">
<div>" . $companyName ."</div><br />
<div lang='ja'>" . pll__('Stock Info') ."</div>
</td>
<td class=\"a-center\" style=\"width:26%\">
<div style=\"font-size: 50px; color: #004729; font-weight: bold;\" lang='ja'>" . pll__('Stock Report'). "</div>
</td>
<td style=\"width:26%; text-align: right\">
<div>". $date ."</div><br />
<img src=\"" . ASSETS_URL . "images/logo.png\" width=\"200px\" />
</td>
</tr>
</table>
<table class=\"table\" border=\"0\" cellpadding=\"7\">
<thead>
<tr>
<th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 1") ."</th>
<th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 2") ."</th>
<th class=\"column-header w-150\">" . pll__("Col 3") ."</th>
<th class=\"column-header w-quality\">" . pll__("Col 4") . "</th>
<th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 5") . "</th>
<th class=\"column-header w-80\">" . pll__("Col 6") . "</th>
<th class=\"column-header\" style=\"width: 130px\">" . pll__("Col 7") . "</th>
<th class=\"column-header\">" . pll__("Col 8") . "</th>
<th class=\"column-header\">" . pll__("Col 9") . "</th>
<th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 10") . "</th>
<th class=\"column-header\">" . pll__("Col 11") . "</th>
<th class=\"column-header\" style=\"width: 120px\">" . pll__("Coll 12") . "</th>
<th class=\"column-header w-150\">" . pll__("Col 13") . "</th>
<th class=\"column-header w-150\">" . pll__("Col 14") . "</th>
</tr>
</thead>
<tbody>
";
/** #var StockItem $row */
foreach ($records as $row){
$html .= "
<tr>
<td class=\"cell a-center\">" . $row->col1 ."</td>
<td class=\"cell a-center\">".$row->col2."</td>
<td class=\"cell a-center w-150\">". $row->col3 ."</td>
<td class=\"cell w-quality\">".$row->col4."</td>
<td class=\"cell w-150\">".$row->col5."</td>
<td class=\"cell a-center w-80\">".$row->col6."</td>
<td class=\"cell a-center w-80\">".$row->col7."</td>
<td class=\"cell a-center\" lang='ja'>".$row->col8."</td>
<td class=\"cell a-center\">".$row->col9."</td>
<td class=\"cell a-center\">".$row->col10."</td>
<td class=\"cell a-center\">".$row->col11."</td>
<td class=\"cell a-center w-80\">".$row->col12."</td>
<td class=\"cell a-center w-150\">".$row->col13."</td>
<td class=\"cell cell-last a-center w-150\">".$row->col14."</td>
</tr>";
}
$html .= "
</tbody>
</table>
</body>
</html>
";
return $html;
}
}
Can someone help me with a proper configuration for mPDF or suggest some other configuration with other Japanese font which is already working?
Thanks in advance!
Eventually I've changed the script to use TCPDF and this way has actually worked very nice. I know this answer did not respond to the question I've asked, but it solved my problem.

Change table row color based on value

I have a table that looks something like this:
ID | photo | ident | status
------------------------------------------------
80 | img/photo1 | ACH3882 | V
81 | img/photo2 | SHD8837 | V
82 | img/photo3 | SFF4837 | X
83 | img/photo4 | DLL3266 | V
Is it possible to change the rows background color, depending on the value? So if status cell value is V, make yellow, and if X make green?
This is my table, and what I have tried:
<table class="blueTable" border="2" style= "background-color: #f9f9f9; color: #000000; margin: 0 auto;" >
<thead style= "background-color: #FFFFFF">
<tr>
<th>Photo</th>
<th>Ident</th>
<th>Status</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
if($row['status'] == 'V') {
$style = 'style="background-color:#00FF00;';
}
if($row['status'] == 'X') {
$style = 'style="background-color:#FF00FF;';
}
echo
"<tr>
<td>{$row['photo']}</td>
<td>{$row['ident']}</td>
<td>{$row['status']}</td>
<td><a href='delete.php?id={$row['id']};'>Delete</a></td>
</tr>";
}
/*freeresultset*/
$result->free();
}
?>
</tbody>
</table>
But somehow the background color does not change. Any suggestions?
You can use your status var as a class and add the colors for the classes in css like this
.color-v {
background-color: blue;
}
.color-x {
background-color: green;
}
<table class="blueTable" border="2" style= "background-color: #f9f9f9; color: #000000; margin: 0 auto;" >
<thead style= "background-color: #FFFFFF">
<tr>
<th>Photo</th>
<th>Ident</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
if ($result = $link->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
echo
"<tr class='color-{$row['status']}'>
<td>{$row['photo']}</td>
<td>{$row['ident']}</td>
<td>{$row['status']}</td>
</tr>";
}
/*freeresultset*/
$result->free();
}
?>
</tbody>
</table>

CSS Style override in Unix Shell

I am trying to send email from Unix using a HTML code. For paragraph tag I am trying to override style but its not working
(
echo "From: $FROM"
echo "To: $TO"
echo "Subject: $SUBJECT"
echo "Content-type: text/html"
echo ""
echo "<html>"
echo "<style>"
echo "table {border: 1px solid black; }"
echo "body { font-family: Verdana, Helvetica, sans-serif; background-color: #FFFFFF; }"
echo "p { font-family: Verdana, Helvetica, sans-serif; font-size: 12px; }"
echo "</style>"
echo "<body>"
echo "<p style="font-style: bold";>${BodyHeading}:</p>"
echo "<p>Content of the email: </p>"
echo "</body>"
echo "</html>"
) | sendmail $TO
#Line 14 we are overriding style for paragraph tag. But when we get email we are not getting bold text for that p tag
Use
(
echo "From: $FROM"
echo "To: $TO"
echo "Subject: $SUBJECT"
echo "Content-type: text/html"
echo ""
echo "<html>"
echo "<style>"
echo "table {border: 1px solid black; }"
echo "body { font-family: Verdana, Helvetica, sans-serif; background-color: #FFFFFF; }"
echo "p { font-family: Verdana, Helvetica, sans-serif; font-size: 12px; }"
echo "</style>"
echo "<body>"
echo "<p style='font-weight: bold;'>${BodyHeading}:</p>"
echo "<p>Content of the email: </p>"
echo "</body>"
echo "</html>"
) | sendmail $TO
Replace Line 15 with echo "<p style='font-weight: bold;'>${BodyHeading}:</p>" and it should work.
more over id you want to use double quotes(") in echo statements, then you should escape them using \ . E.g.
echo "<p style=\"font-weight: bold;\">${BodyHeading}:</p>"
Refer: https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
Replace echo "<p style="font-style: bold";>${BodyHeading}:</p>" with echo "<p style="font-weight: bold";>${BodyHeading}:</p>"

Import all CSV data and exported in a HTML email for report via Powershell

I have a CSV file formatted with date, name, and timeToExpire. I want to send that CSV data in a email. I would like to create a HTML file.
When I run the code I received the email but with only one data and not all of that.
I know between the "here" and "end" I need to put a loop inside the body parameter. Or if you have another solution or option welcome on that.
$From = "System Administrator <sysadm#>"
$To = "pcisn#email.com"
#$Cc = "rn#email.com"
#$Attachment = "logs\pcemail.csv"
$Subject = "Passwd Reminder Report"
$users = import-csv -Path $csvfile | select *
foreach($user in $users)
{
$date = $user.'Date'
$name = $user.'name'
$email = $user.'EmailAddress'
$expire = $user.'DaystoExpire'
$expireon = $user.'ExpiresOn'
$notify = $user.'Notified'
# Email Body Set Here, Note You can use HTML, including Images.
$body ="
<table style= ""font-family: arial, sans-serif;border-collapse: collapse;width: 100%;"" >
<tr style=""background-color: #f6f8f1;"" >
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">Date</th>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">Name</th>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">Email</th>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">Days to Expire</th>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">Expire On</th>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">Was Notify?</th>
</tr>
#=================== Here's the part that I need help ===========================
<tr style=""background-color: #dddddd;"">
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">$date</td>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">$name</td>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">$email</td>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">$expire</td>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">$expireon</td>
<th style=""border: 1px solid #dddddd;text-align: left;padding: 8px;"">$notify</td>
</tr>
#================== end ================================
</table>
<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"" background=""#000000"">
<tr style=""background-color: #000000;"" >
<td align=""center""><font color=""#f6f8f1"">If you have any questions or issues, please contact us</font><br/>
<font color=""#f6f8f1"">IT Support</font>
</td>
</tr>
<tr style=""background-color: #000000;"" >
<td align=""center"" style=""padding: 20px 0 0 0;"">
<table border=""0"" cellspacing=""0"" cellpadding=""0"">
<tr>
<td width=""37"" style=""text-align: center; padding: 0 10px 0 10px;"">
<a href=""http:///"">
<img src=""https://s6.postimg.org/5rut/xs_blue_200.png"" width=""50"" height=""37"" alt=""AXS"" border=""0"" />
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
"
# Send Email Message
#Send-Mailmessage -smtpServer $smtpServer -credential $creds -from $from -to $email -subject $subject -body $body -bodyasHTML -usessl -port 587 -priority High
Write-Host "Notificacion enviada a" $name
}
# End Send Message
The current code overwrites the Body field (and sends an email) on every iteration of the foreach loop... Basicly, what you are doing now is for every user : add the current user entry in the Body and mail this (and this happens for every entry in your input file). I also don't understand the "manual" construction of the HTML : Powershell has a native CMDlet for exporting to HTML, ConvertTo-HTML (you can use the -fragement parameter on this one to only output the - adding custom formatting and styling can be done with CSS instead of applying them in-line)

I cannot apply CSS in html table

I cannot apply the CSS in the table. For example I want the table to be displayed in the middle and to change the font color and size as well. But I can't. Should I use the tbody tag since I have the body one? Also I generate the table data from database.
<?php
include 'connect.php';
$year = $_POST['year'];
$lecturer = $_POST['lecturer'];
$years = array(
2005,
2006,
2007
);
$lecturers = array(
'lec1',
'lec2',
'lec3',
'lec4'
);
if (in_array($lecturer, $lecturers) && in_array($year, $years)) {
$sql = "SELECT unit_name,a1,a2,a3,l1,l2,r1,r2,u1,u2,u3 FROM $lecturer WHERE year=$year";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="mytable">
<table width="900" border="1" cellspacing="1">
<tr>
<tbody>
<td>Unit Name</td>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tbody>
</tr>
<?php
while($unit=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$unit['unit_name']."</td>";
echo "<td>".$unit['a1']."</td>";
echo "<td>".$unit['a2']."</td>";
echo "<td>".$unit['a3']."</td>";
echo "<td>".$unit['l1']."</td>";
echo "<td>".$unit['l2']."</td>";
echo "<td>".$unit['r1']."</td>";
echo "<td>".$unit['r2']."</td>";
echo "<td>".$unit['u1']."</td>";
echo "<td>".$unit['u2']."</td>";
echo "<td>".$unit['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
css:
<!-- principalLecturers
-->
#body{
color:white;
padding-bottom: 20px;
}
#table{
margin:0;
border-collapse: collapse;
color:#b50a1e;
font-family:verdana,arial,sans-serif;
font-size:10px;
}
#table#mytable{
display: table-row-group;
vertical-align: middle;
border-color: inherit
}
I can't see anywhere in your table <table id="table"...>. That's why the styles for #table {...} does not apply.