VMware Cloud Community
cbelnap
Contributor
Contributor

Email VM Owners to delete powered off images

I created this script as an easier way to email my vm users, based on them owning images that have been powered off for more than 30 days.  Hoping that they don't need them and they can be deleted.  I use another script that outputs the image list to a csv file with all the details needed.   I have many users that own a ton of images (100+) so trying to find an alternative way to write this report so it's not huge listing out each image, while also not making the email look un-professional with extra lines of junk.   So to save space here I took out a bunch of functions and if/else statements.  Currently I have it working with 12 functions snapmail1 through snapmail12 and 12 if/else statements to correspond to the nbr of images owned by a user.  Any input would be appreciated.

Thanks, Chad

:::SCRIPT:::

param(

        [Parameter()]

        [ValidateNotNullOrEmpty()]

        [string]$csv=$(throw "CSV File name is needed, from C:\data\csv-files folder.")

    )

$smtp = "smtp.example.com"

$MailFrom = "email@example.com"

Function SnapMail1 ($Mailto, $group){

   $msg = new-object Net.Mail.MailMessage

   $smtp = new-object Net.Mail.SmtpClient($smtp)

   $msg.From = $MailFrom

   $msg.To.Add($Mailto)

   $msg.Subject = "**Action Required** Powered Off Images more than 30 days"

$MailText = @"

The following image has been powered off for 30 days or more.  To help save and recycle storage resources, please delete the image if you no longer need it.

If you have a need to keep the image please let us know.

 

Owner Email: $($mailto)

$($group.vmname) vm on $($group.vcenter) - powered off since $($group.PowerOffDate) - disk space GB: $($group.TotalDiskGB)

Thank you,

Virtualization Team

"@

   $msg.Body = $MailText

   $smtp.Send($msg)

}

Function SnapMail2 ($Mailto, $group){

   $msg = new-object Net.Mail.MailMessage

   $smtp = new-object Net.Mail.SmtpClient($smtp)

   $msg.From = $MailFrom

   $msg.To.Add($Mailto)

   $msg.Subject = "**Action Required** Powered Off Images more than 30 days"

$MailText = @"

The following images have been powered off for 30 days or more.  To help save and recycle storage resources, please delete images you no longer need.

If you have a need to keep any specific image(s) please let us know.

Owner Email: $($mailto)

$($group.vmname[0]) vm on $($group.vcenter[0]) - powered off since $($group.PowerOffDate[0]) - disk space GB: $($group.TotalDiskGB[0])

$($group.vmname[1]) vm on $($group.vcenter[1]) - powered off since $($group.PowerOffDate[1]) - disk space GB: $($group.TotalDiskGB[1])

Thank you,

Virtualization Team

"@

   $msg.Body = $MailText

   $smtp.Send($msg)

}

----------  extra functions here----------

Function SnapMail12 ($Mailto, $group){

   $msg = new-object Net.Mail.MailMessage

   $smtp = new-object Net.Mail.SmtpClient($smtp)

   $msg.From = $MailFrom

   $msg.To.Add($Mailto)

   $msg.Subject = "**Action Required** Powered Off Images more than 30 days"

$MailText = @"

The following images have been powered off for 30 days or more.  To help save and recycle storage resources, please delete images you no longer need.

If you have a need to keep any specific image(s) please let us know.

Owner Email: $($mailto)

$($group.vmname[0]) vm on $($group.vcenter[0]) - powered off since $($group.PowerOffDate[0]) - disk space GB: $($group.TotalDiskGB[0])

$($group.vmname[1]) vm on $($group.vcenter[1]) - powered off since $($group.PowerOffDate[1]) - disk space GB: $($group.TotalDiskGB[1])

$($group.vmname[2]) vm on $($group.vcenter[2]) - powered off since $($group.PowerOffDate[2]) - disk space GB: $($group.TotalDiskGB[2])

$($group.vmname[3]) vm on $($group.vcenter[3]) - powered off since $($group.PowerOffDate[3]) - disk space GB: $($group.TotalDiskGB[3])

$($group.vmname[4]) vm on $($group.vcenter[4]) - powered off since $($group.PowerOffDate[4]) - disk space GB: $($group.TotalDiskGB[4])

$($group.vmname[5]) vm on $($group.vcenter[5]) - powered off since $($group.PowerOffDate[5]) - disk space GB: $($group.TotalDiskGB[5])

$($group.vmname[6]) vm on $($group.vcenter[6]) - powered off since $($group.PowerOffDate[6]) - disk space GB: $($group.TotalDiskGB[6])

$($group.vmname[7]) vm on $($group.vcenter[7]) - powered off since $($group.PowerOffDate[7]) - disk space GB: $($group.TotalDiskGB[7])

$($group.vmname[8]) vm on $($group.vcenter[8]) - powered off since $($group.PowerOffDate[8]) - disk space GB: $($group.TotalDiskGB[8])

$($group.vmname[9]) vm on $($group.vcenter[9]) - powered off since $($group.PowerOffDate[9]) - disk space GB: $($group.TotalDiskGB[9])

$($group.vmname[10]) vm on $($group.vcenter[10]) - powered off since $($group.PowerOffDate[10]) - disk space GB: $($group.TotalDiskGB[10])

$($group.vmname[11]) vm on $($group.vcenter[11]) - powered off since $($group.PowerOffDate[11]) - disk space GB: $($group.TotalDiskGB[11])

Thank you,

Virtualization Team

"@

   $msg.Body = $MailText

   $smtp.Send($msg)

}

$report = Import-CSV "C:\data\csv-files\$csv.csv"

# Group CSV Objects based on owner email field

$groups = $report | Group-Object -Property {$_.OwnerEmail}

# Display output of grouped objects

$groups

ForEach ($owner in $groups){

    Write-Host $owner.name $owner.count

    if ($owner.group.count -eq "1"){

    Write-Host $owner.group.vmname $owner.group.vcenter $owner.group.totaldiskgb $owner.group.poweroffdate

    snapmail1 $owner.name $owner.group

    }

    elseif ($owner.group.count -eq "2"){

    Write-Host $owner.group.vmname[0] $owner.group.vcenter[0] $owner.group.totaldiskgb[0] $owner.group.poweroffdate[0]

    Write-Host $owner.group.vmname[1] $owner.group.vcenter[1] $owner.group.totaldiskgb[1] $owner.group.poweroffdate[1]

    snapmail2 $owner.name $owner.group

    }

    ------- extra if/else statements here--------

    elseif ($owner.group.count -eq "12"){

    Write-Host $owner.group.vmname[0] $owner.group.vcenter[0] $owner.group.totaldiskgb[0] $owner.group.poweroffdate[0]

    Write-Host $owner.group.vmname[1] $owner.group.vcenter[1] $owner.group.totaldiskgb[1] $owner.group.poweroffdate[1]

    Write-Host $owner.group.vmname[2] $owner.group.vcenter[2] $owner.group.totaldiskgb[2] $owner.group.poweroffdate[2]

    Write-Host $owner.group.vmname[3] $owner.group.vcenter[3] $owner.group.totaldiskgb[3] $owner.group.poweroffdate[3]

    Write-Host $owner.group.vmname[4] $owner.group.vcenter[4] $owner.group.totaldiskgb[4] $owner.group.poweroffdate[4]

    Write-Host $owner.group.vmname[5] $owner.group.vcenter[5] $owner.group.totaldiskgb[5] $owner.group.poweroffdate[5]

    Write-Host $owner.group.vmname[6] $owner.group.vcenter[6] $owner.group.totaldiskgb[6] $owner.group.poweroffdate[6]

    Write-Host $owner.group.vmname[7] $owner.group.vcenter[7] $owner.group.totaldiskgb[7] $owner.group.poweroffdate[7]

    Write-Host $owner.group.vmname[8] $owner.group.vcenter[8] $owner.group.totaldiskgb[8] $owner.group.poweroffdate[8]

    Write-Host $owner.group.vmname[9] $owner.group.vcenter[9] $owner.group.totaldiskgb[9] $owner.group.poweroffdate[9]

    Write-Host $owner.group.vmname[10] $owner.group.vcenter[10] $owner.group.totaldiskgb[10] $owner.group.poweroffdate[10]

    Write-Host $owner.group.vmname[11] $owner.group.vcenter[11] $owner.group.totaldiskgb[11] $owner.group.poweroffdate[11]

    snapmail12 $owner.name $owner.group

    }

}

0 Kudos
2 Replies
MaheshBadge
Contributor
Contributor

i suppose on top of this you will have to write a function to check which VM's in your environment are powered-off since 30 days. Collect that data in excel.

0 Kudos
cbelnap
Contributor
Contributor

I use another script to pull the list of powered off vms with a function Luc Dekens wrote called get-dump.  It searches datastores for mod dates rather than use events since they have been deleted from the vc db.  So just looking for a way to make this script work for users with a ton of vms. 

0 Kudos