View Only
  • 1.  Host Alarms as mail

    Posted Mar 22, 2021 12:18 PM

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

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


    # 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



    The error I get is



    What I am missing? Any help?

  • 2.  RE: Host Alarms as mail
    Best Answer

    Posted Mar 22, 2021 12:36 PM

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