-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNew-PowerCLI_ESXCLIv2_Function.ps1
364 lines (301 loc) · 16.8 KB
/
New-PowerCLI_ESXCLIv2_Function.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
function new-PowerCLI_ESXCLIv2_Function{
param(
$ToAnalyse
)
process{
$FullNameSpace = $ToAnalyse.Fullname -replace "vim.", ""
$ShortName = $ToAnalyse.Name
#write-Host "$FullName AND $ShortName"
#Line below will be executed only once for the esxcliv2 "root object" because it doesn't have a function "childelements" and no methods defined at this level.
if($ToAnalyse.gettype().fullname -eq 'VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliImpl'){
$ToAnalyse | Get-Member -MemberType CodeProperty | foreach-object{
$CodePropertyName = $_.Name
new-PowerCLI_ESXCLIv2_Function -ToAnalyse $ToAnalyse.$CodePropertyName
}
}
Else{
#/////Identify all methods associated to this namespace, and generate a PowerCLI Function for each of them.
$ToAnalyse.methods | foreach-object{
$MethodName = $_.name
#Write-host "MethodName $MethodName"
$FunctionName = $MethodName + "-" + $FullNameSpace
$Global:Stream.WriteLine("function $FunctionName{")#Start of the function
#////Help section
$Global:Stream.WriteLine('<#')
$Global:Stream.WriteLine('.SYNOPSIS')
$MethodHelp = $ToAnalyse.$MethodName.help().help
$Global:Stream.WriteLine($MethodHelp)
$Global:Stream.WriteLine("")
$Global:Stream.WriteLine('.DESCRIPTION')
$Global:Stream.WriteLine('This function provide access via Power-Cli to the esxcli equivalent function.')
$Global:Stream.WriteLine('All parameters and help associated to the original esxcli function are available.')
$Global:Stream.WriteLine('This function is based on the original get-esxcli -v2 PowerCLI cmdlet')
$Global:Stream.WriteLine('A PowerCli VMHost object is a mandatory parameter')
$Global:Stream.WriteLine('It is also possible to pipe VMhost objects to execute this function accross many hosts in one operation')
$Global:Stream.WriteLine('')
$Global:Stream.WriteLine('.NOTES')
$Global:Stream.WriteLine('Author: Christophe Calvet')
$Global:Stream.WriteLine('Blog: http://www.thecrazyconsultant.com/get-esxcli_on_steroids')
if($ToAnalyse.$MethodName.help().param){
$ToAnalyse.$MethodName.help().param | foreach-object{
$Global:Stream.WriteLine('')
$DisplayName = $_.DisplayName
#Fix an issue with some variable that have already a meaning in PowerShell.
If(($DisplayName -eq "host") -or ($DisplayName -eq "profile") -or ($DisplayName -eq "version") -or ($DisplayName -eq "debug")){
$DisplayName = $DisplayName + "2"
}
#The parameter that have "-" in the name is esxcli, do not have it in get-esxcli
$DisplayNameFixed = $DisplayName -replace "-",""
$SringParameter = ".PARAMETER " + $DisplayNameFixed
$Global:Stream.WriteLine($SringParameter)
$ParameterHelp = $_.Help
$Global:Stream.WriteLine($ParameterHelp)
}
}
$Global:Stream.WriteLine('')
$Global:Stream.WriteLine(".PARAMETER VMHost")
$Global:Stream.WriteLine("One or many PowerCli VMHost object")
$Global:Stream.WriteLine('')
$Global:Stream.WriteLine('.EXAMPLE')
$Global:Stream.WriteLine('Some examples are available in the blog')
$Global:Stream.WriteLine('#>')
#Add the cmdlebinding for better debug
$NewOutputWithTab = "`t" +'[CmdletBinding()]'
$Global:Stream.WriteLine($NewOutputWithTab)
#////Parameter section
$NewOutputWithTab = "`t" +'param('
$Global:Stream.WriteLine($NewOutputWithTab)
if($ToAnalyse.$MethodName | gm | where {$_.Name -eq 'CreateArgs'}){
$Args = $ToAnalyse.$MethodName.CreateArgs()
$Args.getEnumerator() | foreach-object{
$Key = $_.Key
#Fix an issue with some variable that have already a meaning in PowerShell.
If(($Key -eq "host") -or ($Key -eq "profile") -or ($Key -eq "version") -or ($Key -eq "debug")){
$Key = $Key + "2"
}
$Value = $_.Value
switch ($value){
'Unset, ([boolean])'{
#Write-host "TEST1" -foreground green -background yellow
$ParameterMandatory = "`t" + '[Parameter(Mandatory=$true)]'
$Global:Stream.WriteLine($ParameterMandatory)
$ParameterLine = "`t" + '[boolean]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([boolean], optional)'{
#Write-host "TEST2" -foreground green -background yellow
$ParameterLine = "`t" + '[boolean]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([long])'{
#Write-host "TEST3" -foreground green -background yellow
$ParameterMandatory = "`t" + '[Parameter(Mandatory=$true)]'
$Global:Stream.WriteLine($ParameterMandatory)
$ParameterLine = "`t" + '[long]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([long], optional)'{
#Write-host "TEST4" -foreground green -background yellow
$ParameterLine = "`t" + '[long]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([string[]])'{
#Write-host "TEST5" -foreground green -background yellow
$ParameterMandatory = "`t" + '[Parameter(Mandatory=$true)]'
$Global:Stream.WriteLine($ParameterMandatory)
$ParameterLine = "`t" + '[string[]]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([string[]], optional)'{
#Write-host "TEST6" -foreground green -background yellow
$ParameterLine = "`t" + '[string[]]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([string])'{
#Write-host "TEST7" -foreground green -background yellow
$ParameterMandatory = "`t" + '[Parameter(Mandatory=$true)]'
$Global:Stream.WriteLine($ParameterMandatory)
$ParameterLine = "`t" + '[string]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
'Unset, ([string], optional)'{
#Write-host "TEST8" -foreground green -background yellow
$ParameterLine = "`t" + '[string]$'+ $Key + ','
$Global:Stream.WriteLine($ParameterLine)
}
}
}
}
$NewOutputWithTab = "`t" +'[Parameter(Mandatory=$true,ValueFromPipeline=$true)]'
$Global:Stream.WriteLine($NewOutputWithTab)
$NewOutputWithTab = "`t" +'[VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]]$VMhost'
$Global:Stream.WriteLine($NewOutputWithTab)
$NewOutputWithTab = "`t" + ')'
$Global:Stream.WriteLine($NewOutputWithTab)
#////Process section
$NewOutputWithTab = "`t" +'process{'
$Global:Stream.WriteLine($NewOutputWithTab)
$NewOutputWithTab = "`t" + 'foreach($SelectedVMHost in $VMhost){' #Handle case of using many host as parameters and not as pipe
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'Try{'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + '$esxcliv2 = Get-EsxCLI -VMHost $SelectedVMHost -V2'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'Catch{'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'Write-error "Not able to get-esxcli for $(($SelectedVMHost).name)"'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'continue'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
$Global:Stream.WriteLine("")
$NewOutputWithTab = "`t`t" + 'if($esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + '){'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + '#Namespace available for this ESXi host'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'Else{'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'Write-error "The namespace esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + ' is not available for $(($SelectedVMHost).name)"'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + 'continue'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
$Global:Stream.WriteLine("")
#Two scenario below, some parameters have been identified or none
if($ToAnalyse.$MethodName | gm | where {$_.Name -eq 'CreateArgs'}){
$NewOutputWithTab = "`t`t`t" + 'if($esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + ' | gm | where {$_.Name -eq ''CreateArgs''}){'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t" + '#To anticipate scenario with commands that didn''t have any parameters in a previous build'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t" + '#More details about this challenge in the blog'
$Global:Stream.WriteLine("$NewOutputWithTab")
#/// Create the hashtable
$NewOutputWithTab = "`t`t`t" + '$HashTable = $esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + '.CreateArgs()'
$Global:Stream.WriteLine("$NewOutputWithTab")
#$NewOutputWithTab = "`t`t`t" + '$CheckAllParameters = $true'
#$Global:Stream.WriteLine("$NewOutputWithTab")
#/// Check if any parameters have been used to call the function
#If it is the case, update the relevant item in the hashtable
#Moreover if a parameter IS NOT in the hashtable, throw an error.
#If someone was planning to use a parameter, better to not execute the command than executing it without taking it into account
#Didn't manage to store the enumarator in a variable previously to reuse it here
if($ToAnalyse.$MethodName | gm | where {$_.Name -eq 'CreateArgs'}){
$Args = $ToAnalyse.$MethodName.CreateArgs()
$Args.getEnumerator() | foreach-object{
$Key = $_.Key
$OriginalKey = $Key
#Fix an issue with some variable that have already a meaning in PowerShell.
If(($Key -eq "host") -or ($Key -eq "profile") -or ($Key -eq "version") -or ($Key -eq "debug")){
$Key = $Key + "2"
}
$NewOutputWithTab = "`t`t`t`t" + 'if($PSBoundParameters.ContainsKey(''' + $Key + ''')){'
$Global:Stream.WriteLine("$NewOutputWithTab")
#For each parameter we check that it is available in the hashtable for this build and we update the value accordingly
$NewOutputWithTab = "`t`t`t`t`t" + 'if($HashTable.containskey('''+ $OriginalKey + ''')){'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t`t`t" + '$HashTable.' + $OriginalKey + ' = $' + $Key
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
#If it nos the case, an error is generated
$NewOutputWithTab = "`t`t`t`t`t" + 'Else{'
$Global:Stream.WriteLine("$NewOutputWithTab")
#$NewOutputWithTab = "`t`t`t`t`t`t" + '$CheckAllParameters = $False'
#$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t`t`t" + 'Write-error "The parameter ' + $Key + ' is not available for $(($SelectedVMHost).name)"'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t`t`t" + 'continue'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
}
$Global:Stream.WriteLine("")
#$FullCommand = "`t`t`t`t" + 'if($CheckAllParameters){'
#$Global:Stream.WriteLine($FullCommand)
$FullCommand = "`t`t`t`t`t" + '$esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + '.invoke($hashtable)'
$Global:Stream.WriteLine($FullCommand)
#$FullCommand = "`t`t`t`t" + '}'
#$Global:Stream.WriteLine($FullCommand)
}
$NewOutputWithTab = "`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
#Scenario where we will work with a previous build of ESXi that don't have any parameters for this command
$NewOutputWithTab = "`t`t`t" + 'Else{'
$Global:Stream.WriteLine("$NewOutputWithTab")
#Every parameter will not be compatible with this build
$AllParameters = "Start"
if($ToAnalyse.$MethodName | gm | where {$_.Name -eq 'CreateArgs'}){
$Args = $ToAnalyse.$MethodName.CreateArgs()
$Args.getEnumerator() | foreach-object{
$Key = $_.Key
#Fix an issue with some variable that have already a meaning in PowerShell.
If(($Key -eq "host") -or ($Key -eq "profile") -or ($Key -eq "version") -or ($Key -eq "debug")){
$Key = $Key + "2"
}
$AllParameters = $AllParameters + ' -or $PSBoundParameters.ContainsKey(''' + $Key + ''')'
}
$AllParameters = $AllParameters.replace('Start -or ','')
$NewOutputWithTab = "`t`t`t`t" + 'if(' + $AllParameters + '){'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t`t" + 'Write-error "No parameters are available for $(($SelectedVMHost).name)"'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
}
$NewOutputWithTab = "`t`t`t`t" + 'Else{'
$Global:Stream.WriteLine("$NewOutputWithTab")
$FullCommand = "`t`t`t`t`t" + '$esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + '.invoke()'
$Global:Stream.WriteLine($FullCommand)
$NewOutputWithTab = "`t`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t`t`t" + '}'
$Global:Stream.WriteLine("$NewOutputWithTab")
}
Else{
$FullCommand = "`t`t`t" + '$esxcliv2.' + ($ToAnalyse.Fullname).replace("vim.EsxCLI.", "") + '.' + $MethodName + '.invoke()'
$Global:Stream.WriteLine($FullCommand)
}
$NewOutputWithTab = "`t`t" + '}' #End of the foreach($SelectedVMHost -in $VMhost)
$Global:Stream.WriteLine("$NewOutputWithTab")
$NewOutputWithTab = "`t" +'}'
$stream.WriteLine($NewOutputWithTab) #End of process
$Global:Stream.WriteLine('}')#End of the function
$Global:Stream.WriteLine('')
}
#/////End of Identify all methods associated to this namespace
#/////Go through all child elements if any
#However it is necessary to isolate a bug with EsxCLI.vsan
#Fix bug for vsan path. It is possible to use codeproperty as a workaround here because there are no methods defined at this level.
if(($FullNameSpace -eq 'EsxCLI.vsan')){
#Write-host "In the special case" -foreground blue
$ToAnalyse | Get-Member -MemberType CodeProperty | foreach-object{
$CodePropertyName = $_.Name
new-PowerCLI_ESXCLIv2_Function -ToAnalyse $ToAnalyse.$CodePropertyName
}
}
Else{
$ToAnalyse.childelements | foreach-object{
$ChildElementName = $_.name
#Write-host "ChildElementName $ChildElementName"
new-PowerCLI_ESXCLIv2_Function -ToAnalyse $ToAnalyse.$ChildElementName
}
}
#/////End of Go through all child elements if any
}
}
}
#How to use it
#$MyHost = get-vmhost "10.0.0.101"
#$esxcli = get-esxcli -v2 -vmhost $MyHost
#$Global:Stream.close()
#$Global:Stream = [System.IO.StreamWriter] "C:\temp\Get-EsxCLI_on_steroids_21-07-2016.ps1"
#new-PowerCLI_ESXCLIv2_Function -ToAnalyse $esxcli
#$Global:Stream.close()