VMware Cloud Community
jamie20
Enthusiast
Enthusiast
Jump to solution

Host Alarms as mail

Hi guys,

I am trying to get alarm events of host as mail report , but ends with a error, Below is the script what I tried:

#Vcenter Info
Connect-VIServer -server 10.0.0.1 -User vcadmin -Password welcome
$esx = Get-VMHost
$host = Get-VIEvent -Entity $esx -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) | Where-Object {($_.FullFormattedMessage -like "Alarm*")}

# Create column
$HtmlTable1 = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #FFFFFF'>
<th align=left><b>Time</b></th>
<th align=left><b>Alert</b></th>
</tr>"

# Insert data
foreach ($row in $host)
{
$HtmlTable1 += "<tr style='font-size:13px;background-color:#FFFFFF'>
<td>" + $row.CreatedTime + "</td>
<td>" + $row.FullFormattedMessage + "</td>
</tr>"
}
$HtmlTable1 += "</table>"

Disconnect-viserver

# Send Mail Inputs
$smtpserver = "***************"
$from = "***************"
$to = "***************"
$subject = "Alerts"
$body = "Host Alert:<br/><br/>" + $HtmlTable1

Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml

#End

 

The error I get is

jamie20_0-1616415396761.png

 

What I am missing? Any help?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The variable $host is an automatic PowerShell variable.
You can't use that, pick another valid variable name instead of $host


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

View solution in original post

1 Reply
LucD
Leadership
Leadership
Jump to solution

The variable $host is an automatic PowerShell variable.
You can't use that, pick another valid variable name instead of $host


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