VMware Cloud Community
Garethgtt
Contributor
Contributor
Jump to solution

Snapshots and Email Reporting

Hi Guys,

Located in various places including here bits of scripts for creating a script to logon to a vcenter, get a list of certain vms in a folder- format it and embed it within a mail to send out (needs to be embedded not attached)

this is what ive got so far:

Connect-VIServer Servername

$smtpSrv = "name"    
$from = "VMware@vmware.com"
$to = "user@vmware.com"
$subject = "Snapshot Report"
$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | ft -AutoSize
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$smtp = new-object Net.Mail.SMTPclient($smtpSrv)
$smtp.send($msg)

so this seems to work when ran but emails out the following to my inbox (exchange server 2010)

Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData

any help much appreciated

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try changing this line

$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | ft -AutoSize

into

$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | Out-String

Since PowerShell v2 you can send email with a cmdlet.

Your script would become

Connect-VIServer Servername

$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | Out-String
Send-MailMessage -From "VMware@vmware.com" -To "user@vmware.com" `
 
-SmtpServer "name" -Body $body `
 
-Subject "Snapshot Report"


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Try changing this line

$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | ft -AutoSize

into

$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | Out-String

Since PowerShell v2 you can send email with a cmdlet.

Your script would become

Connect-VIServer Servername

$body = Get-Folder foldername | Get-VM | Get-Snapshot | Select Name, VM, SizeMb | Out-String
Send-MailMessage -From "VMware@vmware.com" -To "user@vmware.com" `
 
-SmtpServer "name" -Body $body `
 
-Subject "Snapshot Report"


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

Reply
0 Kudos
Garethgtt
Contributor
Contributor
Jump to solution

Thanks

That works

is there any way or formatting the results?

for example I get

Name                      VM                                           SizeGB

*****                         ***********                                   ********

*************                              **************                              ********************

**                         ****                                                  ******

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is there no data in the table, or did you remove that ?

Could it be that there are no snapshots on the VMs in that folder ?

I do get the info in the email body

snap.png


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

Reply
0 Kudos
Garethgtt
Contributor
Contributor
Jump to solution

Hey this is what I get mailed

none of the columns line up - this is using the original code not the second powershell 2 example you suggested

layout.jpg

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I get a similar result when I use that old method of sending email.

Can't you use the Send-MailMessage cmdlet ?


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

Reply
0 Kudos
Garethgtt
Contributor
Contributor
Jump to solution

get the same output using the cmdlet version of code too Smiley Sad

Reply
0 Kudos
HingeThunder
Contributor
Contributor
Jump to solution

Sorry I am so late to the discussion, I have written the following piece of code for emailing snapshot info and datastore status in formatted tables by outputting in html.

It certainly works for our needs and is run as a daily scheduled task on the vCenter. If you want to give it a try, only need to change a few variables, $smtpserver, $from, $to. Might not be quite what you are after but it should get you down the right path.

dailyreport.ps1

# Add VMWare plugin to Powershell and connect to vCenter

Add-pssnapin VMware.VimAutomation.Core

Set-executionpolicy remotesigned -Confirm:$false

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$false

connect-viserver localhost

# create table to capture VM snapshot info

$vmtable = New-Object system.Data.DataTable "VMTable"

$col1 = New-Object system.Data.DataColumn VM,([string])

$col2 = New-Object system.Data.DataColumn Snapshot,([string])

$col3 = New-Object system.Data.DataColumn SizeMB,([string])

$col4 = New-Object system.Data.DataColumn Created,([string])

$vmtable.columns.add($col1)

$vmtable.columns.add($col2)

$vmtable.columns.add($col3)

$vmtable.columns.add($col4)

#examine all VMs on vCenter, if snapshot found - add it to the table

ForEach ($vm in (Get-VM | Sort-Object -Property Name))

{

  ForEach ($snapshot in (Get-Snapshot -VM $vm.Name | Sort-Object -Property Name))

    {

  $created = -split $snapshot.Created

  $row = $vmtable.NewRow()

  $row.VM = $vm.Name

  $row.Snapshot = $snapshot.Name

  $row.SizeMB = "{0:N0}" -f $snapshot.SizeMb

  $row.Created = $created[0]

  $vmtable.Rows.Add($row)

    }

}

# Create an HTML version of the VM Table

$vmhtml = "<table><tr align='center' style='font-weight: bold'><td>VM</td><td>Snapshot</td><td>Size</td><td>Created</td></tr>"

ForEach ($row in $vmtable.Rows)

{

    $vmhtml += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td><td align = 'right'>" + $row[2] + "</td><td>" + $row[3] + "</td></tr>"

}

$vmhtml += "</table>"

# create table to capture Datastore info

$dstable = New-Object system.Data.DataTable "DSTable"

$col1 = New-Object system.Data.DataColumn Datastore,([string])

$col2 = New-Object system.Data.DataColumn FreeGB,([string])

$col3 = New-Object system.Data.DataColumn PercFree,([string])

$dstable.columns.add($col1)

$dstable.columns.add($col2)

$dstable.columns.add($col3)

#examine all Datastores visible to vCenter hosts and add status to table

ForEach ($Datastore in (Get-Datastore | Sort-Object -Property Name))

{

  $row = $dstable.NewRow()

  $row.Datastore = $Datastore.Name

  $row.FreeGB = [math]::Round(($Datastore.CapacityMB - $Datastore.FreeSpaceMB)/1024,2)

  $row.PercFree = [math]::Round(($Datastore.FreeSpaceMB / $DataStore.CapacityMB)*100,0)

  $dstable.Rows.Add($row)

}

# Create an HTML version of the Datastore Table

$dshtml = "<table><tr align='center' style='font-weight: bold'><td>Datastore</td><td>FreeGB</td><td>PercFree</td></tr>"

ForEach ($row in $dstable.Rows)

{

    $dshtml += "<tr><td>" + $row[0] + "</td><td align = 'right'>" + $row[1] + "</td><td align = 'right'>" + $row[2] + "</td></tr>"

}

$dshtml += "</table>"

# finished with vCenter so disconnect

Disconnect-VIServer -Confirm:$false

#set variables for email

$smtpserver = "X.X.X.X"

$from = "vcenter_reporter@x.com"

$to = "emailaddress@x.com"

$subject = "Snapshot/Storage report"

# write $body string with HTML flags for formatting and add the strings for the HTML tables

$writeTime = get-date -format "dd/MM/yyyy"

$body = "<h2 align='center'>vCenter Daily Report $writeTime</h2><hr><br /><br />Existing Snapshots:<br /><br />" + $vmhtml + "<br /><br />Datastore Status:<br /><br />" + $dshtml

# send email in HTML format

Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml

Reply
0 Kudos