A closer look at Get-EsxCli V2

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.

esxcli-v2In 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

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!

The resulting worksheet look like this.

The V1 XRef

esxcli-v1-xref

The V2 XRef

esxcli-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!

One Comment

    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.rn”)}
    elseif ($vib.count -eq 0) {$output.AppendText(“Select VIBs and try again.rn”)}
    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 selectedrn”)}
    $esxcli.software.vib.remove.invoke($arguments)
    $output.Appendtext(“Complete!rn”)
    $output.AppendText(“A reboot may be required.rn”)
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.