Offene Snapshots in SCVMM reporten

Mal wieder ein Klassiker… offene Snapshots.

Hier wird ein Report erstellt und per Mail geschickt, der alle offenen Snapshot der Hyper-V Umgebung (mittels SCVMM) ermittelt. Es gibt eine Besonderheit: Manche Snapshots dürfen da sein. Diese sind dann mit “persistent” in der Snapshot Beschreibung gekennzeichnet.

# Funktion um die Tabelle zu konvertieren
function Convert-Table2HTML {
    <#
	.SYNOPSIS
	    Convert a PowerShell-Table to a HTML-Table.

	.DESCRIPTION
	    The Convert-Table2HTML cmdlet converts a PowerShell-Table to a HTML-Table in order to send it via email and have it available directly within the email.
		
	.PARAMETER Table
	    Specifies the PowerShell-Table. 

	.EXAMPLE
		PS C:\> Convert-Table2HTML -Table $ExampleTable
		PS C:\> Convert-Table2HTML -Table $ExampleTable -NoteProperties "VMName, ClusterName"

	.NOTES
		Author: Joachim Armbruster
		Blog  : http://www.PowerShell24.de/
	#>

	Param
	(
		[parameter(Mandatory=$true)]$Table,
		$NoteProperties
	)


	$Farbe1 = "bgcolor='#c5c5c5'"
	$Farbe2 = "bgcolor='#f0f0f0'"
	
	if(!$NoteProperties)
	{
		$NoteProperties = $Table | Get-Member -Type *Property*
		$CustomNoteProperty = $false
	}
	else
	{
		$Array = $NoteProperties -Split ","
		$NoteProperties = @()
		foreach($Iterator in $Array)
		{
			While($Iterator -like "* ")
			{
				$Iterator = $Iterator.SubString(0, $Iterator.Length-1)
			}
			While($Iterator -like " *")
			{
				$Iterator = $Iterator.SubString(1, $Iterator.Length-1)
			}
			$NoteProperties += $Iterator
		}
		$CustomNoteProperty = $true
	}
	
	$html = "<table border='1'>"
	$html += "<tr>"
	
	
	foreach($NoteProperty in $NoteProperties)
	{
		if($CustomNoteProperty)
		{
			$NotePropertyName = $NoteProperty
		}
		else
		{
			$NotePropertyName = $NoteProperty.Name
		}
		$html += "<th>$NotePropertyName</th>"
	}	
	$html += "</tr>"
	
	$FarbenCounter = 0
	
	
	for ($i = 0; $i -lt $Table.Count; $i++)
	{
	
		if(($FarbenCounter % 2) -eq 0)
		{
			$bgcolor = $Farbe1
		}else
		{
			$bgcolor = $Farbe2
		}
		
		$html += "<tr "  + $bgcolor + ">"
		foreach($NoteProperty in $NoteProperties)
		{
			if($CustomNoteProperty)
			{
				$NotePropertyName = $NoteProperty
			}
			else
			{
				$NotePropertyName = $NoteProperty.Name
			}
			$Eintrag = $Table[$i].$NotePropertyName
			$html += "<td>$Eintrag</td>"
		}	
			
		$html += "</tr>"
		$FarbenCounter++
	
	}
	
	$html += "</table>"
	return $html

}

# Konstante definieren
$MailFrom = "Monitoring@PowerShell24.de"
$MailTo = "Joachim@PowerShell24.de"
$MailSubject = "Snapshots auf Hyper-V Umgebung"
$MailSMTPServer = "smtp.powershell24.de"

$MailBody = "Folgende Snapshots sind auf der Hyper-V Umgebung offen:"

# Start
Import-Module virtualmachinemanager

$Tabelle = @()
Get-VMCheckpoint | %{
	$Name = $_.Name 
	$Description = $_.Description 
	if(($Description -notlike "*persistent*") -and ($Name -notlike "*persistent**")){
		
		$SSSizeCombined = 0

		$AddedTime = $_.AddedTime	

		$VMName = $_.VM.Name
		$VMHost = $_.VM.VMHost
		
		$CheckpointLocation = $_.VM.CheckpointLocation
		$ID = $_.ID.Guid
		$unc = "\\" + $VMHost + "\" + ($CheckpointLocation -Replace ":","$")
		$UncInhalt = Get-ChildItem $unc | ? {$_.name -like "*avhd*"}
		
		foreach ($snapshot in $UncInhalt){
			$SSSizeCombined += $snapshot.Length / 1024 / 1024 / 1024	
		}
		$SSSizeCombined = [math]::round($SSSizeCombined,2)

		$Reihe = New-Object System.Object
		$Reihe | Add-Member -type NoteProperty -name VMName -value $VMName
		$Reihe | Add-Member -type NoteProperty -name SnapshotName -value $Name 
		$Reihe | Add-Member -type NoteProperty -name Erstellt -value $AddedTime 
		$Reihe | Add-Member -type NoteProperty -name AlleSnapshotsGB -value $SSSizeCombined
		$Reihe | Add-Member -type NoteProperty -name Beschreibung -value $Description
		$Tabelle += $Reihe
	}
}



if ($Tabelle){
	$MailBody += Convert-Table2HTML -Table ($Tabelle | sort VMName) -NoteProperties "VMName,SnapshotName,Erstellt,AlleSnapshotsGB,Beschreibung" | Out-String
	Send-MailMessage -From $MailFrom -To $MailTo -Subject $MailSubject -Body $MailBody -SmtpServer $MailSMTPServer -BodyAsHTML
}