miked
miked Working the technologies in local government.

CSV Email Report


CSV Email Report

One of the things that Hyper-V and VMM don’t give in the GUI sense is a report of CSV usage. I imagine you can get there with SCOM but it seems to be more hassle than it’s worth (though, I’d love to be proven wrong on that). While we do use SCOM to inform us when CSV pass our alerting thresholds, I was looking for a way to provide a picture of CSV usage that we could actually use.

Enter the CSV Email Report PowerShell script. I wrote this to email us a report on Friday before heading into the weekend to give the on call person a head’s up of potential issues. The next step is to take the HTML this script generates and put it behind a web server so we can reference ad hoc. Just need to change a couple lines of code and that will be in place.

This script does highlight table rows based on my thresholds, but should be pretty easy to figure out how to change for you. I appreciate any feedback. And, happy scripting!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#  Title: Email CSV info Script 
    Author: Mike Dunphy
    Description: Script to email CSV usage by cluster. Fill in variables as noted in script
    License: GPL 3.0
#

ipmo failoverclusters -erroraction stop

#email variables
$emlTo = "To email address"
$emlFrom = "From email address"
$emlSubject = "Email subject"
$emlSMTP = "SMTP Server"

$Header = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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>
<title>
CSV report 
</title>
"@
$Pre = @" 
<h2>CSV Report - Sent Fridays at 8AM</h2> 
<body>
<table>
<tr><th style="width:25%">Name</th><th style="width:25%">FreeSpace(GB)</th><th style="width:25%">PercentFree</th><th style="width:25%">Size(GB)</th></tr>
"@
$Post = "</table><h3>Report run at: $(get-date) on $env:computername </h3></body></html>"
$csvInfo = @()

#fill with your clusternames or create an array called $clusters programmatically
$clusters = @("cluster1","cluster2") 

foreach ($cluster in $clusters)
{
	
    $csvs = Get-ClusterSharedVolume -Cluster $cluster
    foreach ($csv in $csvs) 
        {   
			
            $csvPart = Get-ClusterSharedVolume -cluster $cluster -name $csv.name | select -ExpandProperty SharedVolumeInfo|select -ExpandProperty Partition
            $1CSV = new-object psobject -Property @{
                                        Cluster = $cluster
                                        Name = $csv.Name
                                        Size = $csvPart.Size
                                        UsedSpace = $csvPart.Usedspace
                                        FreeSpace = $csvPart.FreeSpace
                                        PercentFree = $csvPart.PercentFree
                                        }
   
        $csvInfo += $1CSV
        }

}


$csvoutput = $csvInfo|sort freespace|select cluster,name,@{ Label ="FreeSpace(GB)" ; Expression = { "{0:N2}" -f ($_.FreeSpace/1024/1024/1024) }},percentfree,@{ Label = "Size(GB)" ; Expression = { "{0:N2}" -f ($_.Size/1024/1024/1024) } }|group cluster -AsHashTable -AsString
$body = $null
$body = $Header
$body += $Pre

foreach ($mykey in $csvoutput.keys)
{
    
    $i = 1
    while ($i -gt 0)
    {
        $body += '<tr><th colspan="4" align="center"> ' + $myKey + ' </th></tr>' + "`n"
        $i=0
    }
    
    foreach ($thing in $csvoutput.$mykey)
    {
        if ([int]$thing.'FreeSpace(GB)' -le 1024)
		{
			$body += "<tr bgcolor='Red'>"
		}
		elseif ([int]$thing.'FreeSpace(GB)' -gt 1024 -and [int]$thing.'FreeSpace(GB)' -le 2048)
		{
			$body += "<tr bgcolor='Yellow'>"
		}
		else
		{
			$body += "<tr>"
		}
        $body += "<td>" + $thing.name + "</td>`n"
        $body += "<td>" + $thing.'FreeSpace(GB)' + "</td>`n"
        $body += "<td>" + $thing.PercentFree + "</td>`n"
        $body += "<td>" + $thing.'Size(GB)' + "</td>`n"
        $body += "</tr>"
    }
    
}

$body += $Post

Send-MailMessage -to $emlTo -from $emlFrom -Subject $emlSubject -Body $Body -BodyAsHtml -SmtpServer $emlSMTP


 

comments powered by Disqus