Automation

 View Only
  • 1.  How to output the result of PowerCLI command in an email ?

    Posted Apr 30, 2015 12:22 PM


    Hi Everyone,

    Can someone provide me help with this example. I'm starting to work with PowerCLI and I have difficulty to output my result in an email.

    How can I store my result in array and import the data in HTML in an email ?

    From the example below, I don't see any result in the email from the powercli command " Get-VM | Select-Object Name, NumCPU, MemoryMB, ProvisionedSpaceGB"

    Do I need to create a loop to extract the result ?

    #-------------------------CHANGE THESE VALUES--------------------------------
    $SMTPServer = "SMTP.toto.com"
    $vCenterServerName1 = "vsphere1.toto.com"
    $ToAddress = lady@toto.com
    #-----------------------------------------------------------------------------

    $Title = "Connection settings for vCenter"
    $Author = "Lady"
    $Header =  "Connection Settings"

    $Login = account@toto.com# for LAFAM domain
    $Password = "xxxxxxxxxx#

    add-pssnapin VMware.VimAutomation.Core

    $HTMLHeader = "<HTML><TITLE> VMware report </TITLE>"
    $HTMLFooter = "Made with POWERSHELL Script 2.0 - Version of the script " + $ScriptVersion + "</HTML>"

    $ScriptVersion = "v1.0"
    $Subject = "VMware Report - VM Inventory "
    $FromAddress = "VMware" + "@toto.com"

    $ColorArray = "Red", "Orange", "Purple", "Blue", "Olive", "SlateGrey", "Orange", "Purple", "Blue", "Olive"
    $ColorArrayIndex = 0

    $HTMLHeader = "<HTML><TITLE> VMware Report for " + $vCenterServerName1 " </TITLE>"
    $HTMLFooter = "Made with POWERCLI Script 5.0 - " +  $Author + ".<br><br>"
    $HTMLFooter += "<B>NOTE:</B> $Date </HTML>"


    $HTMLBody = "<BODY>"

    $vCenter_inventory = @(Get-VM | Select-Object Name, NumCPU, MemoryMB, ProvisionedSpaceGB)

    ################################### OUTPUT #######################################

    # HEADER
    $HTMLBody += CreateHeader ("REPORT FOR <font color=Blue><b> vCENTER " + $vCenterServerName1 + "</font></b><br>")

    # INTRO TEXT

    $HTMLBody += "<br><br>"
    $HTMLBody += $vCenter_inventory


    ########################### SEND REPORT BY E-MAIL #################################
    $HTMLBody += "</BODY>" # Close HTML Body
    $HTMLPage = $HTMLHeader + $HTMLBody + $HTMLFooter
    Send-Mailmessage -From $FromAddress -To $ToAddress -Subject $Subject -BodyAsHTML -Body $HTMLPage -Priority Normal -SmtpServer $SMTPServer
    Write-Host "Report has been sent by E-mail to " $ToAddress " from " $FromAddress
    ################################### CLEANUP #######################################

    $HTMLHeader = ""
    $HTMLBody = ""
    $HTMLFooter = ""
    $HTMLPage = ""
    $vCenter1_Inventory = ""
    $vCenterServerName1 = ""



  • 2.  RE: How to output the result of PowerCLI command in an email ?

    Posted Apr 30, 2015 12:55 PM

    Send-MailMessage doesn't like arrays. There are two ways you can go about this that I know of:

    1. Pipe your body to "Out-String":

    $HTMLPage = $HTMLPage | Out-String

    The lazy method but might not become the prettiest output though.


    2. Don't use an array (+=), make sure it is still a string:

    $HTMLBody = "<HTML><TITLE> VMware Report for " + $vCenterServerName1 " </TITLE>" + "<BODY>" + "REPORT FOR <font color=Blue><b> vCENTER " + $vCenterServerName1 + "</font></b><br>" + "<br><br>" + $vCenter_inventory + "Made with POWERCLI Script 5.0 - " +  $Author + ".<br><br>" + "<B>NOTE:</B> $Date </HTML>"



  • 3.  RE: How to output the result of PowerCLI command in an email ?

    Posted Apr 30, 2015 07:37 PM

    LittleNickey

    LittleNickey

    Thanks for your answer. The first solution works but like you mention the output is messy.

    For the second solution, same if I remove the array, i was not able to see the result in the email.



  • 4.  RE: How to output the result of PowerCLI command in an email ?
    Best Answer

    Posted May 03, 2015 08:56 AM

    Alright, cutting some of your code, this works for me anyway:

    ## This sets the format for the HTML table

    $Header = @"

    <style>

    body{

    background-color:#FFFFFF;

    font-family:Arial;

    font-size:12pt;

    }

    td, th{

    border:1px solid black;

    border-collapse:collapse;

    }

    th{

    color:White;

    background-color:DodgerBlue;

    }

    table, tr, td, th{

    padding: 5px;

    margin: 0px;

    }

    table{

    margin-left:50px;

    }

    </style>

    <H2>TITLE</H2>

    <br>

    "@

    ## Collect data

    $vCenter_inventory = Get-VM | Select-Object Name, NumCPU, MemoryMB, ProvisionedSpaceGB


    ## Convert to HTML

    $HTML = $vCenter_inventory | ConvertTo-HTML -Fragment

    ## Prepare Mail Message

    $Message = "Some random text."

    $Body = $Header + "<body>" + $Message + "<br><br>" + $HTML + "</body></table>"

    $smtpServer = "smtpserver"

    $HostName = $env:COMPUTERNAME

    $From = $HostName + "@littlenickey.se"

    $To = "me@littlenickey.se"

    ## Send Mail Message

    Send-MailMessage -SmtpServer $smtpServer -From $From -To $To -Subject $Subject -Body $Body -BodyAsHtml

    This should give you a bit nicer output in the form of a table.



  • 5.  RE: How to output the result of PowerCLI command in an email ?

    Posted May 04, 2015 02:02 AM

    Thanks for your help. It's works.