How to create an HTML format email from ForEach

Hi guys,

I need to create an HTML format email from this script, is it possible?

foreach ($snap in Get-VM | Get-Snapshot)
{$snapevent = Get-VIEvent -Entity $snap.VM -Types Info -Finish $snap.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'}
if ($snapevent -ne $null){Write-Host ( "VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + " by " + $snapevent.UserName +".")}
else {Write-Host ("VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + ". This event is not in vCenter events database")}}
M
September 7th, 2015 10:51am

Have you checked out ConvertTo-Html
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 11:29am

Yes, but don't know how to incorporate on the script... 
September 7th, 2015 11:54am

Yes, but don't know how to incorporate on the scrip
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 11:55am

That's not a nice answer. Isn't this a forum? What is the point of that answer really....
September 7th, 2015 12:10pm

That's not a nice answer. Isn't this a forum? What is the point of that answer
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 12:22pm

Here is a starter.

This is what your code look like when some small attempt is made to make it readable:

Get-VM | 
    Get-Snapshot |
    ForEach-Object{
        $snapEvent=Get-VIEvent -Entity $_.VM -Types Info -Finish $_.Created -MaxSamples 1 | 
             Where-Object{$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'}0
        if($snapevent){
            Write-Host ( "VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + " by " + $snapevent.UserName +".")
        }else{
            Write-Host ("VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + ". This event is not in vCenter events database")
        }
    }

To start with you need to output objects in order to generate HTML.  Take all of you write statements and convert them to custom objects. then you can use Convert.

Part of you problem is in assuming that what you have can actually easily be turned into HTML.  If you use this as a learning tool you will understand why your initial post was not just a simple question.  It shows that you do not know the basics.  You must make the effort to learn the basics on your own.  You can ask questions about things you do not understand.

You are also going to have to fix the logic and design errors in your initial script before you can generate objects.

September 7th, 2015 12:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics