VijayKumarMyada
Enthusiast
Enthusiast

Diskconsolidation send email as HTML is blank

Jump to solution

Hello !

am trying to gathers list of vms in a vc and send an email as HTML, i see script is executing but when it is sending report is blank.

help me in correcting this.

# Get date in UK format day/month/year

$date = Get-Date -Format dd/MM/yy

$Header = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>

"@

Connect-VIServer -Server vc -User -u admin -p admin

$Report = Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | Select Name, PowerState, UsedSpaceGB | ConvertTo-HTML -Head $Header

# Send email message

Send-mailmessage -to "xyz@abc.com" -from "zyx@abc.com" -subject "Disk consolidation needed" -body "Please find below vms requried disk consolidation , take necessary action." -BodyAsHtml -SmtpServer "10.1.2.3"

=============================

pastedImage_5.png

in email am receiving without data

pastedImage_6.png

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The Body parameter of the Send-MailMessage cmdlet, expects a single string, not an array of strings.

You need to have an Out-String after the ConvertTo-Html.

Something like this.

I use splatting for the Send-MailMessage cmdlet, this to not have a long line.

# Get date in UK format day/month/year

$date = Get-Date -Format dd/MM/yy

$Header = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>

"@


Connect-VIServer -Server vc -User -u admin -p admin

$Report = Get-VM |

Where-Object { $_.Extensiondata.Runtime.ConsolidationNeeded } |

Select Name, PowerState, UsedSpaceGB |

ConvertTo-HTML -Head $Header | Out-String

# Send email message


$sMail = @{

   To = "xyz@abc.com"

   From = "xyz@abc.com"

   Subject = "Disk consolidation needed"

   Body = $report

   BodyAsHtml = $true

   SmtpServer = "10.1.2.3"

}


Send-mailmessage @sMail

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
10 Replies
RvdNieuwendijk
Leadership
Leadership

You should use $Report as the value of the Send-Mailmessage Body parameter:

Send-mailmessage -to "xyz@abc.com" -from "zyx@abc.com" -subject "Disk consolidation needed" -body $Report -BodyAsHtml -SmtpServer "10.1.2.3"

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
ganapa2000
Enthusiast
Enthusiast

try like this,

# Get date in UK format day/month/year

$date = Get-Date -Format dd/MM/yy

$Header = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>

"@

Connect-VIServer -Server navc -User -u admin -p admin

$body += "Please find below vms requried disk consolidation , take necessary action. <br>"

$body += Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | Select Name, PowerState, UsedSpaceGB | ConvertTo-HTML -Head $Header

# Send email message

Send-mailmessage -to "xyz@abc.com" -from "zyx@abc.com" -subject "Disk consolidation needed" -body $body -BodyAsHtml -SmtpServer "10.1.2.3"

0 Kudos
LucD
Leadership
Leadership

The Body parameter of the Send-MailMessage cmdlet, expects a single string, not an array of strings.

You need to have an Out-String after the ConvertTo-Html.

Something like this.

I use splatting for the Send-MailMessage cmdlet, this to not have a long line.

# Get date in UK format day/month/year

$date = Get-Date -Format dd/MM/yy

$Header = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>

"@


Connect-VIServer -Server vc -User -u admin -p admin

$Report = Get-VM |

Where-Object { $_.Extensiondata.Runtime.ConsolidationNeeded } |

Select Name, PowerState, UsedSpaceGB |

ConvertTo-HTML -Head $Header | Out-String

# Send email message


$sMail = @{

   To = "xyz@abc.com"

   From = "xyz@abc.com"

   Subject = "Disk consolidation needed"

   Body = $report

   BodyAsHtml = $true

   SmtpServer = "10.1.2.3"

}


Send-mailmessage @sMail

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
VijayKumarMyada
Enthusiast
Enthusiast

Lucd, i received output like this

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

I updated the code above, try like that.


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

0 Kudos
VijayKumarMyada
Enthusiast
Enthusiast

in output if no vms are applicable email body should contain no vms consolidation is required, this is missing in the script, how do we can achieve that by adding else

0 Kudos
LucD
Leadership
Leadership

Try like this

# Get date in UK format day/month/year

$date = Get-Date -Format dd/MM/yy

$Header = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>

"@


#Connect-VIServer -Server vc -User -u admin -p admin

$report = Get-VM |

   Where-Object { $_.Extensiondata.Runtime.ConsolidationNeeded } |

  Select Name, PowerState, UsedSpaceGB

if($report -ne $null){

   $report = $report | ConvertTo-HTML -Head $Header | Out-String

}

else{

   $report = 'No VMs disk consolidation is required'

}


# Send email message

$sMail = @{

   To = "xyz@abc.com"

   From = "xyz@abc.com"

   Subject = "Disk consolidation needed"

   Body = $report

   BodyAsHtml = $true

   SmtpServer = "10.1.2.3"

}


Send-mailmessage @sMail


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

0 Kudos
vmk2014
Expert
Expert

LucD,

Can you help me here to fix below error

Send-mailmessage : The specified string is not in the form required for an e-mail address.

At D:\nilay\Disk-consolidation.ps1:59 char:1

+ Send-mailmessage @sMail

+ ~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidType: (:) [Send-MailMessage], FormatException

    + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage

Send-mailmessage : A recipient must be specified.

At D:\nilay\Disk-consolidation.ps1:59 char:1

+ Send-mailmessage @sMail

+ ~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], InvalidOpe

   rationException

    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.SendMailMessage

Thanks

vmk

0 Kudos
LucD
Leadership
Leadership

I would need to see the value you provided on the To and From parameter.
It should be of the format user@domain.


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

0 Kudos
vmk2014
Expert
Expert

Hi LucD,

It was my mistake. It's fixed now.

Thanks

vmk

0 Kudos