VMware Cloud Community
Retcher
Enthusiast
Enthusiast
Jump to solution

Mail with Overview of VMs with Snapshots

Hi,

I've startet to script some basic things. The commands work fine, but I've trouble to get the output of the command a mailbody.

Here is my script:

Connect-VIServer -Server "vcentername" - Protocol https -User "username" - Password "userpassword"

$snap = Get-VM | Get-Snapshot | Select VM, SizeMB

$smtpServer = "mymailhost"

$smtpFrom = "sendermailadress"

$smtpTo = "recipientmailadress"

$messagesubject = "Snapoverview"

$messagebody = $snap

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($smtpFrom, $smtpTo, $messagesubject, $messagebody)

I got a mail and the subject is correct but I don't have a body in the mail.

Any ideas?

Kind regards

Patrick

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
SimonStrutt
Enthusiast
Enthusiast
Jump to solution

Apologies, there was a typo in the bit of script I provided...

$messagebody += ($vm.VM.Name + "`t" + $vm.SizeMB + "`n")

"The greatest challenge to any thinker is stating the problem in a way that will allow a solution." - Bertrand Russell

View solution in original post

0 Kudos
18 Replies
SimonStrutt
Enthusiast
Enthusiast
Jump to solution

The problem will be that your $messagebody needs to a string, but $snap is an array of objects.  To convert you could do something like (in place of $messagebody = $snap)

foreach ($vm in $snap) {

     $messagebody += $vm.VM.Name + "`t" + $vm.SizeMB + "`n"

}

"The greatest challenge to any thinker is stating the problem in a way that will allow a solution." - Bertrand Russell
vlife201110141
Enthusiast
Enthusiast
Jump to solution

Smth like this mate

Add-PSsnapin VMware.VimAutomation.Core
$mailfrom = "xxx@xxx.com"
$to = "xxx@xxx.com, yyy@xxx.com"
$subject = "Snapshots PowerCLI Export"
$smtpserver = 'xxx'
Connect-VIServer -Server 'vcenter' -User 'xxx' -Password 'xxx'

$a = "<style>"
$a = $a + "BODY{background-color:#CCFFFF;}"
$a = $a + "TABLE{border-width: 20px;border-style: ridge;border-bottom-color:#3399FF;border-right-color:#3399FF;border-top-color:#336633;border-left-color:#336633;border-collapse: collapse;}"
$a = $a + "TH{border-width: 3px;padding: 5px;border-style: ridge;border-color: black;background-color:#99FFFF}"
$a = $a + "TD{border-width: 3px;padding: 3px;border-style: ridge;border-color: black;background-color:#CCFFFF;text-align:center;}"
$a = $a + "</style>"

$body = Get-VM | Get-Snapshot |
select VM, SizeMB, Powerstate, Created |
  ConvertTo-HTML -head $a -body "<H2>Snapshots PowerCLI Export</H2>"

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$msg.From = $mailfrom
foreach($mailTo in $to){
$msg.To.Add($mailto)
}
$msg.Subject = $subject
$msg.Body = $body
$msg.IsBodyHtml = $true
$smtp.send($msg)
$msg.Dispose()

mattboren
Expert
Expert
Jump to solution

Hello, Retcher-

I am all for learning how to do things, and not trying to discourage you from writing your own code here, but I wanted to mention the SnapReminder script that Virtu-Al wrote a while back, in case you haven't seen it.  It does some of the things it looks that your are intending to do, and provides a little more detail in the report, as well.  If nothing else, it's something else to look at.

0 Kudos
Retcher
Enthusiast
Enthusiast
Jump to solution

Hey guys,

thanks for your answers.

I've modifite my script and now it looks like this:

Connect-VIServer -Server "vcenter"

$snap = Get-VM | Get-Snapshot | Select VM, SizeMB

$smtpServer = "mailserver"
$smtpFrom = "senderadress"
$smtpTo = "recipientadress"
$messagesubject = "Snapoverview"
if($snap -ne "")
{
    foreach ($vm in $snap) {
     $messagebody += ($vm.VM.Name + "`t" + %vm.SizeMB + "`n")
    }
}
else
    {
        exit
    }
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom, $smtpTo, $messagesubject, $messagebody)

If I now execute the script, I get the following error:

You have to set a value-expressive on the right site of the operator "+"

+      $messagebody += ($vm.VM.Name + "`t" + <<<<  %vm.SizeMB + "`n")
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression

Kind regards

0 Kudos
Retcher
Enthusiast
Enthusiast
Jump to solution

Hello mattboren,

I've seen the script, but I would like to unterstand what happens and I'm not possible to see this in this script.

0 Kudos
SimonStrutt
Enthusiast
Enthusiast
Jump to solution

Apologies, there was a typo in the bit of script I provided...

$messagebody += ($vm.VM.Name + "`t" + $vm.SizeMB + "`n")

"The greatest challenge to any thinker is stating the problem in a way that will allow a solution." - Bertrand Russell
0 Kudos
Retcher
Enthusiast
Enthusiast
Jump to solution

Thanks that was the problem.

The mail llooks like this now:

CL-Win7 24.08

Thats exactly what I want to have. Thanks everybody for your help.

0 Kudos
Retcher
Enthusiast
Enthusiast
Jump to solution

Here is the complete working script:

Connect-VIServer -Server "vcenter"

$snap = Get-VM | Get-Snapshot | Select VM, SizeMB

$smtpServer = "mailhost"
$smtpFrom = "senderadress"
$smtpTo = "recipient"
$messagesubject = "Snapoverview"
if($snap -eq $null)
{
    exit
}
else
    {
     foreach ($vm in $snap) {
     $messagebody += ($vm.VM.Name + "`t" + $vm.SizeMB + "`n")
    }
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($smtpFrom, $smtpTo, $messagesubject, $messagebody)
    }

0 Kudos
fredlock
Contributor
Contributor
Jump to solution

Script works fine but for some kind of reason the sent list of VM´s with snaphots contains 2 times the output of:

$snap = Get-VM | Get-Snapshot | Select VM, SizeMB

instead of just 1 time

regards

fred

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that you 2 connections to your vCenter open ?

Have a look by displaying the content of $defaultVIServers.


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

0 Kudos
fredlock
Contributor
Contributor
Jump to solution

Hi  Lucd,

My PowerCli session was already closed so i could not check content of $defaultVIServers anymore...

Started new PowerCli session and ran the snapshotscript ---- works fine !

So i assume it was indeed caused by having 2 connections to my vCenter

Thank you

Fred

0 Kudos
SimonStrutt
Enthusiast
Enthusiast
Jump to solution

I tend to use the folloing in all scripts I write before the Connect-VIServer to ensure I don't have any old connections lying around, if you've any existing connections, they'll get disconnected...

if ($DefaultVIServers.Count) {
    Disconnect-VIServer -Server * -Force -Confirm:$false
}
"The greatest challenge to any thinker is stating the problem in a way that will allow a solution." - Bertrand Russell
0 Kudos
fredlock
Contributor
Contributor
Jump to solution

hmmm...   I  am running into the same issue.

Just found out that the first time I run the script the output is ok.

but...

when you run it 2nd time the output is double

and

when you run it 3rd time the output is triple

and so on

Just 1 connection to my vCenter according to $DefaultVIServers

Any suggestion?

Fred

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which script are you using ?

The one in this thread or the one on Alan's site ?


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

0 Kudos
fredlock
Contributor
Contributor
Jump to solution

I use the script in this thread ...

0 Kudos
fredlock
Contributor
Contributor
Jump to solution

any suggestions?

regards

fred

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This script does a Connect-VIServer at the beginning, but it never does a Disconnect-VIServer.

That's why you this behaviour.

Just add a Disconnect-VIserver at the end of the script.


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

0 Kudos
fredlock
Contributor
Contributor
Jump to solution

Smiley Happy Thanks again LucD

works fine!

regards

Fred

0 Kudos