Tag Archives: SMTP

Powershell Issues

I was recently working on a script to send an SMTP email reporting on VM’s that only have a certain amount of free guest VM disk space left.

It turns out to use ‘Connect-VIServer’ cmdlet in your PowerShell script. You have to follow two steps:

1) Install VMware PowerCLI in your machine. It will register itself in PowerShell during the installation.

2) Use ‘Add-PSSnapIn VMware.VimAutomation.Core’ to add the registered PowerShell VMware Snapin to the current session.

So essential, the first lines of your script should have the ‘Add-PSSnapIn VMware.VimAutomation.Core’ entry otherwise the native Microsoft PowerShell

will fail due to unknown commands.

For those interested, the script looked like the below.

Add-PSSnapIn VMware.VimAutomation.Core

Connect-VIServer <servername>

$treshold = 10GB

$report = Get-VM | where { $_.PowerState -eq “PoweredOn” -and $_.Guest } | Get-VMGuest | %{
$vm = $_
$_.Disks | where {$_.FreeSpace -le $treshold} | `
select @{N=”VMName”;E={$vm.VMName}},Path,@{N=”DiskFreeGB”;E={[math]::Round((($_.FreeSpace)/1GB),2)}}
}

$emailFrom = <from-email-addr>
$emailTo = <to-email-addr>
$subject = “Free disk space less than ” + ($treshold/1GB) + “GB”
$body = $report | ft -AutoSize | Out-String
$smtpServer = <smtp-server>
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
A big thank you to LucD on the VMware Communities Forum.