drivera01
Enthusiast
Enthusiast

report for multiple vms that are created

Jump to solution

I have a  vm creation script that I modified to be able to be able to run in a loop using a CSV file to create more than one VM at a time. My initial script would send me an email stating for example:

VM

IPaddress

CPU

Memory

ToolsVer

ToolStatus

DataStore

Folder

ESXiHost

myvm

123.45.67.89

3

4096

8384

toolsOk

myds

my-folder

myesxhost

The problem I want to try and figure out is how can I get ALL tthe VM's to report on one single email report. Right now since it is looping, every VM I create will send out a report. So if I set it up to build 4 vm's, it will send 4 emails.. I don't like that.

How my script works pretty much...

foreach($var in $vmlist){

script here

...

...

...


######################## Create and Send Email Report - Start ###############################################
#
Function Create-HTMLTable {
  param([array]$Array)
  $arrHTML = $Array | ConvertTo-Html
  $arrHTML[-1] = $arrHTML[-1].ToString().Replace(‘</body></html>’,"")
  Return $arrHTML[5..2000]
}

$output = @()
$output += '<html><head></head><body><style>table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:100%;}th{text-align:left;}td{background-color:#fff;width:20%;border-style:solid;border-width:1px;}body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}</style>'
$output += ‘<H2>The vms that were created</H2>'
$output += ‘<H2> </H2>'
#$Report = @()
$Report = New-Object -TypeName system.collections.arraylist
  foreach ($vm in get-vm -Name $var.myvms )
   {
...

...

...

...

...
  $report.add($reportedvm)|out-null

}


if ($HTML -eq "yes") {
  $output += '<p>'
  $output += '<p>'
  $output += Create-HTMLTable $report  $output += '</p>'
  $output += '</body></html>'
  $output | Add-Content $FileHTML
}
if ($SendEmail -eq "yes") {
$body = Get-Content $FileHTML | out-String
Send-MailMessage –From $EmailFrom –To $EmailTo –Subject $EmailSubject –SmtpServer $EmailSMTP -Body $body -BodyAsHtml }
#

}

0 Kudos
1 Solution

Accepted Solutions
drivera01
Enthusiast
Enthusiast

ok, i'll have to skip this for now, I can not get it to work.

thanks

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership

Try something along these lines

Function Create-HTMLTable {

    param([array]$Array)

    $arrHTML = $Array | ConvertTo-Html

    $arrHTML[-1] = $arrHTML[-1].ToString().Replace(‘</body></html>’,"")

    Return $arrHTML[5..2000]

}

$output = @()

$output += '<html><head></head><body><style>table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:100%;}th{text-align:left;}td{background-color:#fff;width:20%;border-style:solid;border-width:1px;}body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}</style>'

$output += ‘<H2>The vms that were created</H2>'

$output += ‘<H2> </H2>'

foreach($var in $vmlist){

    # script here

    ######################## Create and Send Email Report - Start ###############################################

    #

    #$Report = @()

    $Report = New-Object -TypeName system.collections.arraylist

    foreach ($vm in get-vm -Name $var.myvms )

    {

        #

        $report.add($reportedvm)|out-null

    }

    #

}

if ($HTML -eq "yes") {

    $output += '<p>'

    $output += '<p>'

    $output += Create-HTMLTable $report $output += '</p>'

    $output += '</body></html>'

    $output | Add-Content $FileHTML

}

if ($SendEmail -eq "yes") {

    $body = Get-Content $FileHTML | out-String

    Send-MailMessage –From $EmailFrom –To $EmailTo –Subject $EmailSubject –SmtpServer $EmailSMTP -Body $body -BodyAsHtml

}


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

drivera01
Enthusiast
Enthusiast

LucD,

I'm having a problem seeing any difference than what my original script is?

I think I did what you suggested but it still sends reports one by one.

0 Kudos
LucD
Leadership
Leadership

The location of the ForEach loop is different.

In my version the creation of the HTML header and the call to Create-HTMLTable is done outside the ForEach loop.

That way you will get 1 HTML header and 1 table with the content from the $report array.


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

drivera01
Enthusiast
Enthusiast

ok, i'll have to skip this for now, I can not get it to work.

thanks

0 Kudos