forked from alanrenouf/vCheck-vSphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vCheck.ps1
322 lines (283 loc) · 12.7 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
param ([Switch]$config, $Outputpath, $GlobalVariableFile)
###############################
# vCheck - Daily Error Report #
###############################
# Thanks to all who have commented on my blog to help improve this project
# all beta testers and previous contributors to this script.
#
# Added code for Database exports of results
$Version = "6.17"
$a = $host.PrivateData
#
# Grab the Warning foreground and background colors the user configured
#
$Warning_Foreground = $a.WarningForegroundColor
$Warning_Background = $a.WarningBackgroundColor
function Write-CustomOut ($Details){
$LogDate = Get-Date -Format T
Write-Host "$($LogDate) $Details"
#write-eventlog -logname Application -source "Windows Error Reporting" -eventID 12345 -entrytype Information -message "vCheck: $Details"
}
Function Get-ID-String ($file_content,$ID_name) {
if ($file_content | Select-String -Pattern "\$+$ID_name\s*=")
{
# Write-CustomOut "${ID_name}: ", $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 [array]( $Title, $Ver, $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) {
} Else {
$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 speach 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)
$ProviderName = "VMWareVCheck"
$PluginsFolder = $ScriptPath + "\Plugins\"
$Plugins = Get-ChildItem -Path $PluginsFolder -filter "*.ps1" | Sort Name
If($GlobalVariableFile)
{$GlobalVariables = $GlobalVariableFile}
else
{$GlobalVariables = $ScriptPath + "\GlobalVariables.ps1"}
$file = Get-Content $GlobalVariables
$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) {
cls
Write-Host -foreground $Warning_Foreground -background $Warning_Background
Write-Host -foreground $Warning_Foreground -background $Warning_Background "Welcome to vCheck by Virtu-Al http://virtu-al.net"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "================================================="
Write-Host -foreground $Warning_Foreground -background $Warning_Background "This is the first time you have run this script or you have re-enabled the setup wizard."
Write-Host -foreground $Warning_Foreground -background $Warning_Background
Write-Host -foreground $Warning_Foreground -background $Warning_Background "To re-run this wizard in the future please use vCheck.ps1 -Config"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "To define a path to store each vCheck report please use vCheck.ps1 -Outputpath C:\tmp"
Write-Host -foreground $Warning_Foreground -background $Warning_Background
Write-Host -foreground $Warning_Foreground -background $Warning_Background "Please complete the following questions or hit Enter to accept the current setting"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "After completing ths wizard the vCheck report will be displayed on the screen."
Write-Host -foreground $Warning_Foreground -background $Warning_Background
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" , "SQLExport", "Date")
foreach($vcvar in $vcvars) {
if (!($(Get-Variable -Name "$vcvar" -erroraction 'silentlycontinue'))) {
Write-Host "Variable `$$vcvar is not defined in GlobalVariables.ps1" -foregroundcolor "Red"
Write-Host "Press any key to exit ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Exit
}
}
# If $SQLExport is required, check that Connection String file has been created and test connection to DB
If($SQLExport -eq $true)
{
$SqlSCripts = $ScriptPath + "\SaveToDatabase.ps1"
. $SqlSCripts
$SQLStringPath = $ScriptPath + "\SqlConnectionString.bin"
If (!(Test-Path $SQLStringPath -ErrorAction SilentlyContinue))
{
# Prompt for SQL Connection String and encrypt
Write-Host -foreground $Warning_Foreground -background $Warning_Background "Save To Database v1.0 by Alan van Wyk"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "==================================="
Write-Host -foreground $Warning_Foreground -background $Warning_Background "This is the first time DB backup has been requested"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "In order for succesful backup to DB, you'll need to provide an SQL connection string"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "The account writing to DB will require write access to the tables generated"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "If the tables do not exist, the account will need the rights to create the relevant tables"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "This connection string will be encrypted and only the account that creates the config"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "file will be able to decrypt it"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "In order to reset this, simply delete the file: $SQLStringPath"
Write-Host -foreground $Warning_Foreground -background $Warning_Background
Write-Host -foreground $Warning_Foreground -background $Warning_Background "This will be stored (encrypted) at:"
Write-Host -foreground $Warning_Foreground -background $Warning_Background "For help with connection strings, see: http://www.connectionstrings.com/sql-server-2012/"
$stringIn = read-host "Please a provide valid Connection String"
Encrypt-String -string $stringIn -path $SQLStringPath
}
}
$StylePath = $ScriptPath + "\Styles\" + $Style
if(!(Test-Path ($StylePath))) {
# The path is not valid
# Use the default style
Write-Debug "Style path ($($StylePath)) is not valid"
$StylePath = $ScriptPath + "\Styles\Default"
Write-Debug "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)
$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 {
$IDinfo = Get-PluginID $_.Fullname
$Title = $IDinfo[0]
$Ver = $IDinfo[1]
$Author = $IDinfo[2]
Write-CustomOut "..start calculating $Title by $Author v$Ver"
$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 "..finished calculating $Title by $Author v$Ver"
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
$MyReport += Get-CustomHeaderClose
}
If($SQLExport -eq $true)
{
Write-CustomOut "..writing $Title to database by Alan van Wyk v1.0"
Export-ToVCheckDB -InputObject $Details -ProviderType $ProviderName -Description $Title -InstanceName $Server -CreateTableIfDoesNotExist -ConnectionString (Get-EncryptedString -path $SQLStringPath)
}
}
}
$MyReport += Get-CustomHeader ("This report took " + [math]::round(((Get-Date) - $Date).TotalMinutes,2) + " minutes to run all checks.") "The following plugins took longer than $PluginSeconds seconds to run, there may be a way to optimize these or remove them if not needed"
$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
if ($DisplayToScreen -or $SetupSetting) {
Write-CustomOut "..Displaying HTML results"
$Filename = $Env:TEMP + "\" + $Server + "vCheck" + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + ".htm"
$MyReport | out-file -encoding ASCII -filepath $Filename
Invoke-Item $Filename
}
if ($SendAttachment) {
$Filename = $Env:TEMP + "\" + $Server + "vCheck" + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + ".htm"
$MyReport | out-file -encoding ASCII -filepath $Filename
}
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"
$MyReport | out-file -encoding ASCII -filepath $Filename
}
if ($SendEmail) {
Write-CustomOut "..Sending Email"
If ($SendAttachment) {
send-Mailmessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPSRV -Body "vCheck attached to this email" -Attachments $Filename
} Else {
send-Mailmessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPSRV -Body $MyReport -BodyAsHtml
}
}
if ($SendAttachment -eq $true -and $DisplaytoScreen -ne $true) {
Write-CustomOut "..Removing temporary file"
Remove-Item $Filename -Force
}
$End = $ScriptPath + "\EndScript.ps1"
. $End