Vmware Snapshot Mail Notifications, ESXI Mismatch Check
PS ISE
Vmware Snapshot Check Mail Notifications
Connect-VIServer "YourvCenter"
$Header = @"
<style>
BODY{font-family: Arial; font-size: 12pt;}"
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>
"@
$Date = Get-Date
$AllVMs = Get-VM -Location "clustername"
$owners = $AllVMs.CustomFields | Where-Object -Property key -EQ "VM Owner" | select -Unique
foreach($owner in $owners)
{
$vms = $allvms | where {$_.CustomFields.value -eq $owner.value}
$Result=foreach($vm in $vms)
{
$VMOwner = ($VM.CustomFields | Where-Object -Property key -EQ "VM Owner").Value.Replace('@domain.com', '')
$VMSnapshots = $VM | Get-Snapshot
$e= $VMOwner+"@domain.com"
foreach ($Snapshot in $VMSnapshots)
{
[pscustomobject]@{
"VM Name:" = $VM.Name
"VM Owner:" = $VMOwner
"Snapshot Name:" = $Snapshot.Name
"Snapshot Description:" = $Snapshot.Description
"Total Snapshots in this VM:" = ($VMSnapshots | Measure-Object).Count
"Date Creation:" = $Snapshot.created
"Snapshot age (Days):" = [System.Math]::Round( ($Date - $Snapshot.Created).TotalDays / 1)
"Snapshot SizeGB" = [System.Math]::Round($Snapshot.SizeGB, 1)
"Total Snapshots SizeGB:" = [System.Math]::Round( ($VMSnapshots | Measure-Object -Sum SizeGB).Sum, 1)
}
}
}
$Email = $Result| ConvertTo-Html -Head $Header
$e= ($VM.CustomFields | Where-Object -Property key -EQ "VM Owner").value
if($result -eq "" -or $result -eq $null)
{
write host "no snap on $vm"
}
else
{
Send-MailMessage -Subject "Your VM's have snapshot!" -Body " <h1 style=color:black;font-size:20px;>Dear, $VMOwner</h1><span style=color:black;font-size:16px;> We notice that the following VM includes snapshots, <br />
this means that snapshots are utilized storage capacity and degraded the server performance.<br /> Please login to the vCenter and delete unnecessary snapshots, otherwise, you can send us email at [email protected] and we'll do it for you. <br /> <br /> - If snapshot is required, send us email to exclude this vm from alert.<br /><br /> Thank you very much.<br /><br /></p> </span ></style> $Email " -BodyAsHtml -SmtpServer "EmailServer" -From [email protected] -to $e
}
}
Get all ESXI details Powercli
$myESXiHosts = Get-VMHost | %{ $_ | Select Name,
@{N="ESXi Version";E={"$($_.Version) build $($_.Build)"}},
@{N="vCenter";E={$_.cluster.Split('/')[2]}},
@{N="vCenter version";E={
$global:DefaultVIServers |
where {$_.Name -eq ($_.ExtensionData.Client.ServiceUrl.Split('/')[2])} |
%{"$($_.Version) build $($_.Build)"}
}},
@{N="Make";E={(Get-EsxCli -VMHost $_.Name).hardware.platform.get().VendorName}},
@{N="Model";E={(Get-EsxCli -VMHost $_.Name).hardware.platform.get().ProductName}},
@{N="Serial";E={(Get-EsxCli -VMHost $_.Name).hardware.platform.get().SerialNumber}}
}
$myESXiHosts | Export-csv C:\Esxi.csv
Find VM name and Folder is mismatch
$VMFolder = @()
Foreach ($VM in (Get-VM |Get-View)){
$Details = “” |Select-Object VM,Path
$Folder = ((($VM.Summary.Config.VmPathName).Split(‘]‘)[1]).Split(‘/‘))[0].TrimStart(‘ ‘)
$Path = ($VM.Summary.Config.VmPathName).Split(‘/‘)[0]
If ($VM.Name-ne $Folder){
$Details.VM= $VM.Name
$Details.Path= $Path
$VMFolder += $Details
}
}
$VMFolder |Export-Csv -NoTypeInformation C:\Mismatch.csv
This post is licensed under CC BY 4.0 by the author.