forked from alanrenouf/vCheck-vSphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vCheckUtils.ps1
288 lines (217 loc) · 19.2 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
$global:vCheckPath = $MyInvocation.MyCommand.Definition | Split-Path
function Get-VCheckPlugin
{
<#
.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.
.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
Get-VCheckPlugins PluginName
#>
[CmdletBinding()]
Param
(
[Parameter(mandatory=$false)] [String]$name,
[Parameter(mandatory=$false)] [String]$proxy,
[Parameter(mandatory=$false)] [Switch]$installed,
[Parameter(mandatory=$false)] [Switch]$notinstalled,
[Parameter(mandatory=$false)] [String]$category
)
Process
{
$pluginObjectList = @()
foreach ($localPluginFile in (Get-ChildItem $vCheckPath\Plugins\*.ps1))
{
$localPluginContent = Get-Content $localPluginFile
if ($localPluginContent | Select-String -pattern "title")
{
$localPluginName = ($localPluginContent | Select-String -pattern "Title").toString().split("`"")[1]
}
if($localPluginContent | Select-String -pattern "description")
{
$localPluginDesc = ($localPluginContent | Select-String -pattern "description").toString().split("`"")[1]
}
elseif ($localPluginContent | Select-String -pattern "comments")
{
$localPluginDesc = ($localPluginContent | Select-String -pattern "comments").toString().split("`"")[1]
}
if ($localPluginContent | Select-String -pattern "author")
{
$localPluginAuthor = ($localPluginContent | Select-String -pattern "author").toString().split("`"")[1]
}
if ($localPluginContent | Select-String -pattern "PluginVersion")
{
$localPluginVersion = @($localPluginContent | Select-String -pattern "PluginVersion")[0].toString().split(" ")[-1]
}
if ($localPluginContent | Select-String -pattern "PluginCategory")
{
$localPluginCategory = @($localPluginContent | Select-String -pattern "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.name
$pluginObjectList += $pluginObject
}
if (!$installed)
{
try
{
$webClient = new-object system.net.webclient
if ($proxy)
{
$proxyURL = new-object System.Net.WebProxy $proxy
$proxyURL.UseDefaultCredentials = $true
$webclient.proxy = $proxyURL
}
$response = $webClient.openread("http://www.virtu-al.net/vcheck/plugins/plugins.xml")
$streamReader = new-object system.io.streamreader $response
[xml]$plugins = $streamReader.ReadToEnd()
foreach ($plugin in $plugins.pluginlist.plugin)
{
if (!($pluginObjectList | where {$_.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 {$_.name -eq $name}
} Else {
if ($category){
$pluginObjectList | Where {$_.Category -eq $category}
} Else {
if($notinstalled){
$pluginObjectList | where {$_.status -eq "Not Installed"}
} else {
$pluginObjectList
}
}
}
}
}
function Add-VCheckPlugin
{
<#
.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"
#>
[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("/")[-1]
$filename = [System.Web.HttpUtility]::UrlDecode($filename)
try
{
Write-Host "Downloading File..."
$webClient = new-object system.net.webclient
$webClient.DownloadFile($pluginObject.location,"$vCheckPath\Plugins\$filename")
Write-Host -ForegroundColor green "The plugin `"$($pluginObject.name)`" has been installed to $vCheckPath\Plugins\$filename"
Write-Host -ForegroundColor green "Be sure to check the plugin for additional configuration options."
}
catch [System.Net.WebException]
{
write-error $_.Exception.ToString()
return
}
}
}
}
function Remove-VCheckPlugin
{
<#
.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"
#>
[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 ("$vCheckPath\plugins\$($pluginobject.location)") -confirm:$false
}
}
}
Function Get-vCheckCommand {
Get-Command *vCheck*
}
Get-vCheckCommand