Report o VMs a snapshotoch vo vCenter

Script, ktorý vygeneruje .csv s informáciami o VMs a html tabuľku so snapshotami staršími ako 14 dní. Upozornenie o snapshotoch zasiela aj správcom applikácií.

Prvá časť scriptu vytvorí zoznam VMs. Tento zoznam obsahuje Meno, Verziu vHW, Power state, IP, Pamat RAM, Resource Pool, Poznámku, počet CPU, Folder a ESX host na ktorom daná VM beží.

Čo sa týka snapshotov. V prípade, že applikačný správca dostane e-mail o snapshote staršom ako 14 dní, má 2 možnosti.

  • Alebo ho odignoruje a  vmware správca ho zmaže po 3 dňoch
  • Alebo požiada e-mailom o jeho predlženie

V prípade, že požiada o jeho ponechanie, vmware správca danú VM otaguje vytvoreným tagom vo vCenter – “Dlhodobe snapshoty”. Script potom tieto VMs a ich snapshoty z notifikovania vynechá.

$TagVMs = Get-VM -Tag "Dlhodobe snapshoty"

$VMs = Compare-Object -ReferenceObject $TagVMs -DifferenceObject $AllVMs | Where-Object {$_.SideIndicator -eq "=>"}

Najrýchlejšie je asi otagovať VM z PowerCLI:

New-TagAssignment -Tag "Dlhodobe snapshoty" -Entity (Get-VM <nazov VM>)

Samotný správcovia applikácií  sú v tomto riešení pridaní  ako tagy v kategórií AppOwners a mail, na ktorý sa má zaslať notifikácia sa “vytiahne” odtiaľ.

$mail = Get-TagAssignment -Entity $_.VM -Category AppOwners | Select-Object -ExpandProperty Tag | Select-Object -ExpandProperty Name

V prípade, že správca aplikácie nieje zadaný, notifikácia sa pošle na default mail (napr. tímový mail).

Informácie o tom, komu boli zaslané notifikácie sa ukladajú do logu, ktorý je zasielaný spolu s:

  • logom o spustení a ukončení scriptu
  • zoznamom VMs
  • tabuľkou snapshotov a snapshotov v kategórii “Dlhodobe snapshoty”.
"$((Get-Date).ToString("dd.MM.yyyy hh:mm:ss")): Bol spusteny ListVMsSnaps14.ps1" | Add-Content C:\Scripts\ListVMsSnaps14.log
add-pssnapin VMware.VimAutomation.Core
connect-VIserver -server vc.yourcompany.com
$report = @()

$filename1="C:\Scripts\Exports\ListVMs{0:yyyyMMdd}.csv" -f (Get-Date)
$filename3="C:\Scripts\Exports\SnapshotsMailInfo.log"

$style = "<style>BODY{font-family:Arial;font-size:10pt;}"
$style = $style + "TABLE{border:1px solid black;border-collapse:collapse;}"
$style = $style + "TH{border:1px solid black;background:#dddddd;padding:5px;}"
$style = $style + "TD{border:1px solid black;padding:5px;}"
$style = $style + "</style>"

$AllVMs = Get-VM
$TagVMs = Get-VM -Tag "Dlhodobe snapshoty"
$VMs = Compare-Object -ReferenceObject $TagVMs -DifferenceObject $AllVMs | Where-Object {$_.SideIndicator -eq "=>"}
$Snapshots = Get-VM -Name $VMs.InputObject | Get-Snapshot
$SnapshotsLt14 = $Snapshots | Where-Object {$_.Created -lt ((Get-date).AddDays(-14))}
$SnapshotsLt14Edit = $SnapshotsLt14 | Select-Object VM,Name,Created,@{N="SizeGB";E={[math]::round($_.SizeGB, 3)}} | Sort-Object -Property SizeGB -Descending
$SnapshotsLt14EditHTML = $SnapshotsLt14Edit | ConvertTo-Html -Head $style
$SnapshotsTagVMsHTML = Get-Snapshot -VM $TagVMs | Select-Object VM,Name,Created,@{N="SizeGB";E={[math]::round($_.SizeGB, 3)}} | Sort-Object -Property SizeGB -Descending | ConvertTo-Html -Head $style

$AllVMs | ForEach-Object {

$row = "" | Select "VM Name", "HW Version", State, IP, Memory, OS, "Resource Pool", Notes, NumCpu, Folder, Host

$Ver = $_|get-view
$vmGuest = $_ | Get-VMGuest
$row."VM Name" = $_.Name
$row."HW Version"= $Ver.Config.Version
$row.State = $_.PowerState
$row.IP=$vmGuest.IPAddress[0]
$row.Memory=$_.MemoryMB
$row.OS=$vmGuest.OSFullName
$row."Resource Pool"=$_.ResourcePool
$row.Notes=$_.Notes
$row.NumCpu=$_.NumCpu
$row.Folder=$_.Folder
$row.Host=$_.VMHost
$report += $row

}

$SnapshotsLt14 | ForEach-Object {

$sDateCreated = ($_.Created).ToString("dd.MM.yyyy hh:mm:ss")
$sDays = ((Get-Date) - ($_.Created)).Days
$sDateDEL = ((Get-Date).adddays(3)).ToString("dd.MM.yyyy")
$mail = Get-TagAssignment -Entity $_.VM -Category AppOwners | Select-Object -ExpandProperty Tag | Select-Object -ExpandProperty Name
if ($mail.Length -eq 0) {
$mail = "VMTeam@yourcompany.com"
send-mailmessage -from "VM Team <VMTeam@yourcompany.com>" -to $mail -subject "$sDays dni stary snapshot na $($_.VM)" `
-BodyAsHtml "Na serveri <b>$($_.VM)</b> bol dna <b>$sDateCreated</b> vytvoreny snapshot. Ak do <b>$sDateDEL</b> nepoziadate o jeho predlzenie, bude automaticky zmazany. VMTeam" -smtpServer smtp.yourcompany.com
"$((Get-Date).ToString("dd.MM.yyyy")): Kedze polozka App owner nebola vyplnena, upozornenie o starom snapshote na $($_.VM) bola zaslana na $mail" | Add-Content C:\Scripts\Exports\SnapshotsMailInfo.log
}
else {
send-mailmessage -from "VM Team <VMTeam@yourcompany.com>" -to $mail -subject "$sDays dni stary snapshot na $($_.VM)" `
-BodyAsHtml "Na serveri <b>$($_.VM)</b> bol dna <b>$sDateCreated</b> vytvoreny snapshot. Ak do <b>$sDateDEL</b> nepoziadate o jeho predlzenie, bude automaticky zmazany. VMTeam" -smtpServer smtp.yourcompany.com
"$((Get-Date).ToString("dd.MM.yyyy")): Upozornenie ze na $($_.VM) je snapshot stary $($sDays) dni bolo zaslane na $mail" | Add-Content C:\Scripts\Exports\SnapshotsMailInfo.log
}

}
$report | Export-CSV $filename1 -NoTypeInformation

send-mailmessage -from "VCenter <vcenter@yourcompany.com>" -to "VMTeam@yourcompany.com <VMTeam@yourcompany.com>" -subject "Reports from vcenter" -bodyAsHtml "List of VMs and List of snapshots-14days old <br><br>$($SnapshotsLt14EditHTML) <br><br>Dlhodobe snapshoty:<br><br>$($SnapshotsTagVMsHTML)" -Attachments "$filename1","$filename3" -smtpServer smtp.yourcompany.com
"$((Get-Date).ToString("dd.MM.yyyy hh:mm:ss")): Bol ukonceny ListVMsSnaps14.ps1" | Add-Content C:\Scripts\ListVMsSnaps14.log

Author: Martin

Infrastructure engineer | virtualization & cloud enthusiast | vSphere specialist | blogger | VMware vExpert 2017, 2018, 2019, 2020 | VMCE | Slovak VMUG Leader |  user group ambassador for kmug.sk | husband&father

Leave a Reply

Your email address will not be published. Required fields are marked *