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.
- 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.
$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