VMware Cloud Community
sathu0206
Contributor
Contributor

How to convert to HTML and send email from script - VCenter current resource space details.

Hello Team,

One of our Vmware community members, last time he shared one script to pull the VCenter resource availability details. 

I need to convert it to HTML format and how to set automated email for every day along with the VCenter name. Please help with this. I am new to powershell/cli script creations.

 

$Totals = [PSCustomObject]@{NumCpu=0
CpuUsageGhz=0
CpuFreeGhz=0
CpuTotalGhz=0
MemoryUsageGB=0
memoryFreeGB=0
MemoryTotalGB=0
StorageUsageTB=0
StorageFreeTB=0
StorageTotalTB=0}

foreach ($Vmhost in (Get-VMHost | where {$_.PowerState -eq "PoweredOn"})){
$Totals.NumCpu+=$Vmhost.NumCpu
$Totals.CpuUsageGhz+=[Math]::Round($Vmhost.CpuUsageMhz/1000,2)
$Totals.CpuFreeGhz+=[Math]::Round(($Vmhost.CpuTotalMhz - $Vmhost.CpuUsageMhz)/1000,2)
$Totals.CpuTotalGhz+=[Math]::Round($Vmhost.CpuTotalMhz/1000,2)
$Totals.MemoryUsageGB+=[Math]::Round($Vmhost.MemoryUsageGB,2)
$Totals.memoryFreeGB+=[Math]::Round(($Vmhost.MemoryTotalGB - $Vmhost.MemoryUsageGB),2)
$Totals.MemoryTotalGB+=[Math]::Round($Vmhost.MemoryTotalGB,2)}

foreach ($Datastore in Get-Datastore){
$Totals.StorageUsageTB+=[Math]::Round(($Datastore.CapacityGB - $Datastore.FreeSpaceGB)/1024,2)
$Totals.StorageFreeTB+=[Math]::Round($Datastore.FreeSpaceGB/1024,2)
$Totals.StorageTotalTB+=[Math]::Round($Datastore.CapacityGB/1024,2)}

$Totals

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership

I think in a previous reply to one fo your threads, to which you never bothered to reply, I already showed you how to do that.
See Re: Virtual machines drive space details - How to ... - VMware Technology Network VMTN


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sathu0206
Contributor
Contributor

Hi LucD

Sorry for the delay, last script is working for me, and accepted the answer.

But this request is different we need to get the overall data center available CPU, Memory, and Storage details, I have the script, but not sure how to convert it to HTML and send email. 

Reply
0 Kudos
LucD
Leadership
Leadership

I'm not sure how this request is different.
You take your result (in $totals), pass it through ConvertTo-Html and place the results in the Body of the email (just like in the other thread).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sathu0206
Contributor
Contributor

I tried to modify it, but I am unable to complete the script execution. I think I missed completing the script. 

Please advise me on how to complete and get the script execution completed.  Attached screenshot of FYR.

Reply
0 Kudos
LucD
Leadership
Leadership

Your original snippet is already incorrect.

You define 1 PSObject and then you loop over all ESXi nodes, and each time overwrite the values in the properties.
Then you do the same with the Datastores.

First, you will have to define how your output has to look.
I assume you want a line per ESXi node?

Then what happens if an ESXi node has more than 1 datastore?
How do you want to display that in a single row?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sathu0206
Contributor
Contributor

Yes, @LucD we have three data centers, I need all three data centers' overall CPU, Memory, and Storage details.

I need complete VCenter details, not the specific hosts or clusters. 

Reply
0 Kudos
LucD
Leadership
Leadership

That is not what I asked.
How do you envisage getting all the information (per ESXi node and per Datastore) on 1 line?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sathu0206
Contributor
Contributor

Sorry for the delay @LucD 

I pointed out the vcenter name and pulled the information for each host and datastore in the script, now the current situation is i updated the script and am able to get the output information, but it's not working from the task scheduler. When i directly execute from the PowerShell or Powershell ISE it's working, but when I execute from the task scheduler it is completed successfully, I received the email, but the value for all is zero. 

 

 

$vcenter="########"

Connect-VIServer -Server $vcenter -Protocol https -User #### -Password ####


$Totals = [PSCustomObject]@{NumCpu=0

CpuUsageGhz=0

CpuFreeGhz=0

CpuTotalGhz=0

MemoryUsageGB=0

MemoryFreeGB=0

MemoryTotalGB=0

StorageUsageTB=0

StorageFreeTB=0

StorageTotalTB=0}

foreach ($Vmhost in (Get-VMHost | where {$_.PowerState -eq "PoweredOn"})){
$Totals.NumCpu+=$Vmhost.NumCpu
$Totals.CpuUsageGhz+=[Math]::Round($Vmhost.CpuUsageMhz/1000,2)
$Totals.CpuFreeGhz+=[Math]::Round(($Vmhost.CpuTotalMhz - $Vmhost.CpuUsageMhz)/1000,2)
$Totals.CpuTotalGhz+=[Math]::Round($Vmhost.CpuTotalMhz/1000,2)
$Totals.MemoryUsageGB+=[Math]::Round($Vmhost.MemoryUsageGB,2)
$Totals.memoryFreeGB+=[Math]::Round(($Vmhost.MemoryTotalGB - $Vmhost.MemoryUsageGB),2)
$Totals.MemoryTotalGB+=[Math]::Round($Vmhost.MemoryTotalGB,2)}

foreach ($Datastore in Get-Datastore){
$Totals.StorageUsageTB+=[Math]::Round(($Datastore.CapacityGB - $Datastore.FreeSpaceGB)/1024,2)
$Totals.StorageFreeTB+=[Math]::Round($Datastore.FreeSpaceGB/1024,2)
$Totals.StorageTotalTB+=[Math]::Round($Datastore.CapacityGB/1024,2)}

$outfilename = 'Datacenterspace' + $currentdate + '.html'
$outfilepath = 'C:\Temp\' + $Totals


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


$ebody = if ($Totals) { $Totals | ConvertTo-Html -Head $style | Out-String | Out-File $outfilepath }

 

#variables for email output

$eto = "mail id"
$efrom = "mail id"
$esubject = "subject"
If ($Totals) { $ebody = (Get-Content $outfilepath | Out-String) }
$eserver = "smtp address"

If ($Totals) { Send-MailMessage -From $efrom -To $eto -Subject $esubject -BodyAsHtml -Body $ebody -smtpserver $eserver }

Reply
0 Kudos