VMware Cloud Community
mobinqasim786
Enthusiast
Enthusiast
Jump to solution

Output to Email

Hi Guys,

Could someone please help how to send output of below script to an email.

#############################################################

###

### Get all thick provisioned disks v2

### Version 1

### 17.5.2013

###

##############################################################

###

### CONFIGURATION

###

$login_user = ""

$login_pwd = ""

$login_host = "Enter your vCenter Server here"

##############################################################

### END

##############################################################

###Prepare variables

$vmlist = @()

###Check if we are connected to vCenter Server(s)

if($global:DefaultVIServers.Count -lt 1)

{

echo "We need to connect first"

#To connect using predefined username-password

#Connect-VIServer $login_host -User $login_user -Password $login_pwd -AllLinked:$true

#To connect using PowerCLI credential store

Connect-VIServer $login_host -AllLinked:$true

}

else

{

echo "Already connected"

}

get-view -ViewType VirtualMachine -Property Name, "Config.Hardware.Device" | %{

$vmName = $_.name

$_.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | %{

if(!$_.Backing.ThinProvisioned){

$sizeInGb = [Math]::Round(($_.CapacityInKB / 1MB),2)

$type = if ($_.Backing.ThinProvisioned) { “THIN” } else { "THICK" }

$label = $_.DeviceInfo.Label

$vmlist += "" | Select-Object @{n="VmName";e={$vmName}},@{n="DiskLabel";e={$label}},@{n="Backing";e={$type}},@{n="SizeInGB";e={$sizeInGb}}

}

}

}

#Print out our table

$vmlist | format-table -autosize

Regards,

Mobin

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the Send-MailMessage cmdlet.

Send-MailMessage -To you@domain.com -From you@domain.com -Subject 'Report' `

    -SmtpServer your-smtp-server@domain.com -Body ($vmList | Out-String)


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the Send-MailMessage cmdlet.

Send-MailMessage -To you@domain.com -From you@domain.com -Subject 'Report' `

    -SmtpServer your-smtp-server@domain.com -Body ($vmList | Out-String)


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

mobinqasim786
Enthusiast
Enthusiast
Jump to solution

Hi,

Thanks for the quick response. That worked like a charm. Output layout is scattered and not a proper format. Is is something need to added in the actual script to specify formatting? 

Appreciate for the help.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can format the Body as HTML, that should provide better formatting.

Send-MailMessage -To you@domain.com -From you@domain.com -Subject 'Report' `

    -SmtpServer your-smtp-server@domain.com `

    -BodyAsHtml -Body ($vmList | ConvertTo-Html | Out-String)

On the ConvertTo-Html cmdlet you can add further formatting, even CSS style sheets.

See for example It's In the Mail Part 2: Sending Rich Messages


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

0 Kudos
mobinqasim786
Enthusiast
Enthusiast
Jump to solution

Thanks for helping me out. I'll try to append different scripts using your command so it can generate emails with better format.

Regards

0 Kudos
mobinqasim786
Enthusiast
Enthusiast
Jump to solution

Hi,

Sorry for bothering again as I'm new to PowerCLI. I need to know how to get around with the following:

Let's say I have a below PowerCLI command which gives an output for VM hardware version.

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server 'vCenter Server' -User 'domain\username' -Password 'password'

#CMDlet to get VMs HW version

Get-VM | Select Name, Version | Sort-Object Version | FT -AutoSize

#Send Email

Send-MailMessage -To email@domain.com -From email2@domain.com -Subject 'Report' `

    -SmtpServer smtp.domain.com -BodyAsHtml -Body ($vmList | ConvertTo-Html | Out-String)

Here I don't have $vmlist parameters but in previous script I had that parameter. How can I modify the above script to send output in the email. Do I have to create a parameter first?

Appreciate for the help.

0 Kudos