-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmreport.ps1
182 lines (149 loc) · 6.47 KB
/
vmreport.ps1
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#Script written by Mark Bernhardt
#This script will give a basic overview of your VM environment exported into
#a plain text file: report.txt.
#It will also collect a list of "hot" items that may warrant attention in a
#separate hotlist.txt.
#You will need to run get-vc prior to running script.
#TimeStamp for calculating total run time.
$start = Get-Date;
#Supress Error Messages
$erroractionpreference = "SilentlyContinue";
#Status Message
"Generating Reports..."
"VM Environment Report: " + (Get-Date) > report.txt;
"" >> report.txt;
"" >> report.txt;
#Status Message
"Cluster Resources..."
#Resource Usage By Cluster
foreach ($c in get-cluster | sort-object `
@{expression={$_.name};ascending=$true}) `
{'==================== ' + $c.name + ' ====================' >> report.txt; `
#Cluster-Wide Stats (CPU & Mem)
($c | select `
@{name='Cluster CPU Average %';expression={"{0,21:#.00}" -f ($_ | get-vmhost | `
get-stat -stat cpu.usage.average -intervalmin 120 -maxsamples 360 | `
measure-object value -ave).average}}, `
@{name='Cluster Mem Average %';expression={"{0,21:#.00}" -f ($_ | get-vmhost | `
get-stat -stat mem.usage.average -intervalmin 120 -maxsamples 360 | `
measure-object value -ave).average}} `
) >> report.txt; `
#Individual Host Stats (CPU & Mem)
($c | get-vmhost | sort-object @{expression={$_.name};ascending=$true} | `
select name, `
@{name='Host CPU Average %';expression={"{0,18:#.00}" -f ($_ | `
get-stat -stat cpu.usage.average -intervalmin 120 -maxsamples 360 | `
measure-object value -ave).average}}, `
@{name='Host Mem Average %';expression={"{0,18:#.00}" -f ($_ | `
get-stat -stat mem.usage.average -intervalmin 120 -maxsamples 360 | `
measure-object value -ave).average}} `
) >> report.txt; `
#VM to ESX Host Ratio (Powered On VM's Only)
($c | select `
@{name='VMs per Host';expression={"{0,12:#.00}" -f ((($c | get-vm | `
where-object {$_.powerstate -eq "PoweredOn"}).count) / (($c | `
get-vmhost).count))}} `
) >> report.txt; `
#Blank spaces between clusters
"" >> report.txt; `
"" >> report.txt; `
"" >> report.txt; `
};
#Status Message
"Datastore Overview..."
#Datastore Overview. Total Capacity, Free Space, Space used by Powered Off VM's.
"Datastore Overview:" >> report.txt;
#NOTE: VM space is marked as being in kb's but is actually mb's.
(get-datacenter | select name, `
@{name='Total Capacity(GB)';expression={"{0,18:#.00}" -f ((get-datastore | `
Measure-Object -Sum -Property capacitymb).sum/1gb)}}, `
@{name='Free Space(GB)';expression={"{0,14:#.00}" -f ((get-datastore | `
Measure-Object -Sum -Property freespacemb).sum/1gb)}}, `
@{name='Powered Off VMs(GB)';expression={"{0,19:#.00}" -f ((get-vm | `
where-object {$_.PowerState -eq "PoweredOff"} | get-harddisk | `
Measure-Object -Sum -Property capacitykb).sum/1mb)}} `
) >> report.txt;
"" >> report.txt;
"" >> report.txt;
#Datastore Usage (AvailableGB, CapacityGB, % Used)
get-datastore | sort-object @{expression={$_.name};ascending=$true} | `
select name, `
@{name='Available(GB)';expression={"{0,13:#.00}" -f ($_.freespacemb/1gb)}}, `
@{name='Capacity(GB)';expression={"{0,12:#.00}" -f ($_.capacitymb/1gb)}}, `
@{name='% Used';expression={"{0,6:#.00}" -f `
((($_.capacitymb - $_.freespacemb)/$_.capacitymb) * 100)}} `
>> report.txt;
#Status Message
"Generating Hotlist..."
#This section creates a hotlist.txt report for VM's and Hosts that are
#running "hot".
#This also includes Datastores that are nearly full.
"VM Environment Hot List: " + (Get-Date) > hotlist.txt;
"" >> hotlist.txt;
"" >> hotlist.txt;
#Status Message
" VMs..."
#VM's that average over 75% memory usage.
"Virtual Machines Over 75% Average Memory Usage:" >> hotlist.txt;
(get-vm | sort-object @{expression={$_.name};ascending=$true} | `
Where-Object {($_ | get-stat -mem -intervalmin 120 -maxsamples 360 | `
Measure-Object value -ave).average -gt 10} | select name, memorymb, `
@{name='VM Mem Usage';expression={"{0,9:#.00}" -f (($_ | `
get-stat -mem -intervalmin 120 -maxsamples 360 | `
Measure-Object value -ave).average)}} `
) >> hotlist.txt;
"" >> hotlist.txt;
#Status Message
" Hosts..."
#Hosts that average over 75% memory usage.
"ESX Hosts Over 75% Average Memory Usage:" >> hotlist.txt;
(get-vmhost | sort-object @{expression={$_.name};ascending=$true} | `
Where-Object {($_ | `
get-stat -stat mem.usage.average -intervalmin 120 -maxsamples 360 | `
Measure-Object value -ave).average -gt 10} | select name, `
@{name='Host Mem Average %';expression={"{0,18:#.00}" -f ($_ | `
get-stat -stat mem.usage.average -intervalmin 120 -maxsamples 360 | `
measure-object value -ave).average}} `
) >> hotlist.txt;
"" >> hotlist.txt;
#Hosts that average over 75% cpu usage.
"ESX Hosts Over 75% Average CPU Usage:" >> hotlist.txt;
(get-vmhost | sort-object @{expression={$_.name};ascending=$true} | `
Where-Object {($_ | `
get-stat -stat cpu.usage.average -intervalmin 120 -maxsamples 360 | `
Measure-Object value -ave).average -gt 10} | select name, `
@{name='Host CPU Average %';expression={"{0,18:#.00}" -f ($_ | `
get-stat -stat cpu.usage.average -intervalmin 120 -maxsamples 360 | `
measure-object value -ave).average}} `
) >> hotlist.txt;
"" >> hotlist.txt;
#Status Message
" Datastores..."
#Datastores with less than 10% free.
"Datastores with less than 10% free:" >> hotlist.txt;
(get-datastore | sort-object @{expression={$_.name};ascending=$true} | `
Where-Object {(($_.freespacemb / $_.capacitymb) * 100) -lt 10} | select name, `
@{name='Available(GB)';expression={"{0,13:#.00}" -f ($_.freespacemb/1gb)}}, `
@{name='Capacity(GB)';expression={"{0,12:#.00}" -f ($_.capacitymb/1gb)}}, `
@{name='% Used';expression={"{0,6:#.00}" -f `
((($_.capacitymb - $_.freespacemb)/$_.capacitymb) * 100)}}`
) >> hotlist.txt;
"" >> hotlist.txt;
"" >> hotlist.txt;
#Status Message
"Checking ESX Logs..."
#This section will dump ESX vmkwarning messages since the previous day.
"ESX Hosts VMKWarnings since " + `
(get-date (get-date).AddDays(-1) -f MMMdd) + ":" >> hotlist.txt;
foreach ($h in get-vmhost -server $vcserver | `
sort-object @{expression={$_.name};ascending=$true}) `
{(get-log -host $h vmkwarning).entries | `
select-string -pattern ( `
("{0,0} {1,2}" -f (get-date (get-date).AddDays(-1) -f MMM), `
(get-date (get-date).AddDays(-1) -f %d)) `
-or ("{0,0} {1,2}" -f (get-date -f MMM),(get-date -f %d))) `
>> hotlist.txt `
};
#Status Message
"Completed in " + ("{0,2:#.00}" -f `
((Get-Date).Subtract($start).totalminutes)) + " Minutes.";