I'm just getting started with powerCLI. Trying to write a script that get the vm name in CSV, check vm's guest disks with less than 20% free space and email to email address listed in CSV. However, I have difficult to group "name" into single email which having same email.
Below is the script that I use to list out vm's guest disks which less than 20%
import-csv ".\vm.csv" | Group-Object $_."email"
ForEach ($VM in Get-VM $_."name" ){($VM.Extensiondata.Guest.Disk | `
Select @{N="Name";E={$VM.Name}},DiskPath, `
@{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, `
@{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}} ,`
@{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}} | Where-Object {($_."Free Space %" -lt 20)})}
For example in CSV :
name email
vm1 test1@abc.com
vm2 test2@abc.com
vm3 test1@abc.com
vm4 test2@abc.com
Output:
email received by test1@abc.com
No VM Free%
1. vm1 10
2. vm3 9
email received by test2@abc.com
No VM Free%
1. vm2 13
2. vm4 15
Appreciate if anyone could help.
Thanks.
try Something like this:
Function SendSimpleMail
{
# Déclaration des paramètres
param([string]$Objet, [string]$Message, [string[]]$ToMail)
$SmtpServer="yoursmtpname"
$encoding=[System.Text.Encoding]::UTF8
$FromMail="from@domain"
Send-MailMessage -To $ToMail -Subject $Objet -Body $Message -SmtpServer $SmtpServer -From $FromMail -Encoding $encoding
}
import-csv ".\vm.csv" | %{
$VM=Get-VM $_.name
$Email==$_.email
$VM.Extensiondata.Guest.Disk | %{
[pscustomobject]@{
Email=$Email
Name=$VM.Name
DiskPath=$_.DiskPath
"Capacity(MB)"=[math]::Round($_.Capacity/ 1MB)
"Free Space(MB)"=[math]::Round($_.FreeSpace / 1MB)
"Free Space %"=if ($_.Capacity -eq 0) {0} else {[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}
}
}
} | group Email | %{
$body=$_.group | Out-String
SendSimpleMail "subject" $body $_.Name
}