forked from alanrenouf/vCheck-vSphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vCheckUtils.ps1
1003 lines (831 loc) · 34.1 KB
/
vCheckUtils.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$global:vCheckPath = $MyInvocation.MyCommand.Definition | Split-Path
$global:pluginXMLURL = "https://vcheck.report/plugins.xml"
$global:pluginURL = "https://raw.github.com/alanrenouf/vCheck-{0}/master/Plugins/{1}/{2}"
<#
.SYNOPSIS
Retrieves installed vCheck plugins and available plugins from the Virtu-Al.net repository.
.DESCRIPTION
Get-vCheckPlugin parses your vCheck plugins folder, as well as searches the online plugin respository in Virtu-Al.net.
After finding the plugin you are looking for, you can download and install it with Add-vCheckPlugin. Get-vCheckPlugins
also supports finding a plugin by name. Future version will support categories (e.g. Datastore, Security, vCloud)
.PARAMETER name
Name of the plugin.
.PARAMETER proxy
URL for proxy usage.
.PARAMETER proxy_user
username for proxy auth.
.PARAMETER proxy_password
password for proxy auth.
.PARAMETER proxy_domain
domain for proxy auth.
.EXAMPLE
Get list of all vCheck Plugins
Get-vCheckPlugin
.EXAMPLE
Get plugin by name
Get-vCheckPlugin PluginName
.EXAMPLE
Get plugin by name using proxy
Get-vCheckPlugin PluginName -proxy "http://127.0.0.1:3128"
.EXAMPLE
Get plugin by name using proxy with auth (domain optional depending on your proxy auth)
Get-vCheckPlugin PluginName -proxy "http://127.0.0.1:3128" -proxy_user "username" -proxy_pass "password -proxy_domain "domain"
.EXAMPLE
Get plugin information
Get-vCheckPlugins PluginName
#>
function Get-vCheckPlugin
{
[CmdletBinding()]
Param
(
[Parameter(mandatory=$false)] [String]$name,
[Parameter(mandatory=$false)] [String]$proxy,
[Parameter(mandatory=$false)] [String]$proxy_user,
[Parameter(mandatory=$false)] [String]$proxy_pass,
[Parameter(mandatory=$false)] [String]$proxy_domain,
[Parameter(mandatory=$false)] [Switch]$installed,
[Parameter(mandatory=$false)] [Switch]$notinstalled,
[Parameter(mandatory=$false)] [Switch]$pendingupdate,
[Parameter(mandatory=$false)] [String]$category
)
Process
{
$pluginObjectList = @()
foreach ($localPluginFile in (Get-ChildItem -Path $vCheckPath\Plugins\* -Include *.ps1, *.ps1.disabled -Recurse))
{
$localPluginContent = Get-Content $localPluginFile
if ($localPluginContent | Select-String -SimpleMatch "title")
{
$localPluginName = ($localPluginContent | Select-String -SimpleMatch "Title").toString().split("`"")[1]
}
if($localPluginContent | Select-String -SimpleMatch "description")
{
$localPluginDesc = ($localPluginContent | Select-String -SimpleMatch "description").toString().split("`"")[1]
}
elseif ($localPluginContent | Select-String -SimpleMatch "comments")
{
$localPluginDesc = ($localPluginContent | Select-String -SimpleMatch "comments").toString().split("`"")[1]
}
if ($localPluginContent | Select-String -SimpleMatch "author")
{
$localPluginAuthor = ($localPluginContent | Select-String -SimpleMatch "author").toString().split("`"")[1]
}
if ($localPluginContent | Select-String -SimpleMatch "PluginVersion")
{
$localPluginVersion = @($localPluginContent | Select-String -SimpleMatch "PluginVersion")[0].toString().split(" ")[-1]
}
if ($localPluginContent | Select-String -SimpleMatch "PluginCategory")
{
$localPluginCategory = @($localPluginContent | Select-String -SimpleMatch "PluginCategory")[0].toString().split("`"")[1]
}
$pluginObject = New-Object PSObject
$pluginObject | Add-Member -MemberType NoteProperty -Name name -value $localPluginName
$pluginObject | Add-Member -MemberType NoteProperty -Name description -value $localPluginDesc
$pluginObject | Add-Member -MemberType NoteProperty -Name author -value $localPluginAuthor
$pluginObject | Add-Member -MemberType NoteProperty -Name version -value $localPluginVersion
$pluginObject | Add-Member -MemberType NoteProperty -Name category -Value $localPluginCategory
$pluginObject | Add-Member -MemberType NoteProperty -Name status -value "Installed"
$pluginObject | Add-Member -MemberType NoteProperty -Name location -Value $LocalpluginFile.FullName
$pluginObjectList += $pluginObject
}
if (!$installed)
{
try
{
$webClient = new-object system.net.webclient
if ($proxy)
{
$proxyURL = new-object System.Net.WebProxy $proxy
if (($proxy_user) -and ($proxy_pass))
{
$proxyURL.UseDefaultCredentials = $false
$proxyURL.Credentials = New-Object Net.NetworkCredential("$proxy_user","$proxy_pass")
}
elseif (($proxy_user) -and ($proxy_pass) -and ($proxy_domain))
{
$proxyURL.UseDefaultCredentials = $false
$proxyURL.Credentials = New-Object Net.NetworkCredential("$proxy_user","$proxy_pass","$proxy_domain")
}
else
{
$proxyURL.UseDefaultCredentials = $true
}
$webclient.proxy = $proxyURL
}
$response = $webClient.openread($pluginXMLURL)
$streamReader = new-object system.io.streamreader $response
[xml]$plugins = $streamReader.ReadToEnd()
foreach ($plugin in $plugins.pluginlist.plugin)
{
$pluginObjectList | Where-Object {$_.name -eq $plugin.name -and [double]$_.version -lt [double]$plugin.version}|
Foreach-Object {
$_.status = "New Version Available - " + $plugin.version
}
if (!($pluginObjectList | Where-Object {$_.name -eq $plugin.name}))
{
$pluginObject = New-Object PSObject
$pluginObject | Add-Member -MemberType NoteProperty -Name name -value $plugin.name
$pluginObject | Add-Member -MemberType NoteProperty -Name description -value $plugin.description
$pluginObject | Add-Member -MemberType NoteProperty -Name author -value $plugin.author
$pluginObject | Add-Member -MemberType NoteProperty -Name version -value $plugin.version
$pluginObject | Add-Member -MemberType NoteProperty -Name category -Value $plugin.category
$pluginObject | Add-Member -MemberType NoteProperty -Name status -value "Not Installed"
$pluginObject | Add-Member -MemberType NoteProperty -name location -value $plugin.href
$pluginObjectList += $pluginObject
}
}
}
catch [System.Net.WebException]
{
write-error $_.Exception.ToString()
return
}
}
if ($name){
$pluginObjectList | Where-Object {$_.name -eq $name}
} Else {
if ($category){
$pluginObjectList | Where-Object {$_.Category -eq $category}
} Else {
if($notinstalled){
$pluginObjectList | Where-Object {$_.status -eq "Not Installed"}
} elseif($pendingupdate) {
$pluginObjectList | Where-Object {$_.status -like "New Version Available*"}
}
Else {
$pluginObjectList
}
}
}
}
}
<#
.SYNOPSIS
Installs a vCheck plugin from the Virtu-Al.net repository.
.DESCRIPTION
Add-vCheckPlugin downloads and installs a vCheck Plugin (currently by name) from the Virtu-Al.net repository.
The downloaded file is saved in your vCheck plugins folder, which automatically adds it to your vCheck report. vCheck plugins may require
configuration prior to use, so be sure to open the ps1 file of the plugin prior to running your next report.
.PARAMETER name
Name of the plugin.
.EXAMPLE
Install via pipeline from Get-vCheckPlugins
Get-vCheckPlugin "Plugin name" | Add-vCheckPlugin
.EXAMPLE
Install Plugin by name
Add-vCheckPlugin "Plugin name"
#>
function Add-vCheckPlugin
{
[CmdletBinding(DefaultParametersetName="name")]
Param
(
[Parameter(parameterSetName="name",Position=0,mandatory=$true)] [String]$name,
[Parameter(parameterSetName="object",Position=0,mandatory=$true,ValueFromPipeline=$true)] [PSObject]$pluginobject
)
Process
{
if($name)
{
Get-vCheckPlugin $name | Add-vCheckPlugin
}
elseif ($pluginObject)
{
Add-Type -AssemblyName System.Web
$filename = $pluginObject.location.split("/")[-2,-1] -join "/"
$filename = [System.Web.HttpUtility]::UrlDecode($filename)
try
{
Write-Warning "Downloading File..."
$webClient = new-object system.net.webclient
$webClient.DownloadFile($pluginObject.location,"$vCheckPath\Plugins\$filename")
Write-Warning "The plugin `"$($pluginObject.name)`" has been installed to $vCheckPath\Plugins\$filename"
Write-Warning "Be sure to check the plugin for additional configuration options."
}
catch [System.Net.WebException]
{
write-error $_.Exception.ToString()
return
}
}
}
}
<#
.SYNOPSIS
Removes a vCheck plugin.
.DESCRIPTION
Remove-vCheckPlugin Uninstalls a vCheck Plugin.
Basically, just looks for the plugin name and deletes the file. Sure, you could just delete the ps1 file from the plugins folder, but what fun is that?
.PARAMETER name
Name of the plugin.
.EXAMPLE
Remove via pipeline
Get-vCheckPlugin "Plugin name" | Remove-vCheckPlugin
.EXAMPLE
Remove Plugin by name
Remove-vCheckPlugin "Plugin name"
#>
function Remove-vCheckPlugin
{
[CmdletBinding(DefaultParametersetName="name",SupportsShouldProcess=$true,ConfirmImpact="High")]
Param
(
[Parameter(parameterSetName="name",Position=0,mandatory=$true)] [String]$name,
[Parameter(parameterSetName="object",Position=0,mandatory=$true,ValueFromPipeline=$true)] [PSObject]$pluginobject
)
Process
{
if($name)
{
Get-vCheckPlugin $name | Remove-vCheckPlugin
}
elseif ($pluginObject)
{
Remove-Item -path $pluginObject.location -confirm:$false
}
}
}
<#
.SYNOPSIS
Geberates plugins XML file from local plugins
.DESCRIPTION
Designed to be run after plugin changes are commited, in order to generate
the plugin.xml file that the plugin update check uses.
.PARAMETER outputFile
Path to the xml file. Defaults to temp directory
#>
function Get-vCheckPluginXML
{
param
(
$outputFile = "$($env:temp)\plugins.xml"
)
# create XML and root node
$xml = New-Object xml
$root = $xml.CreateElement("pluginlist")
[void]$xml.AppendChild($root)
foreach ($localPluginFile in (Get-ChildItem -Path $vCheckPath\Plugins\* -Include *.ps1 -Recurse))
{
$localPluginContent = Get-Content $localPluginFile
if ($localPluginContent | Select-String -SimpleMatch "title")
{
$localPluginName = ($localPluginContent | Select-String -SimpleMatch "Title").toString().split("`"")[1]
}
if($localPluginContent | Select-String -SimpleMatch "description")
{
$localPluginDesc = ($localPluginContent | Select-String -SimpleMatch "description").toString().split("`"")[1]
}
elseif ($localPluginContent | Select-String -SimpleMatch "comments")
{
$localPluginDesc = ($localPluginContent | Select-String -SimpleMatch "comments").toString().split("`"")[1]
}
if ($localPluginContent | Select-String -SimpleMatch "author")
{
$localPluginAuthor = ($localPluginContent | Select-String -SimpleMatch "author").toString().split("`"")[1]
}
if ($localPluginContent | Select-String -SimpleMatch "PluginVersion")
{
$localPluginVersion = @($localPluginContent | Select-String -SimpleMatch "PluginVersion")[0].toString().split(" ")[-1]
}
if ($localPluginContent | Select-String -SimpleMatch "PluginCategory")
{
$localPluginCategory = @($localPluginContent | Select-String -SimpleMatch "PluginCategory")[0].toString().split("`"")[1]
}
$pluginXML = $xml.CreateElement("plugin")
$elem=$xml.CreateElement("name")
$elem.InnerText=$localPluginName
[void]$pluginXML.AppendChild($elem)
$elem=$xml.CreateElement("description")
$elem.InnerText=$localPluginDesc
[void]$pluginXML.AppendChild($elem)
$elem=$xml.CreateElement("author")
$elem.InnerText=$localPluginAuthor
[void]$pluginXML.AppendChild($elem)
$elem=$xml.CreateElement("version")
$elem.InnerText=$localPluginVersion
[void]$pluginXML.AppendChild($elem)
$elem=$xml.CreateElement("category")
$elem.InnerText=$localPluginCategory
[void]$pluginXML.AppendChild($elem)
$elem=$xml.CreateElement("href")
$elem.InnerText= ($pluginURL -f $localPluginCategory, $localPluginFile.Directory.Name, $localPluginFile.Name)
[void]$pluginXML.AppendChild($elem)
[void]$root.AppendChild($pluginXML)
}
$xml.save($outputFile)
}
<#
.SYNOPSIS
Returns settings from vCheck plugins.
.DESCRIPTION
Get-PluginSettings will return an array of settings contained
within a supplied plugin. Used by Export-vCheckSettings.
.PARAMETER filename
Full path to plugin file
#>
Function Get-PluginSettings {
Param
(
[Parameter(mandatory=$true)] [String]$filename
)
$psettings = @()
$file = Get-Content $filename
$OriginalLine = ($file | Select-String -SimpleMatch "# Start of Settings").LineNumber
$EndLine = ($file | Select-String -SimpleMatch "# End of Settings").LineNumber
if (!(($OriginalLine +1) -eq $EndLine)) {
$Line = $OriginalLine
do {
$Question = $file[$Line]
$Line++
$Split = ($file[$Line]).Split("=")
$Var = $Split[0]
$CurSet = $Split[1]
$settings = @{}
$settings.filename = $filename
$settings.question = $Question
$settings.varname = $Var.Trim()
$settings.var = $CurSet.Trim()
$currentsetting = New-Object -TypeName PSObject -Prop $settings
$psettings += $currentsetting
$Line++
} Until ( $Line -ge ($EndLine -1) )
}
$psettings
}
<#
.SYNOPSIS
Applies settings to vCheck plugins.
.DESCRIPTION
Set-PluginSettings will apply settings supplied to a given vCheck plugin.
Used by Export-vCheckSettings.
.PARAMETER filename
Full path to plugin file
.PARAMETER settings
Array of settings to apply to plugin
.PARAMETER GB
Switch to disable Setup Wizard when processing GlobalVariables.ps1
#>
Function Set-PluginSettings {
Param
(
[Parameter(mandatory=$true)] [String]$filename,
[Parameter(mandatory=$false)] [Array]$settings,
[Parameter(mandatory=$false)] [Switch]$GB
)
$file = Get-Content $filename
$OriginalLine = ($file | Select-String -SimpleMatch "# Start of Settings").LineNumber
$EndLine = ($file | Select-String -SimpleMatch "# End of Settings").LineNumber
$PluginName = ($filename.split("\")[-1]).split(".")[0]
Write-Warning "`nProcessing - $PluginName"
if (!(($OriginalLine +1) -eq $EndLine)) {
$Array = @()
$Line = $OriginalLine
do {
$Question = $file[$Line].Trim()
$Found = $false
$Line ++
$Split= ($file[$Line]).Split("=")
$Var = $Split[0].Trim()
$CurSet = $Split[1].Trim()
Foreach ($setting in $settings) {
If ($question -eq $setting.question.Trim()) {
$NewSet = $setting.var
$Found = $true
}
}
If (!$Found) {
# Check if the current setting is in speech marks
$String = $false
if ($CurSet -match '"') {
$String = $true
$CurSet = $CurSet.Replace('"', '').Trim()
}
$NewSet = Read-Host "$Question [$CurSet]"
If (-not $NewSet) {
$NewSet = $CurSet
}
If ($String) {
$NewSet = "`"$NewSet`""
}
}
if ($NewSet -ne $CurSet) {
Write-Warning "Plugin setting changed:"
Write-Warning " Plugin: $PluginName"
Write-Warning " Question: $Question"
Write-Warning " Variable: $Var"
Write-Warning " Old Value: $CurSet"
Write-Warning " New Value: $NewSet"
}
$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) {
$Setup = ($file | Select-String -SimpleMatch '# Set the following to true to enable the setup wizard for first time run').LineNumber
$SetupLine = $Setup ++
$out[$SetupLine] = '$SetupWizard = $False'
}
$out | Out-File -Encoding ASCII $filename
}
}
<#
.SYNOPSIS
Retrieves configured vCheck plugin settings and exports them to CSV.
.DESCRIPTION
Export-vCheckSettings will retrieve the settings from each plugin and export them to a CSV file.
By default, the CSV file will be created in the vCheck folder named vCheckSettings.csv.
You can also specify a custom path using -outfile.
Once the export has been created the settings can then be imported via Import-vCheckSettings.
.PARAMETER outfile
Full path to CSV file
.EXAMPLE
Export-vCheckSettings
Creates vCheckSettings.csv file in default location (vCheck folder)
.EXAMPLE
Export-vCheckSettings -outfile "E:\vCheck-vCenter01.csv"
Creates CSV file in custom location E:\vCheck-vCenter01.csv
#>
Function Export-vCheckSettings {
Param
(
[Parameter(mandatory=$false)] [String]$outfile = "$vCheckPath\vCheckSettings.csv"
)
$Export = @()
$GlobalVariables = "$vCheckPath\GlobalVariables.ps1"
$Export = Get-PluginSettings -Filename $GlobalVariables
Foreach ($plugin in (Get-ChildItem -Path $vCheckPath\Plugins\* -Include *.ps1, *.ps1.disabled -Recurse)) {
$Export += Get-PluginSettings -Filename $plugin.Fullname
}
$Export | Select-Object filename, question, var | Export-Csv -NoTypeInformation $outfile
}
<#
.SYNOPSIS
Retrieves configured vCheck plugin settings and exports them to XML.
.DESCRIPTION
Export-vCheckSettings will retrieve the settings from each plugin and export them to a XML file.
By default, the XML file will be created in the vCheck folder named vCheckSettings.xml.
You can also specify a custom path using -outfile.
Once the export has been created the settings can then be imported via Import-vCheckSettingsXML.
.PARAMETER outfile
Full path to XML file
.EXAMPLE
Export-vCheckSettings
Creates vCheckSettings.xml file in default location (vCheck folder)
.EXAMPLE
Export-vCheckSettingsXML -outfile "E:\vCheck-vCenter01.xml"
Creates XML file in custom location E:\vCheck-vCenter01.xml
#>
Function Export-vCheckSettingsXML {
Param
(
[Parameter(mandatory=$false)] [String]$outfile = "$vCheckPath\vCheckSettings.xml"
)
$Export = @()
$GlobalVariables = "$vCheckPath\GlobalVariables.ps1"
$Export = Get-PluginSettings -Filename $GlobalVariables
Foreach ($plugin in (Get-ChildItem -Path $vCheckPath\Plugins\* -Include *.ps1 -Recurse)) {
$Export += Get-PluginSettings -Filename $plugin.Fullname
}
$xml = "<vCheck>`n"
foreach ($e in $Export) {
$xml += "`t<setting>`n"
$xml += "`t`t<filename>$($e.Filename)</filename>`n"
$xml += "`t`t<question>$($e.Question)</question>`n"
$xml += "`t`t<varname>$($e.VarName)</varname>`n"
$xml += "`t`t<var>$($e.Var)</var>`n"
$xml += "`t</setting>`n"
}
$xml += "</vCheck>"
$xml | Out-File -FilePath $outfile -Encoding utf8
}
<#
.SYNOPSIS
Retreives settings from CSV and applies them to vCheck.
.DESCRIPTION
Import-vCheckSettings will retrieve the settings exported via Export-vCheckSettings and apply them to the
current vCheck folder.
By default, the CSV file is expected to be located in the vCheck folder named vCheckSettings.csv.
You can also specify a custom path using -csvfile.
If the CSV file is not found you will be asked to provide the path.
The Setup Wizard will be disabled.
You will be asked any questions not found in the export. This would occur for new settings introduced
enabling a quick update between versions.
.PARAMETER csvfile
Full path to CSV file
.EXAMPLE
Import-vCheckSettings
Imports settings from vCheckSettings.csv file in default location (vCheck folder)
.EXAMPLE
Import-vCheckSettings -outfile "E:\vCheck-vCenter01.csv"
Imports settings from CSV file in custom location E:\vCheck-vCenter01.csv
#>
Function Import-vCheckSettings {
Param
(
[Parameter(mandatory=$false)] [String]$csvfile = "$vCheckPath\vCheckSettings.csv"
)
If (!(Test-Path $csvfile)) {
$csvfile = Read-Host "Enter full path to settings CSV file you want to import"
}
$Import = Import-Csv $csvfile
$GlobalVariables = "$vCheckPath\GlobalVariables.ps1"
$settings = $Import | Where-Object {($_.filename).Split("\")[-1] -eq ($GlobalVariables).Split("\")[-1]}
Set-PluginSettings -Filename $GlobalVariables -Settings $settings -GB
Foreach ($plugin in (Get-ChildItem -Path $vCheckPath\Plugins\* -Include *.ps1, *.ps1.disabled -Recurse)) {
$settings = $Import | Where-Object {($_.filename).Split("\")[-1] -eq ($plugin.Fullname).Split("\")[-1]}
Set-PluginSettings -Filename $plugin.Fullname -Settings $settings
}
Write-Warning "`nImport Complete!`n"
}
<#
.SYNOPSIS
Retreives settings from XML and applies them to vCheck.
.DESCRIPTION
Import-vCheckSettingsXML will retrieve the settings exported via Export-vCheckSettingsXML, or via .\vCheck.ps1 -GUIConfig
and apply them to the current vCheck folder.
By default, the XML file is expected to be located in the vCheck folder named vCheckSettings.csv.
You can also specify a custom path using -xmlfile.
If the XML file is not found you will be asked to provide the path.
The Setup Wizard will be disabled.
You will be asked any questions not found in the export. This would occur for new settings introduced
enabling a quick update between versions.
.PARAMETER csvfile
Full path to XML file
.EXAMPLE
Import-vCheckSettingsXML
Imports settings from vCheckSettings.xml file in default location (vCheck folder)
.EXAMPLE
Import-vCheckSettingsXML -xmlfile "E:\vCheck-vCenter01.xml"
Imports settings from XML file in custom location E:\vCheck-vCenter01.xml
#>
Function Import-vCheckSettingsXML {
Param
(
[Parameter(mandatory=$false)] [String]$xmlFile = "$vCheckPath\vCheckSettings.xml"
)
If (!(Test-Path $xmlFile)) {
$xmlFile = Read-Host "Enter full path to settings XML file you want to import"
}
$Import = [xml](Get-Content $xmlFile)
$GlobalVariables = "$vCheckPath\GlobalVariables.ps1"
$settings = $Import.vCheck.Setting | Where-Object {($_.filename).Split("\")[-1] -eq ($GlobalVariables).Split("\")[-1]}
Set-PluginSettings -Filename $GlobalVariables -Settings $settings -GB
Foreach ($plugin in (Get-ChildItem -Path "$vCheckPath\Plugins\" -Filter "*.ps1" -Recurse)) {
$settings = $Import.vCheck.Setting | Where-Object {($_.filename).Split("\")[-1] -eq ($plugin.Fullname).Split("\")[-1]}
Set-PluginSettings -Filename $plugin.Fullname -Settings $settings
}
Write-Warning "`nImport Complete!`n"
}
Function Get-vCheckCommand {
Get-Command *vCheck*
}
Get-vCheckCommand
function Schedule-vCheck {
$vCheckJobName = Read-Host -Prompt "Enter the name of the vCheck job to create"
$TriggerTime = Read-Host -Prompt "Enter the time $vCheckJobName should run at, in the format 'H:MM AM/PM' (e.g. '2:00 AM')"
$Location = Read-Host -Prompt "Enter the fully qualified location where the vCheck script resides"
$sb = [scriptblock]::Create($Location)
$dailyTrigger = New-JobTrigger -Daily -At $TriggerTime
$option = New-ScheduledJobOption -StartIfOnBattery -StartIfIdle
Register-ScheduledJob -Name $vCheckJobName -Trigger $dailyTrigger -ScheduledJobOption $option `
-ScriptBlock $sb
}
# Below is a set of functions to make upgrading a vCheck directory easier
<#
.SYNOPSIS
Lists the variables in vCheck plugin files.
.DESCRIPTION
Plugin file will be scanned as a text file and any variables in between the "Start of Settings"
and "End of Settings" section markers will be sent out the pipeline. Files can be sent in
via the pipeline or individually with a loop. If using the "-Verbose" option for troubleshooting
then using a loop is recommended.
.PARAMETER PluginFile
The file to be processed. Can be passed as text, a file object, or a set of files, such as
"Get-ChildItem *.ps1".
.EXAMPLE
Simple
Get-vCheckVariablesSettings -PluginFile "c:\scripts\vcheck6\vcenter\Plugins\20 Cluster\75 DRS Rules.ps1"
Recursed
Get-ChildItem -Path E:\vCheckLatestTesting -File -Filter *.ps1 -Recurse | % { Get-vCheckVariablesSettings -PluginFile $_.FullName }
.INPUTS
System.IO.FileInfo
.OUTPUTS
File Selected.System.Management.Automation.PSCustomObject The 'Fullname' property from the plugin file.
Variable Selected.System.Management.Automation.PSCustomObject The text of the variable assignment from the plugin file.
.NOTES
With multiple vCheck directories to upgrade I needed an easy to pull the variables used
in the old vCheck installation to go into the new version.
.LINK
https://github.com/alanrenouf/vCheck-vSphere
Recent Comment History
20150127 cmonahan Initial release.
#>
Function Get-vCheckVariablesSettings {
[CmdletBinding()]
param (
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)] $PluginFile
)
#begin {
Write-Verbose "Started $PluginFile"
if (Test-Path $PluginFile) {
$PluginFile = Get-ChildItem $PluginFile
$contents = Get-Content $PluginFile
$end = $contents.length }
else { throw "Value passed to File parameter is not a file." }
$i=0
#} # end begin block
#process {
while ( ($i -lt $end) -and ($contents[$i] -notmatch "Start of Settings") ) { $i++ }
while ( ($i -lt $end) -and ($contents[$i] -notmatch "End of Settings") ) {
if ($contents[$i] -match "`=") { "" | Select-Object @{n='File';e={$PluginFile.fullname}},@{n='Variable';e={$contents[$i]}}; $i++ }
else { $i++ }
}
#} #end process block
#end {
Write-Verbose "Ended $PluginFile"
#} #end end block
<#
Recent Comment History
20150127 cmonahan Initial release.
#>
} # end function
<#
.SYNOPSIS
Matches the disabled plugins in a target directory with those in a source directory.
.DESCRIPTION
I wrote it for when I'm upgrading vCheck. This will go through the old directory and
any plugin marked as disabled there will be marked as disabled in the new directory.
.PARAMETER OldVcheckDir
What you you think it is.
.PARAMETER NewVcheckDir
No tricks here.
.EXAMPLE
Sync-vCheckDisabledPlugins -OldVcheckDir c:\scripts\vcheck6\vccenter_old_20150218_163057 -NewVcheckDir c:\scripts\vcheck6\vcenter
.LINK
https://github.com/alanrenouf/vCheck-vSphere
Recent Comment History
20150128 cmonahan Initial release.
#>
function Sync-vCheckDisabledPlugins {
[cmdletbinding(SupportsShouldProcess=$True)]
param (
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)] $OldVcheckDir,
[Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false)] $NewVcheckDir
)
# $WhatIfPreference
$OldVcheckPluginsDir = (Get-ChildItem "$($OldVcheckDir)\Plugins").PsParentPath
$NewVcheckPluginsDir = (Get-ChildItem "$($NewVcheckDir)\Plugins").PsParentPath
$OldDisabled = Get-ChildItem $OldVcheckDir -Recurse | ? { $_ -like "*.disabled" } #| select -First 1
$OldDisabled
foreach ($file in $OldDisabled) {
Get-ChildItem $NewVcheckDir -Recurse | Where-Object { $_ -match $file.Name } | Select-Object FullName
Get-ChildItem $NewVcheckDir -Recurse -Filter $file.BaseName | ForEach-Object { Move-Item -Path $_.FullName -Destination ($_.FullName -replace("ps1","ps1.disabled")) }
}
<# Comment History
20150128 cmonahan Initial release.
#>
} # end function
<#
.SYNOPSIS
Lists the disabled plugins in a target directory.
.DESCRIPTION
Essentially a stripped down version of Sync-vCheckDisabledPlugins I threw it in
in case someone found it useful.
.PARAMETER VcheckDir
What you you think it is.
.EXAMPLE
Get-vCheckDisabledPlugins -VcheckDir c:\scripts\vcheck6\vcenter
.LINK
https://github.com/alanrenouf/vCheck-vSphere
Recent Comment History
20150128 cmonahan Initial release.
#>
function Get-vCheckDisabledPlugins {
param ( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)] $vCheckDir )
Get-ChildItem (Get-ChildItem "$($vCheckDir)\Plugins").PsParentPath -Recurse | ? { $_ -like "*.disabled" } #| select -First 1
<# Comment History
20150128 cmonahan Initial release.
#>
} # end function
<#
.SYNOPSIS
Does most of the work upgrading to a new version of vCheck.
.DESCRIPTION
This function will:
-Backup the current directory by renaming it with the current date
-Copy in the new version from a directory you've specified
-Save all the variable settings to a text file
-Set the same disabled plugins in the new version
-Opens the saved variable settings in Notepad
Then run vCheck.ps1 for the first time to configure it using the
variables listing open in Notepad as a reference.
.PARAMETER CurrentvCheckPath
Directory to be upgraded.
.PARAMETER NewvCheckSource
Location of the new version downloaded from GitHub.
.EXAMPLE
Upgrade-vCheckDirectory -CurrentvCheckPath c:\scripts\vcheck6\vcenter -NewvCheckSource c:\scripts\vcheck6\vcenter\vCheck-vSphere-master
.NOTES
If you have multiple directories and some settings like smtp server
are the same for them all you could upgrade the file(s) in the new
vCheck version directory and they'll be copied out with each upgrade.
This is my process for upgrading vCheck with this function.
1. Extract a new, unmodified version of the vCheck to a directory. For this example "C:\Scripts\vCheck\vCheck-vSphere-master".
2. Load the utility - ". C:\Scripts\vCheck\vCheck-vSphere-master\vCheckUtils.ps1" .
3. Upgrade-vCheckDirectory âCurrentvCheckPath C:\Scripts\vcheck\vcenterprod -NewvCheckSource C:\Scripts\vcheck6\vCheck-vSphere-master
4. The list of plugin variable values is automatically opened in Notepad.
5. Change directory to C:\Scripts\vcheck\vcenterprod .
6. Run vCheck.ps1 . Input all the prompts for variable values with the ones in the file opened by Notepad. For the global variable â $EmailFrom = "[email protected]" â I use my own email address until after I done a test run. Then I change it back to the group email address.
7. After all the variable have been entered vCheck will run.
8. Review the PowerShell console for script errors and the vCheck email report for any problems.
9. If there are not problems set the âEmailFromâ variable in âGlobalVariables.ps1â back to itâs original value.
.LINK
https://github.com/alanrenouf/vCheck-vSphere
Recent Comment History
20150127 cmonahan Initial release.
#>
Function Upgrade-vCheckDirectory {
param (
[Parameter(Position=0,Mandatory=$true)] $CurrentvCheckPath,
[Parameter(Position=1,Mandatory=$true)] $NewvCheckSource
)
function Get-Now { (get-date -uformat %Y%m%d) + "_" + (get-date -uformat %H%M%S) }
$TS = Get-Now # TS means time stamp
# Test that directories exist
if ( !(Test-Path -Path $CurrentvCheckPath) ) { break }
if ( !(Test-Path -Path $NewvCheckSource) ) { break }
$OldvCheckPath = "$($CurrentvCheckPath)_old_$($TS)"
$OldvCheckVariables = "$($OldvCheckPath)\vCheckVariables_$($TS).txt"
# Backup current directory and setup new directory
Move-Item -Path $CurrentvCheckPath -Destination $OldvCheckPath
mkdir $CurrentvCheckPath
robocopy $NewvCheckSource $CurrentvCheckPath /s /e /z /xj /r:2 /w:5 /np
# Save variable settings
Get-ChildItem -Path $OldvCheckPath -Filter *.ps1 -Recurse | % { Get-vCheckVariablesSettings -PluginFile $_.FullName } | Format-Table -AutoSize | Out-File -FilePath $OldvCheckVariables
# Make the disabled plugins match
Sync-vCheckDisabledPlugins -OldVcheckDir $OldvCheckPath -NewVcheckDir $CurrentvCheckPath
# Configure it
notepad $OldvCheckVariables
Write-Output "Locally on the server hosting the vCheck script run vCheck.ps1"
<# Comment History
20150128 cmonahan Initial release.
#>
} # end function
Function Get-vCheckLogData {
param(
[string] $vCheckFile,
[string] $Section
)
# Find the comment above the specified section table and grab the Post context for 6 lines beyond the comment
# The HTML is stored within the next 6 lines.
# line 1: <div style='height: 10px; font-size: 10px;'> </div>
# line 2: <a name="plugin-#" />
$ContextInfo = Select-String "Plugin Start - $Section" $vCheckFile -context 0,6
# lines 3-6 are the data we want.
$table = $ContextInfo.Context.PostContext | Select-Object -last 4
# The table actually ends on line 7. But line 6 looks like this:
# <tr><td style="text-align: right; background: #FFFFFF"><a href="#top" style="color: black">Back To Top</a>
# There is no ending </td></tr>
# Line 7: </table>
# So add these missing tags back in.
$table += "</td></tr></table>"
try {
# Convert to XML for easier parsing
$xmlObj = [xml]$table
} catch {
# This catches any instances where there are no matches in the file, and then the only data is the ending tags.
# just in case you want to see it, Write-Verbose
Write-Verbose "$vCheckFile : $table"
}
# There is a sub table with the data - so get the TR that contains a sub table
$ParentTR = $xmlObj.table.tr | ? { $_.td.table }
# Get the TD
$ParentTD = $ParentTR.td
# Get the table
$SubTable = $ParentTD.Table
# Use the TH to get the header names
$th = $subTable.tr.th
# Create a hash table that stores all the header names, and use the index as the key. We'll use this as a lookup when we get to the TD
$thHash = @{}
for ($i=0;$i -lt $th.count; $i++) {
$thHash.Add($i,$th[$i])
}
# Loop through each TR containing the log data
for ($i=1; $i -lt $subTable.tr.count; $i++) {
# Get the TDs under the TR, and loop through those
$td = $subTable.tr[$i].td
# build a hash table pulling the column name from the TH hash table, and the value from the TD
$tdHash = @{}
for ($j=0; $j -lt $td.count;$j++) {
$tdHash.Add($thHash[$j],$td[$j])
}
# Return this as an object