In IT we don’t like breaking changes in our software. But sometimes you do need to break an egg to make an omelet. Standing still is ultimately moving backwards.
In the most recent PowerCLI Release (v6.3 R1) such a change was introduced for the Get-EsxCli cmdlet. With the ingenious introduction of the V2 switch this is not yet a breaking change, but you should be aware that the “old” way of using Get-EsxCli will ultimately go away.
In the VMware vSphere PowerCLI Reference: Automating vSphere Administration, 2nd Edition, we included a script (Chapter 15, Listing 15-2), that allowed you to create a handy Reference Chart of the available methods under the Get-EsxCli cmdlet. This post provides an update to that script for V2.
The Script
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 |
function Get-EsxCliCommand { <# .SYNOPSIS Returns all available namespaces through the Get-EsxCli object .DESCRIPTION The Get-EsxCli cmdlet returns an object that can be used to access all properties and methods that are available. This function retuns all available methods in thes namespaces, together with a short help text and the method's Parameters. .NOTES Source: Original: Automating vSphere Administration, 2nd Edition Author: Luc Dekens .ParamETER VMHost The host for which to list the esxcli namespaces .Parameter V2 Switch to use the EsxCli object version 2, that was introduced in PowerCLI 6.3 Release 1 .EXAMPLE Get-EsxCliCommand -VMHost $esx -V2 #> [CmdLetBinding()] Param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [PSObject]$VMHost, [Switch]$V2 ) Process{ if($V2){ $esxcli = Get-EsxCli -VMHost $VMHost -V2 $cmd = {$esxcli.esxcli.command.list.Invoke()} } else{ $esxcli = Get-EsxCli -VMHost $VMHost $cmd = {$esxcli.esxcli.command.list()} } Invoke-Command -ScriptBlock $cmd | Foreach-Object { $steps = $_.NameSpace.Split('.') + $_.Command if($V2){ $stepsHelp = $_.NameSpace.Split('.') + $_.Command } else{ $stepsHelp = $_.NameSpace.Split('.') + 'help' } # Get the namespace object $nSpace = $esxcli $steps | Foreach-Object { $nSpace = $($nSpace.$($_)) } if($V2){ $nSpace = $($nSpace.Invoke) } # Get the namespace Help object $helpObject = $esxcli $stepsHelp | Foreach-Object { $helpObject = $($helpObject.$($_)) } # Fetch the help for the method if($helpObject){ if($V2){ $helpObjectelp = $helpObject.Help() } else{ $helpObjectelp = $helpObject.Invoke($_.Command) } } else{ $helpObjectelp = $null } # Method details $_ | Select @{N='Command';E={ "`$esxcli.$($_.NameSpace).$($_.Command)"}}, @{N='Syntax/Parameter';E={"$($nSpace.Value.ToString())"}}, @{N='Help';E={$helpObjectelp.Help}} # Parameter details if($helpObject){ $helpObjectelp.Param | Select @{N='Command';E={''}}, @{N='Syntax/Parameter';E={$_.DisplayName}}, @{N='Help';E={$_.Help}} } } } } |
Annotations
Line 26: This version of the function introduces a new switch, named V2, that allows you to select which mode to use, V1 or V2
Line 32: The V2 version makes use of the Invoke() method to call the methods available in the different namespaces.
Line 39: To be able to use a single loop for V1 and V2, the script uses the Invoke-Command cmdlet. The actual codeblock used depends on the setting of the V2 switch
Line 54: Another adaptation to use the V2 Invoke() method
Line 68: Same as for line 32 and 54, the Help function is called via the Invoke() method
Sample Usage
To create a Reference Chart that holds the V1 and V2 syntax of the different namespaces and available methods, the following sample uses Doug Finke‘s ImportExcel module.
This ImportExcel module is an indispensable tool if you need to do reporting in your PowerShell scripts. And not just for tabular data, the charting feature and the conditional formatting feature make this one of more valuable modules available in the PowerShell Gallery! And there is a lot more available!
1 2 3 4 |
$fileName = 'C:\Users\lucd\Scripts\esxcli.xlsx' Get-EsxCliCommand -VMHost esx1.local.lab | Export-Excel -Path $fileName -WorkSheetname 'V1' -FreezeTopRow -AutoSize Get-EsxCliCommand -VMHost esx1.local.lab -V2 | Export-Excel -Path $fileName -WorkSheetname 'V2' -FreezeTopRow -AutoSize |
The resulting worksheet look like this.
The V1 XRef
The V2 XRef
The important differences that were introduced with V2 are
- the way the methods are called. This happens with the Invoke() method.
- the way the parameters are passed. This is done with a HashTable, so no more positional parameters
Enjoy!
Joshua
I am attempting to make a GUI using powercli. I seem to be having issue with the following block of code. I have Checkbox’s set for applied methods but does not seem to take when I use in a function. However, it works for me when I manually enter in each value to the table. Can you point me in the right direction?
Function RemoveVib {
$vmhost = $objectlist.SelectedItem.ToString()
$vib = $dd_vib.SelectedItem.ToString()
if ($vmhost.count -eq 0 -or $vmhost.count -gt 1) {$output.AppendText(“Select Host and try again.
r
n”)}elseif ($vib.count -eq 0) {$output.AppendText(“Select VIBs and try again.
r
n”)}else {$output.AppendText(“Removing selected VIB…”)
$esxcli = Get-EsxCli -V2 -VMHost $vmhost
$arguments = $esxcli.software.vib.remove.CreateArgs()
$arguments.vibname = $vib
$arguments.maintenancemode = $false
if ($cb_force.checked -eq $true) {$arguments.force = $true}
elseif ($cb_dryrun.Checked -eq $true) {$arguments.dryrun = $true}
elseif ($cb_maint.Checked -eq $true) {$arguments.maintenancemode = $true}
else {$output.AppendText(“No methods selected
r
n”)}$esxcli.software.vib.remove.invoke($arguments)
$output.Appendtext(“Complete!
r
n”)$output.AppendText(“A reboot may be required.
r
n”)}
}