justinhamlin_ti
Contributor
Contributor

Output disk space to email

Jump to solution

Hopefully this is an easy one I am just missing something.  I would like to run a script as a scheduled task, the script would query a group of servers disk space and for each server whose disk space is under a certain threshold, it would send a separate email regarding that server.  For example, if I have 5 servers who have a disk with under 20% free, I would like to get 5 separate emails

This is the base script I have been working with, so far, it works great, but what happens is if ANY server has a drive under 20% it sends only 1 email with the $name variable in the subject being the last server whose disk space is under 20%

# Preparing Console

Add-PSSnapin VMware.VimAutomation.Core

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$False

connect-viServer -server vCenterServer.domain.com

#Get Disk Space

ForEach ($VM in ( Get-VM -Location "Preview Environments" |Get-View)){

($info = $VM.Guest.Disk |Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(GB)";E={[math]::Round($_.Capacity/ 1GB)}}, @{N="Free Space(GB)";E={[math]::Round($_.FreeSpace / 1GB)}}, @{N="Free Space %";E={[math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)),0)}})|Format-Table

}

#Output

ForEach ($path in $info){

$freespace = $path."Free Space %"

$name = $path.Name

$diskpath = $path.DiskPath

if ($freespace -lt 20){Send-MailMessage -to "email@email.com" -From "alert@vmware.com" -SmtpServer "emailserver.domain.com" -Subject "Free Space on $name on $diskpath is lower than $freespace %" }

}

Any guidance would be appreciated!

0 Kudos
1 Solution

Accepted Solutions
kunaludapi
Expert
Expert

Missing parts in bold red.

# Preparing Console

Add-PSSnapin VMware.VimAutomation.Core

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$False

connect-viServer -server vCenterServer.domain.com

#Get Disk Space

$info =  @()

ForEach ($VM in ( Get-VM -Location "Preview Environments" |Get-View)){

($info += $VM.Guest.Disk |Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(GB)";E={[math]::Round($_.Capacity/ 1GB)}}, @{N="Free Space(GB)";E={[math]::Round($_.FreeSpace / 1GB)}}, @{N="Free Space %";E={[math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)),0)}})|Format-Table

}

#Output

ForEach ($path in $info){

$freespace = $path."Free Space %"

$name = $path.Name

$diskpath = $path.DiskPath

if ($freespace -lt 20){Send-MailMessage -to "email@email.com" -From "alert@vmware.com" -SmtpServer "emailserver.domain.com" -Subject "Free Space on $name on $diskpath is lower than $freespace %" }

}

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

View solution in original post

0 Kudos
4 Replies
kunaludapi
Expert
Expert

Missing parts in bold red.

# Preparing Console

Add-PSSnapin VMware.VimAutomation.Core

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$False

connect-viServer -server vCenterServer.domain.com

#Get Disk Space

$info =  @()

ForEach ($VM in ( Get-VM -Location "Preview Environments" |Get-View)){

($info += $VM.Guest.Disk |Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(GB)";E={[math]::Round($_.Capacity/ 1GB)}}, @{N="Free Space(GB)";E={[math]::Round($_.FreeSpace / 1GB)}}, @{N="Free Space %";E={[math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)),0)}})|Format-Table

}

#Output

ForEach ($path in $info){

$freespace = $path."Free Space %"

$name = $path.Name

$diskpath = $path.DiskPath

if ($freespace -lt 20){Send-MailMessage -to "email@email.com" -From "alert@vmware.com" -SmtpServer "emailserver.domain.com" -Subject "Free Space on $name on $diskpath is lower than $freespace %" }

}

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
justinhamlin_ti
Contributor
Contributor

Thanks for the quick reply, but unfortunately that did not change the end result, I still only get a single email with the subject line relating to the last server that was processed.

(as a test I am setting the threshold at 70% free space, so I should get quite a few more emails, the folder "Preview Environments" has approximately 25 servers included in its folder)

What is odd, is the output in the PowerCLI screen SHOWS all of the servers, drives, space, etc, but only one email is generated.

On a related note, if I change the Get-VM to only reference a single server, and more than 1 drive is under the threshold, I get multiple emails

0 Kudos
sneddo
Hot Shot
Hot Shot

Did you catch both edits? Just tested that code and it works for me Smiley Happy

0 Kudos
justinhamlin_ti
Contributor
Contributor

ahhh, damn.  I missed the + in red

Just tested, works like a charm!  Thanks!!

0 Kudos