vCenter altında silinen sanal makinelerin PowerCLI yardımı ile bulunması ve mail ile gönderilmesi için aşağıdaki script’i kullanabilirsiniz. İlgili yerleri kendinize göre değiştirmeyi unutmayın.
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
Function Get-VMDeleteHistory {
param(
[Parameter(Mandatory=$false)]$MaxSamples=500
)
$results = @()
$events = Get-VIEvent -Start (Get-Date).AddMinutes(-15) -Types Info | where {$_.GetType().Name -eq "TaskEvent" -and $_.FullFormattedMessage -eq "Task: Delete virtual machine" }
foreach ($event in $events) {
$tmp = [pscustomobject] @{
VM = $event.Vm.Name;
User = $event.UserName;
Date = $event.CreatedTime;
}
$results += $tmp
}
$results
}
Import-Module -Name VMware.VimAutomation.Core
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$vmcredential = Import-Clixml -Path "${env:\userprofile}\VMware-test.Cred"
Connect-VIServer 192.168.0.110 -Credential $vmcredential
#Get-VmDeleteHistory
if (Get-VmDeleteHistory)
{
$deletedvms = Get-VmDeleteHistory |Sort Date -Descending|Select Date,User,VM | ConvertTo-Html -Property Date,User,VM -Head $Header | Out-File c:\Script\DeletedVMs.html | ft -AutoSize
$deletedvmslist = Get-Content C:\Script\DeletedVMs.html
$credential = Import-Clixml -Path "${env:\userprofile}\Mail-Cred.Cred"
Send-MailMessage -From "VMware Alert <vmware.alert@domain.com>" -To "taner@domain.com" -Subject "vCenter Silinen VM" -Body ($deletedvmslist | Out-String) -BodyAsHtml -Priority High -dno onSuccess, onFailure -SmtpServer "mail.domain.com" -Port 587 -Credential $credential
}
Bu script ile, çalıştığı zamandan geri doğru 15 dakika geriye doğru silinen VM’leri C:\Script
dizini altında DeletedVMs.html
isimli bir HTML dosyada oluşturuyor. Scripti windows bir sunucu üzerinde 15 dakikada bir çalışacak şekilde zamanlarsanız, bu dosyayı mail ile belirtilen mail adreslerine gönderiyor. Aşağıdaki gibi bir çıktı oluşuyor.