VMware Cloud Community
xbelond
Contributor
Contributor

Get tag valu and send mail to this value

Hi

I would like to modify this script, to retrieve the value of a tag (managed by) and send the email using this value.

 

Add-PSSnapin VMware.VimAutomation.Core
$From = "admin-vmw@***.fr"
$To = "xavier.b@***.fr"
$Smtp = "***"
$Delay = 70

#Connect-VIServer $ESXserver -User $ESXUser -Password $ESXPwd
Connect-viserver s00750..***.**

$VMNamesArray = Get-VM | select Name
foreach ($VMName in $VMNamesArray)
{
$VMSnaps = Get-Snapshot $VMName.name
foreach ($Snap in $VMSnaps)
{
$now = Get-Date
if ($Snap.Created.AddDays($Delay) -gt $now -eq $false)
{
$MailString = "Bonjour, le serveur '$SnapVM' a un snapshot qui existe depuis plus de $Delay jours."
Send-MailMessage -From $From -To $To -Subject "Alerte snapshot sur '$snapvm' " -SmtpServer $Smtp -Body $MailString
}
$SnapVM = $Snap.VM
else
{
$MailString = "Aucun snapshot datant de plus de $Delay jours existant."
Send-MailMessage -From $From -To $To -Subject "Rapport snapshot" -SmtpServer $Smtp -Body $MailString
}
}
}

0 Kudos
4 Replies
LucD
Leadership
Leadership

You could try something like this

$From = "admin-vmw@***.fr"
$Smtp = "***"
$Delay = 70

#Connect-VIServer $ESXserver -User $ESXUser -Password $ESXPwd
Connect-VIServer s00750..***.**

$VMNamesArray = Get-VM | select Name
foreach ($VMName in $VMNamesArray) {
    $sendTo = (Get-TagAssignment -Entity $VMName -Category 'Managed by').Tag.Name
    $VMSnaps = Get-Snapshot $VMName.name
    foreach ($Snap in $VMSnaps) {
        $now = Get-Date
        if ($Snap.Created.AddDays($Delay) -gt $now -eq $false) {
            $MailString = "Bonjour, le serveur '$SnapVM' a un snapshot qui existe depuis plus de $Delay jours."
            Send-MailMessage -From $From -To $sendTo -Subject "Alerte snapshot sur '$snapvm' " -SmtpServer $Smtp -Body $MailString
        }
        $SnapVM = $Snap.VM
        else
        {
            $MailString = "Aucun snapshot datant de plus de $Delay jours existant."
            Send-MailMessage -From $From -To $sendTo -Subject "Rapport snapshot" -SmtpServer $Smtp -Body $MailString
        }
    }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
0 Kudos
xbelond
Contributor
Contributor

Thanks, 

I created a xxx tag for test, with my email address in value but I don't receive an email

i modified in the code : 
$sendTo = (Get-TagAssignment -Entity '$VMName' -Category 'xxx').Tag.Name

xbelond_0-1662719518488.png

 

0 Kudos
LucD
Leadership
Leadership

That should be without the quotes

$sendTo = (Get-TagAssignment -Entity $VMName -Category 'xxx').Tag.Name

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
xbelond
Contributor
Contributor

Thanks, 


The final Script : 

$From = "****"
$Smtp = "***"
$Delay = 0

Connect-VIServer $ESXserver -User $ESXUser -Password $ESXPwd

$VMNamesArray = Get-VM | select Name

foreach ($VMName in $VMNamesArray) {
$VMSnaps = Get-Snapshot $VMName.Name
foreach ($Snap in $VMSnaps) {
$now = Get-Date

if ($Snap.Created.AddDays($Delay) -gt $now -eq $false) {
echo $VMName.Name
if ((Get-TagAssignment -Entity $VMName.Name -Category 'xxx') -ne $null) {
$sendTo = (Get-TagAssignment -Entity $VMName.Name -Category 'xxx').Tag.Name
echo $sendTo
$MailString = "Bonjour, le serveur '$VMName.Name' a un snapshot qui existe depuis plus de '$Delay' jours."
Send-MailMessage -From $From -To $sendTo -Subject "Alerte snapshot sur '$VMName.Name' " -SmtpServer $Smtp -Body $MailString
}
}

else
{
$MailString = "Aucun snapshot datant de plus de $Delay jours existant."
Send-MailMessage -From $From -To xavier.belond@wurth.fr -Subject "Rapport snapshot" -SmtpServer $Smtp -Body $MailString
}
}
}

0 Kudos