forked from alanrenouf/vCheck-vSphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vCheck.ps1
336 lines (291 loc) · 11.3 KB
/
vCheck.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<#
.SYNOPSIS
vCheck is a PowerShell HTML framework script, designed to run as a scheduled
task before you get into the office to present you with key information via
an email directly to your inbox in a nice easily readable format.
.DESCRIPTION
vCheck Daily Report for vSphere
vCheck is a PowerShell HTML framework script, the script is designed to run
as a scheduled task before you get into the office to present you with key
information via an email directly to your inbox in a nice easily readable format.
This script picks on the key known issues and potential issues scripted as
plugins for various technologies written as powershell scripts and reports
it all in one place so all you do in the morning is check your email.
One of they key things about this report is if there is no issue in a particular
place you will not receive that section in the email, for example if there are
no datastores with less than 5% free space (configurable) then the disk space
section in the virtual infrastructure version of this script, it will not show
in the email, this ensures that you have only the information you need in front
of you when you get into the office.
This script is not to be confused with an Audit script, although the reporting
framework can also be used for auditing scripts too. I dont want to remind you
that you have 5 hosts and what there names are and how many CPUs they have each
and every day as you dont want to read that kind of information unless you need
it, this script will only tell you about problem areas with your infrastructure.
.NOTES
File Name : vCheck.ps1
Author : Alan Renouf - @alanrenouf
Version : 6.17
Thanks to all who have commented on my blog to help improve this project
all beta testers and previous contributors to this script.
.LINK
http://www.virtu-al.net/vcheck-pluginsheaders/vcheck
.LINK
https://github.com/alanrenouf/vCheck-vSphere/
.INPUTS
No inputs required
.OUTPUTS
HTML formatted email, Email with attachment, HTML File
.PARAMETER config
If this switch is set, run the setup wizard
.PARAMETER Outputpath
This parameter specifies the output location for files.
#>
param (
[Switch]$config,
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[string]$Outputpath
)
$Version = "6.17"
function Write-CustomOut ($Details){
$LogDate = Get-Date -Format T
Write-Host "$($LogDate) $Details"
}
Function Get-ID-String ($file_content,$ID_name) {
if ($file_content | Select-String -Pattern "\$+$ID_name\s*=") {
$value = (($file_content | Select-String -pattern "\$+${ID_name}\s*=").toString().split("=")[1]).Trim(' "')
return ( $value )
}
}
Function Get-PluginID ($Filename){
# Get the identifying information for a plugin script
# Write-Host "Filename: $Filename"
$file = Get-Content $Filename
$Title = Get-ID-String $file "Title"
if ( !$Title ) { $Title = $Filename }
$PluginVersion = Get-ID-String $file "PluginVersion"
$Author = Get-ID-String $file "Author"
$Ver = "{0:N1}" -f $PluginVersion
# Write-Host "Title: $Title, PluginVersion: $PluginVersion, Ver: $Ver, Author: $Author"
return @{"Title"=$Title; "Version"=$Ver; "Author"=$Author }
}
Function Invoke-Settings ($Filename, $GB) {
$file = Get-Content $filename
$OriginalLine = ($file | Select-String -Pattern "# Start of Settings").LineNumber
$EndLine = ($file | Select-String -Pattern "# End of Settings").LineNumber
if (!(($OriginalLine +1) -eq $EndLine)) {
$Array = @()
$Line = $OriginalLine
do {
$Question = $file[$Line]
$Line ++
$Split= ($file[$Line]).Split("=")
$Var = $Split[0]
$CurSet = $Split[1]
# Check if the current setting is in speech marks
$String = $false
if ($CurSet -match '"') {
$String = $true
$CurSet = $CurSet.Replace('"', '')
}
$NewSet = Read-Host "$Question [$CurSet]"
If (-not $NewSet) {
$NewSet = $CurSet
}
If ($String) {
$Array += $Question
$Array += "$Var=`"$NewSet`""
} Else {
$Array += $Question
$Array += "$Var=$NewSet"
}
$Line ++
} Until ( $Line -ge ($EndLine -1) )
$Array += "# End of Settings"
$out = @()
$out = $File[0..($OriginalLine -1)]
$out += $array
$out += $File[$Endline..($file.count -1)]
if ($GB) { $out[$SetupLine] = '$SetupWizard =$False' }
$out | Out-File $Filename
}
}
# Add all global variables.
$ScriptPath = (Split-Path ((Get-Variable MyInvocation).Value).MyCommand.Path)
$PluginsFolder = $ScriptPath + "\Plugins\"
$Plugins = Get-ChildItem -Path $PluginsFolder -filter "*.ps1" | Sort Name
$GlobalVariables = $ScriptPath + "\GlobalVariables.ps1"
$file = Get-Content $GlobalVariables
# Setup language hashtable
Import-LocalizedData -BaseDirectory ($ScriptPath + "\lang") -BindingVariable lang
$Setup = ($file | Select-String -Pattern '# Set the following to true to enable the setup wizard for first time run').LineNumber
$SetupLine = $Setup ++
$SetupSetting = Invoke-Expression (($file[$SetupLine]).Split("="))[1]
if ($config) {
$SetupSetting = $true
}
If ($SetupSetting) {
Clear-Host
($lang.GetEnumerator() | where {$_.Name -match "setupMsg[0-9]*"} | Sort-Object Name) | Foreach {
Write-Host -foreground $host.PrivateData.WarningForegroundColor -background $host.PrivateData.WarningBackgroundColor $_.value
}
Invoke-Settings -Filename $GlobalVariables -GB $true
Foreach ($plugin in $Plugins) {
Invoke-Settings -Filename $plugin.Fullname
}
}
. $GlobalVariables
$vcvars = @("SetupWizard" , "Server" , "SMTPSRV" , "EmailFrom" , "EmailTo" , "EmailSubject", "DisplaytoScreen" , "SendEmail" , "SendAttachment" , "Colour1" , "Colour2" , "TitleTxtColour" , "TimeToRun" , "PluginSeconds" , "Style" , "Date")
foreach($vcvar in $vcvars) {
if (!($(Get-Variable -Name "$vcvar" -Erroraction 'SilentlyContinue'))) {
Write-Error ($lang.varUndefined -f $vcvar)
}
}
$StylePath = $ScriptPath + "\Styles\" + $Style
if(!(Test-Path ($StylePath))) {
# The path is not valid
# Use the default style
Write-Warning "Style path ($($StylePath)) is not valid"
$StylePath = $ScriptPath + "\Styles\Default"
Write-Warning "Using $($StylePath)"
}
# Import the Style
. ("$($StylePath)\Style.ps1")
Function Get-Base64Image ($Path) {
$pic = Get-Content $Path -Encoding Byte
[Convert]::ToBase64String($pic)
}
Function Get-CustomHTML ($Header, $HeaderImg){
$Report = $HTMLHeader -replace "_HEADER_", $Header
$Report = $Report -replace "_HEADERIMG_", $HeaderImg
Return $Report
}
Function Get-CustomHeader0 ($Title){
$Report = $CustomHeader0 -replace "_TITLE_", $Title
Return $Report
}
Function Get-CustomHeader ($Title, $Comments){
$Report = $CustomHeaderStart -replace "_TITLE_", $Title
If ($Comments) {
$Report += $CustomheaderComments -replace "_COMMENTS_", $Comments
}
$Report += $CustomHeaderEnd
Return $Report
}
Function Get-CustomHeaderClose{
$Report = $CustomHeaderClose
Return $Report
}
Function Get-CustomHeader0Close{
$Report = $CustomHeader0Close
Return $Report
}
Function Get-CustomHTMLClose{
$Report = $CustomHTMLClose
Return $Report
}
Function Get-HTMLTable {
param([array]$Content, [array]$FormatRules)
# If format rules are specified
if ($FormatRules) {
# Use an XML object for ease of use
$XMLTable = [xml]($content | ConvertTo-Html -Fragment)
$XMLTable.table.RemoveChild($XMLTable.table.colgroup) | out-null
# Check each cell to see if there are any format rules
for ($RowN = 1; $RowN -lt $XMLTable.table.tr.count; $RowN++) {
for ($ColN = 0; $ColN -lt $XMLTable.table.tr[$RowN].td.count; $ColN++) {
if ( $Tableformat.keys -contains $XMLTable.table.tr[0].th[$ColN]) {
# Current cell has a rule, test to see if they are valid
foreach ( $rule in $Tableformat[$XMLTable.table.tr[0].th[$ColN]] ) {
if ( Invoke-Expression ("`$XMLTable.table.tr[`$RowN].td[`$ColN] {0}" -f [string]$rule.Keys) ) {
# Find what to
$RuleScope = ([string]$rule.Values).split(",")[0]
$RuleActions = ([string]$rule.Values).split(",")[1].split("|")
switch ($RuleScope) {
"Row" { $XMLTable.table.tr[$RowN].SetAttribute($RuleActions[0], $RuleActions[1]) }
"Cell" { $XMLTable.table.tr[$RowN].selectSingleNode("td[$($ColN+1)]").SetAttribute($RuleActions[0], $RuleActions[1]) }
}
}
}
}
}
}
return [string]($XMLTable.OuterXml)
}
else {
$HTMLTable = $Content | ConvertTo-Html -Fragment
$HTMLTable = $HTMLTable -Replace '<TABLE>', $HTMLTableReplace
$HTMLTable = $HTMLTable -Replace '<td>', $HTMLTdReplace
$HTMLTable = $HTMLTable -Replace '<th>', $HTMLThReplace
$HTMLTable = $HTMLTable -replace '<', '<'
$HTMLTable = $HTMLTable -replace '>', '>'
Return $HTMLTable
}
}
Function Get-HTMLDetail ($Heading, $Detail){
$Report = ($HTMLDetail -replace "_Heading_", $Heading) -replace "_Detail_", $Detail
Return $Report
}
# Adding all plugins
$TTRReport = @()
$MyReport = Get-CustomHTML "$Server vCheck"
$MyReport += Get-CustomHeader0 ($Server)
$Plugins | Foreach {
$TableFormat = $null
$IDinfo = Get-PluginID $_.Fullname
Write-CustomOut ($lang.pluginStart -f $IDinfo["Title"], $IDinfo["Author"], $IDinfo["Version"])
$TTR = [math]::round((Measure-Command {$Details = . $_.FullName}).TotalSeconds, 2)
$TTRTable = "" | Select Plugin, TimeToRun
$TTRTable.Plugin = $_.Name
$TTRTable.TimeToRun = $TTR
$TTRReport += $TTRTable
$ver = "{0:N1}" -f $PluginVersion
Write-CustomOut ($lang.pluginEnd -f $IDinfo["Title"], $IDinfo["Author"], $IDinfo["Version"])
If ($Details) {
If ($Display -eq "List"){
$MyReport += Get-CustomHeader $Header $Comments
$AllProperties = $Details | Get-Member -MemberType Properties
$AllProperties | Foreach {
$MyReport += Get-HTMLDetail ($_.Name) ($Details.($_.Name))
}
$MyReport += Get-CustomHeaderClose
}
If ($Display -eq "Table") {
$MyReport += Get-CustomHeader $Header $Comments
$MyReport += Get-HTMLTable $Details $TableFormat
$MyReport += Get-CustomHeaderClose
}
}
}
$MyReport += Get-CustomHeader ($lang.repTime -f [math]::round(((Get-Date) - $Date).TotalMinutes,2)) ($lang.slowPlugins -f $PluginSeconds)
$TTRReport = $TTRReport | Where { $_.TimeToRun -gt $PluginSeconds } | Sort-Object TimeToRun -Descending
$TTRReport | Foreach {$MyReport += Get-HTMLDetail $_.Plugin $_.TimeToRun}
$MyReport += Get-CustomHeaderClose
$MyReport += Get-CustomHeader0Close
$MyReport += Get-CustomHTMLClose
# Save the file somewhere, depending on report options
if ($Outputpath) {
$DateHTML = Get-Date -Format "yyyyMMddHH"
$ArchiveFilePath = $Outputpath + "\Archives\" + $VIServer
if (-not (Test-Path -PathType Container $ArchiveFilePath)) { New-Item $ArchiveFilePath -type directory | Out-Null }
$Filename = $ArchiveFilePath + "\" + $VIServer + "_vCheck_" + $DateHTML + ".htm"
}
else {
$Filename = $Env:TEMP + "\" + $Server + "vCheck" + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + ".htm"
}
# Create the file
$MyReport | Out-File -encoding ASCII -filepath $Filename
if ($DisplayToScreen -or $SetupSetting) {
Write-CustomOut $lang.HTMLdisp
Invoke-Item $Filename
}
if ($SendEmail) {
Write-CustomOut $lang.emailSend
If ($SendAttachment) {
Send-Mailmessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPSRV -Body $lang.emailAtch -Attachments $Filename
} Else {
Send-Mailmessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPSRV -Body $MyReport -BodyAsHtml
}
}
# Run EndScript once everything else is complete
. ($ScriptPath + "\EndScript.ps1")