«

»

VM per datastore PowerCLI e-mail report

I have been asked to create a monitoring script to calculate the number of virtual machines per datastore. I know there are a lot of examples in the community doing the exact same thing. This particular case includes one requirement which i have not seen in the communities examples.

  • My customer wants one e-mail to be send per datastore if the number of virtual machines exceeds a specific number.

The specific number was given to the customer by the storage vendor.

I have put together a script which can be be scheduled the same way as described in one of my previous blog post which can be found here.

Change the red marked text in the script to your required values.

#
# PowerCLI script to send e-mail if the number of virtual machines per datastore exceeds 48
# Version 1.0
# Magnus Andersson, Real Time Services AB
#
#
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
#
#
# Get login password
$pwd = Get-Content d:vspherescriptspowerclicred | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential “homedomainpowercli“, $pwd
#
#
# Connect to vCenter Server
connect-viserver vc-demo01.home.test
#
#
$sendTo = “magnus@home.test
$ds = get-datastore
foreach ($datastore in $ds){
$num = Get-Datastore $datastore | Select @{N=”TotalVMs”;E={@($_ | Get-VM ).Count}}
if ($num.TotalVMs -ge 48) {
send-mailmessage -to $sendTo -from vc-demo01@home.test -Subject “Number of VMs per datastore $datastore is” -smtpserver smtp.home.test -body $num.TotalVms
}
else
{
}
}
#
The below picture shows the e-mail you receive when the number of virtual machines exceeds your selected value.
Screen Shot 2013-04-08 at 21.58.29
There are several other options you might want to consider if you got other customer reporting output requirements or no reporting output requirements e.g.:
  • Including all datastores in one e-mail as plain text.
  • Including the report as an attachment to one e-mail.

Run the below PowerCLI code for including all datastores in one e-mail as plain text. Change the red marked text in the script to your required values.

$sendTo = “magnus@home.test
$IssueDS = Get-Datastore | Select Name, @{N=”TotalVMs”;E={@($_ | Get-VM ).Count}} | Where { $_.TotalVMs -ge 48 } | out-string
if ($IssueDS) {
send-mailmessage -to $sendTo -from vc-demo01@home.test -Subject “Report – datastores contaning more than 48 virtual machines” -smtpserver smtp.home.test -body $IssueDS
}
The below picture shows the e-mail you receive when the number of virtual machines exceeds your selected value.

Screen Shot 2013-04-09 at 22.03.27
Run the PowerCLI code for Including all datastores in one e-mail as an attachment. Change the red marked text in the script to your required values.
$sendTo = “magnus@home.test” 
$date = get-date -uformat %Y-%m-%d 
$outfile = “c:vspherescriptsstatisticsVM-per-DS_$date.html” 
function vmperds { Get-Datastore | Select Name,@{N=”TotalVMs”;E={@($_ | Get-VM ).Count}} | Where { $_.TotalVMs -ge 48 }| Sort Name } 
vmperds | ConvertTo-HTML | Out-File $outfile 
send-mailmessage -to $sendTo -from vc-demo01@home.test -Subject “Report – datastores contaning more than 48 virtual machines” -smtpserver smtp.home.test -Attachments $outfile 
Remove-item $outfile
The below picture shows the e-mail you receive when the number of virtual machines exceeds your selected value.
Screen Shot 2013-04-09 at 22.17.15
Thanks to Alan Renouf @alanrenouf, for pointing out a structural PowerCLI error and for suggesting and creating the main part of the e-mail report including all datastores as plain text.