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
Apologies, there was a typo in the bit of script I provided...
$messagebody += ($vm.VM.Name + "`t" + $vm.SizeMB + "`n")
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"
}
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()
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.
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
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.
Apologies, there was a typo in the bit of script I provided...
$messagebody += ($vm.VM.Name + "`t" + $vm.SizeMB + "`n")
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.
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)
}
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
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
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
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
}
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
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
I use the script in this thread ...
any suggestions?
regards
fred
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
Thanks again LucD
works fine!
regards
Fred