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"
=============================
in email am receiving without data
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.
$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
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"
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"
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.
$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
Lucd, i received output like this
I updated the code above, try like that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Try like this
$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
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
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
Hi LucD,
It was my mistake. It's fixed now.
Thanks
vmk