Hi All,
After getting the code right i am now having some issues with formatting in HTML, i have a couple of probably very simple issues and one maybe not so simple. Below is my code and attached is a screenshot, what basically happens is a list of servers are reported against if they exceed the specified resources the cells turn red, however since adding the additional cell coloring the format of the cells has gone all funny - not sure if this is because some of the guests in the text file dont exist yet..
1. I need to a line break after the First statement, i tried "`n" and <br/> but neither work
2. I also need to change the format of the title to be bold when i have added HTML formatting it doesn't work for me anyway.
3. Lastly the funny cell formats shown in the attached output only occurred once i added the elements to change the color.
Thanks in advance, i probably just need to walk away for a while, script needs tidying up as well.
#Start of Script
$Result = @()
foreach ($vm in Get-VM -Name $vmName)
{
$Result += New-Object PSObject -Property @{
ServerName = $vm.Name
VMHost = $vm.VMHost.Name
Powerstate = $vm.PowerState
CPU = $vm.NumCpu
Mem = $vm.MemoryGB
UsedGB = [math]::Round($vm.ProvisionedSpaceGB, 0)
}
}
$Result += New-Object PSObject -Property @{
ServerName = 'Totals'
VMHost = ''
Powerstate = ''
CPU = ($result | Measure-Object -Property CPU -Sum).Sum
Mem = ($result | Measure-Object -Property Mem -Sum).Sum
UsedGB = ($result | Measure-Object -Property UsedGB -Sum).Sum
}
#Start of html formatting
if($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>VMHost</B></TD>
<TH><B>Power State</B></TD>
<TH><B>CPUs</B></TD>
<TH><B>Memory GB</B></TD>
<TH><B>Used Space</B></TH>
</TR>"
Foreach($Entry in $Result){
if($Entry.Mem -gt $agreedMem)
{
$HTML += "<TR bgcolor=Red>"
}else
{
$HTML += "<TR>"
}
if($Entry.CPU -gt $agreedCPU)
{
$HTML += "<TR bgcolor=Red>"
}else
{
$HTML += "<TR>"
}
if($Entry.UsedGB -gt $agreedStr)
{
$HTML += "<TR bgcolor=Red>"
}else
{
$HTML += "<TR>"
}
$HTML += "
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.VMHost)</TD>
<TD>$($Entry.PowerState)</TD>
<TD>$($Entry.CPU)</TD>
<TD>$($Entry.Mem)</TD>
<TD>$($Entry.UsedGB)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputReport
}
#Start of email formatting
$body = write-output $header
$body += "`n write-output $header2 "
$body += [System.IO.File]::ReadAllText($OutputReport)
Send-MailMessage -To $to -From $from -Subject "Daily Checks - Dynamics Resource report" -Body $body -BodyAsHtml -SmtpServer $smtp
start-sleep -s 30
Remove-Item -Path $OutputReport -Force
I moved the creation of the header into the HTML creation, then you can use the HTML H1 tag to get the header in bold.
$Result = @()
foreach ($name in $vmName)
{
$vm = Get-VM -Name $name -ErrorAction SilentlyContinue
$Result += New-Object PSObject -Property @{
ServerName = $name
VMHost = $vm.VMHost.Name
Powerstate = $vm.PowerState
CPU = $vm.NumCpu
Mem = $vm.MemoryGB
UsedGB = [math]::Round($vm.ProvisionedSpaceGB, 0)
}
}
$Result += New-Object PSObject -Property @{
ServerName = 'Totals'
VMHost = ''
Powerstate = ''
CPU = ($result | Measure-Object -Property CPU -Sum).Sum
Mem = ($result | Measure-Object -Property Mem -Sum).Sum
UsedGB = ($result | Measure-Object -Property UsedGB -Sum).Sum
}
#Start of html formatting
if ($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += ""
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<header>
<h1>$header</h1>
</header>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>VMHost</B></TD>
<TH><B>Power State</B></TD>
<TH><B>CPUs</B></TD>
<TH><B>Memory GB</B></TD>
<TH><B>Used Space</B></TH>
</TR>"
Foreach ($Entry in $Result)
{
if ($Entry.Mem -gt $agreedMem)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
if ($Entry.CPU -gt $agreedCPU)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
if ($Entry.UsedGB -gt $agreedStr)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
$HTML += "
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.VMHost)</TD>
<TD>$($Entry.PowerState)</TD>
<TD>$($Entry.CPU)</TD>
<TD>$($Entry.Mem)</TD>
<TD>$($Entry.UsedGB)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputReport
}
#Start of email formatting
$body = [System.IO.File]::ReadAllText($OutputReport)
Send-MailMessage -To $to -From $from -Subject "Daily Checks - Dynamics Resource report" -Body $body -BodyAsHtml -SmtpServer $smtp
Start-Sleep -s 30
Remove-Item -Path $OutputReport -Force
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Let's first start with the HTML output, and skip the mail output for now.
If you also want VM names in there for VMs that do not yet exist, the code needs to be adapted.
The column headers are in bold, not sure what you mean?
$Result = @()
foreach ($name in $vmName)
{
$vm = Get-VM -Name $name -ErrorAction SilentlyContinue
$Result += New-Object PSObject -Property @{
ServerName = $name
VMHost = $vm.VMHost.Name
Powerstate = $vm.PowerState
CPU = $vm.NumCpu
Mem = $vm.MemoryGB
UsedGB = [math]::Round($vm.ProvisionedSpaceGB, 0)
}
}
$Result += New-Object PSObject -Property @{
ServerName = 'Totals'
VMHost = ''
Powerstate = ''
CPU = ($result | Measure-Object -Property CPU -Sum).Sum
Mem = ($result | Measure-Object -Property Mem -Sum).Sum
UsedGB = ($result | Measure-Object -Property UsedGB -Sum).Sum
}
#Start of html formatting
if ($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>VMHost</B></TD>
<TH><B>Power State</B></TD>
<TH><B>CPUs</B></TD>
<TH><B>Memory GB</B></TD>
<TH><B>Used Space</B></TH>
</TR>"
Foreach ($Entry in $Result)
{
if ($Entry.Mem -gt $agreedMem)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
if ($Entry.CPU -gt $agreedCPU)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
if ($Entry.UsedGB -gt $agreedStr)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
$HTML += "
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.VMHost)</TD>
<TD>$($Entry.PowerState)</TD>
<TD>$($Entry.CPU)</TD>
<TD>$($Entry.Mem)</TD>
<TD>$($Entry.UsedGB)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputReport
}
Invoke-Item -Path $OutputReport
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
Yeh if i run the report using your changes below the output in HTML is fine it highlights the cells and the cell format is neat and tidy, as soon as i add the email element back in it goes back to a nasty table..
With regards to the text, its not the column headers but just the tittle i.e. "The agreed resources are...." I want to play with this font in bold and put a line break beneath it before the table, but when i use `n i get html errors.
Thanks again.
I moved the creation of the header into the HTML creation, then you can use the HTML H1 tag to get the header in bold.
$Result = @()
foreach ($name in $vmName)
{
$vm = Get-VM -Name $name -ErrorAction SilentlyContinue
$Result += New-Object PSObject -Property @{
ServerName = $name
VMHost = $vm.VMHost.Name
Powerstate = $vm.PowerState
CPU = $vm.NumCpu
Mem = $vm.MemoryGB
UsedGB = [math]::Round($vm.ProvisionedSpaceGB, 0)
}
}
$Result += New-Object PSObject -Property @{
ServerName = 'Totals'
VMHost = ''
Powerstate = ''
CPU = ($result | Measure-Object -Property CPU -Sum).Sum
Mem = ($result | Measure-Object -Property Mem -Sum).Sum
UsedGB = ($result | Measure-Object -Property UsedGB -Sum).Sum
}
#Start of html formatting
if ($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML += ""
$HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<header>
<h1>$header</h1>
</header>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>VMHost</B></TD>
<TH><B>Power State</B></TD>
<TH><B>CPUs</B></TD>
<TH><B>Memory GB</B></TD>
<TH><B>Used Space</B></TH>
</TR>"
Foreach ($Entry in $Result)
{
if ($Entry.Mem -gt $agreedMem)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
if ($Entry.CPU -gt $agreedCPU)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
if ($Entry.UsedGB -gt $agreedStr)
{
$HTML += "<TR bgcolor=Red>"
}
else
{
$HTML += "<TR>"
}
$HTML += "
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.VMHost)</TD>
<TD>$($Entry.PowerState)</TD>
<TD>$($Entry.CPU)</TD>
<TD>$($Entry.Mem)</TD>
<TD>$($Entry.UsedGB)</TD>
</TR>"
}
$HTML += "</Table></BODY></HTML>"
$HTML | Out-File $OutputReport
}
#Start of email formatting
$body = [System.IO.File]::ReadAllText($OutputReport)
Send-MailMessage -To $to -From $from -Subject "Daily Checks - Dynamics Resource report" -Body $body -BodyAsHtml -SmtpServer $smtp
Start-Sleep -s 30
Remove-Item -Path $OutputReport -Force
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
That has now worked a treat as a HTML report, it looks pants when i add it as the body of an email the header becomes skewed with the table. But thats not an issue as i have added the report as an attachment to the email.
Thanks a lot for all your help as always and your prompt response.