How to run ESXCLI commands in PowerShell using the PowerCLI Get-EsxCli cmdlet

feature-image.png
CONTENTS

    If you’re a vSphere admin, you’ll be very familiar with the esxcli command. But have you ever wondered how to run esxcli commands from PowerCLI? Maybe you don’t want to enable SSH on your hosts, or perhaps you need to run an esxcli command in a PowerShell script. The Get-EsxCli cmdlet from the VMware.PowerCLI PowerShell module is the answer!

    In this tutorial, you’ll learn how to create an esxcli interface to a host using PowerCLI, how to investigate the available namespaces, how to invoke esxcli methods, and more! It’ll give you all the information you need to get the most out of the Get-EsxCli cmdlet.

    Prerequisites

    To follow along with this tutorial, you’ll need the following:

    • PowerShell 5.1 or later. This tutorial will use PowerShell 7.1 on a Windows 10 workstation.
    • VMware.PowerCLI version 12.3 or later (For instructions on how to install PowerCLI, see this VMware guide).
    • A vCenter server with at least one managed ESXi host. Both the server and host should be running version 6.5 or later.
    • A general working knowledge of the esxcli command.

    Creating an interface to ESXCLI using Get-EsxCli

    The first step is to create an interface to an ESXi host using the Get-EsxCli cmdlet. This interface information is stored in a variable, which is then used to invoke esxcli methods.

    The following commands will connect you to your vCenter server and store the esxcli interface in a variable ($EsxCli).

    Connect-ViServer -Server "vcenter.example.com"
    $EsxCli = Get-EsxCli -VMHost "esx01.example.com" -V2
    

    You need the V2 switch parameter to use the latest interface version 2. VMware has deprecated interface version 1.

    Traversing the namespace hierarchy

    With the esxcli interface details now stored in $EsxCli, it’s time to look around. To start with, you can simply call the $EsxCli object to view all root namespace elements; for example:

    $EsxCli
    
    Output
    PS C:\> $EsxCli
    
    ===============================
    EsxCli: esx01.example.com
    
       Elements:
       ---------
       device
       elxnet
       esxcli
       fcoe
       graphics
       hardware
       iscsi
       network
       nvme
       rdma
       sched
       software
       storage
       system
       vm
       vsan
    
    Output showing the available root namespace elements.

    Then, using dot (.) notation, you can traverse the namespace hierarchy to see all available namespaces. For example, to see the namespaces available in the software root namespace, you can do the following:

    $EsxCli.software
    
    Output
    PS C:\> $EsxCli.software
    
    =======================
    EsxCliElement: software
    
       Elements:
       ---------
       acceptance
       profile
       sources
       vib
    
       Methods:
       --------
       string Help()
    
    Output showing the namespaces available in the `esxcli software` root namespace.

    Running esxcli commands

    As you look at the different namespaces, you’ll start to see Method Elements and Methods listed in the output. For example, if you call the $EsxCli.software.vib namespace element, you’ll see the following:

    $EsxCli.software.vib
    
    Output
    PS C:\> $EsxCli.software.vib
    
    ==================
    EsxCliElement: vib
    
       Elements:
       ---------
       signature
    
       Method Elements:
       ---------
       get
       install
       list
       remove
       update
    
       Methods:
       --------
       string Help()
    
    Output showing available Methods and Method Elements for `esxcli software vib`.

    Method Elements vs Methods

    Method Elements refer to PowerShell objects that represent an available esxcli command, whereas Methods are the functions you can execute. Method Element objects contain multiple Methods, as you’ll see later.

    For example, you can execute the $EsxCli.software.vib.Help() Method directly and it will display some useful information, as shown below.

    $EsxCli.software.vib.Help()
    
    Output
    PS C:\> $EsxCli.software.vib.Help()
    
    ================================================================================================================================================================================================================
    vim.EsxCLI.software.vib
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Install, update, remove, or display individual VIB packages
    
    ChildElement
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    - software.vib.signa | Commands to verify signatures and show information about the signature verification status of VIB packages
      ture               |
    
    Method
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    - get                | Displays detailed information about one or more installed VIBs
    - install            | Installs VIB packages from a URL or depot. VIBs may be installed, upgraded, or downgraded. WARNING: If your installation requires a reboot, you need to disable HA first.
    - list               | Lists the installed VIB packages
    - remove             | Removes VIB packages from the host. WARNING: If your installation requires a reboot, you need to disable HA first.
    - update             | Update installed VIBs to newer VIB packages. No new VIBs will be installed, only updates. WARNING: If your installation requires a reboot, you need to disable HA first.
    
    Output showing `Help()` information for `esxcli software vib`

    But, if you try and call the get Method Element directly, you’ll get an error; for example:

    $EsxCli.software.vib.get()
    
    Output
    PS C:\> $EsxCli.software.vib.get()
    #error
    InvalidOperation: Method invocation failed because [VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliElementImpl] does not contain a method named 'get'.
    #enderror
    
    Output showing error when calling a Method Element directly.

    Using the Invoke() Method

    Now that you know the difference between Method Elements and Methods, it’s time to start running some esxcli commands.

    First, print the $EsxCli.software.vib.get object to the console using the following command. You’ll see that it contains its own list of callable Methods.

    $EsxCli.software.vib.get
    
    Output
    PS C:\> $EsxCli.software.vib.get
    
    ==================
    EsxCliMethodElement: get
    
       Methods:
       --------
       Hashtable CreateArgs()
       VIB[] Invoke(Hashtable args)
       string Help()
    
    Output showing the available Methods within the `esxcli software vib get` Method Element.

    You can use the Invoke() Method to run the esxcli software vib get command; for example:

    $EsxCli.software.vib.get.Invoke()
    
    Output
    PS C:\> $EsxCli.software.vib.get.Invoke()
    
    AcceptanceLevel           : VMwareCertified
    Conflicts                 :
    CreationDate              : 2016-01-06
    Depends                   : {vmkapi_2_3_0_0, com.vmware.driverAPI-9.2.3.0}
    Description               : Adaptec HBA Driver
    HardwarePlatformsRequired :
    ID                        : Adaptec_Inc_bootbank_scsi-aacraid_6.0.6.2.1.52011-1OEM.600.0.0.2494585
    InstallDate               : 2020-01-07
    LiveInstallAllowed        : False
    LiveRemoveAllowed         : False
    MaintenanceModeRequired   : True
    Name                      : scsi-aacraid
    Overlay                   : False
    Payloads                  : {scsi-aac}
    Provides                  :
    ReferenceURLs             :
    Replaces                  :
    StatelessReady            : False
    Status                    :
    Summary                   : aacraid: scsi driver for VMware ESX
    Tags                      : {driver, module}
    Type                      : bootbank
    Vendor                    : Adaptec_Inc
    Version                   : 6.0.6.2.1.52011-1OEM.600.0.0.2494585
    
    AcceptanceLevel           : VMwareCertified
    Conflicts                 :
    CreationDate              : 2019-06-30
    Depends                   : {vmkapi_2_5_0_0}
    Description               : Broadcom NetXtreme-E VMKAPI network driver for VMware ESXi
    ...
    
    Partial output showing list of vibs returned from the `esxcli software vib get` command.

    Providing arguments to an esxcli command

    Now that you have learned how to use the Invoke() method to run an esxcli command in PowerCLI, the next step is to look at how to provide arguments.

    Identifying the available arguments

    How do you know which arguments an esxcli command accepts? Thankfully, there are two easy ways to list the available arguments, both of which are available in the $EsxCli.software.vib.get object:

    1. You can use the Help() method. The Help() method prints detailed information on the specific command to the console. For example, you can run $EsxCli.software.vib.get.Help() to print information relating to the esxcli software vib get command, as shown below.

      $EsxCli.software.vib.get.Help()
      
      Output
      PS C:\> $EsxCli.software.vib.get.Help()
      
      ================================================================================================================================================================================================================
      vim.EsxCLI.software.vib.get
      ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Displays detailed information about one or more installed VIBs
      
      Param
      ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      - rebooting-image    | Displays either information for the ESXi image that becomes active after a reboot, or nothing if the pending-reboot image has not been created yet. If not specified, information from the curr
                              | ent ESXi image in memory is returned.
      
      - vibname            | Specifies one or more installed VIBs about which further information is displayed. If this option is not specified, then more information on all the installed VIBs will be displayed. Must be one of the following f
                              | orms: name, name:version, vendor:name, or vendor:name:version.
      
      Output showing the `Help()` info for the `esxcli software vib get` command.
    2. You can use the CreateArgs() helper method. The CreateArgs() method returns an object containing every available argument. You can run the CreateArgs() method, store the resulting object in a variable, and then print the object to the console to see the available arguments, as follows:

      # Run the CreateArgs() method and store the returned object in $ArgsObj.
      $ArgsObj = $EsxCli.software.vib.get.CreateArgs()
      
      # Write the $ArgsObj to the console to view the available arguments.
      $ArgsObj
      
      Output
      PS C:\> $ArgsObj = $EsxCli.software.vib.get.CreateArgs()
      PS C:\> $ArgsObj
      
      Name                           Value
      ----                           -----
      vibname                        Unset, ([string[]], optional)
      rebootingimage                 Unset, ([boolean], optional)
      
      Output showing the `CreateArgs()` object for the `esxcli software vib get` command.

    Supplying arguments to the Invoke() method

    Now you’ll learn how to supply the available arguments to the Invoke() method of an esxcli command. There are two different ways to do this:

    1. You can supply the arguments/values as an inline hashtable. For example, to set the vibname argument to the value tools-light, you can do the following:

      $EsxCli.software.vib.get.Invoke(@{vibname = "tools-light"})
      
      Output
      PS C:\> $EsxCli.software.vib.get.Invoke(@{vibname = "tools-light"})
      
      AcceptanceLevel           : VMwareCertified
      Conflicts                 :
      CreationDate              : 2020-08-06
      Depends                   : {esx-version >= 6.6.0}
      Description               : This package contains cdrom and floppy images used to install the VMware Tools inside virtual machines.
      HardwarePlatformsRequired :
      ID                        : VMware_locker_tools-light_11.1.1.16303738-16701467
      InstallDate               : 2021-01-11
      LiveInstallAllowed        : True
      LiveRemoveAllowed         : True
      MaintenanceModeRequired   : False
      Name                      : tools-light
      Overlay                   : False
      Payloads                  : {tools}
      Provides                  :
      ReferenceURLs             :
      Replaces                  :
      StatelessReady            : True
      Status                    :
      Summary                   : VMware Tools VIB for Windows and Linux guests (generated by ESXi server build 16701467)
      Tags                      :
      Type                      : locker
      Vendor                    : VMware
      Version                   : 11.1.1.16303738-16701467
      
      Supplying arguments to the `Invoke()` method using an inline hashtable.
    2. You can use the object created by the CreateArgs() method. To use the same example again, you can set the vibname argument to the value tools-light , as follows.

      # Run the CreateArgs() method and store the returned object in $ArgsObj.
      $ArgsObj = $EsxCli.software.vib.get.CreateArgs()
      
      # Set the vibname property to "tools-light".
      $ArgsObj.vibname = "tools-light"
      
      # Supply the $ArgsObj object to the Invoke() method.
      $EsxCli.software.vib.get.Invoke($ArgsObj)
      
      Output
      PS C:\> $ArgsObj = $EsxCli.software.vib.get.CreateArgs()
      PS C:\> $ArgsObj.vibname = "tools-light"
      PS C:\> $EsxCli.software.vib.get.Invoke($ArgsObj)
      
      AcceptanceLevel           : VMwareCertified
      Conflicts                 :
      CreationDate              : 2020-08-06
      Depends                   : {esx-version >= 6.6.0}
      Description               : This package contains cdrom and floppy images used to install the VMware Tools inside virtual machines.
      HardwarePlatformsRequired :
      ID                        : VMware_locker_tools-light_11.1.1.16303738-16701467
      InstallDate               : 2021-01-11
      LiveInstallAllowed        : True
      LiveRemoveAllowed         : True
      MaintenanceModeRequired   : False
      Name                      : tools-light
      Overlay                   : False
      Payloads                  : {tools}
      Provides                  :
      ReferenceURLs             :
      Replaces                  :
      StatelessReady            : True
      Status                    :
      Summary                   : VMware Tools VIB for Windows and Linux guests (generated by ESXi server build 16701467)
      Tags                      :
      Type                      : locker
      Vendor                    : VMware
      Version                   : 11.1.1.16303738-16701467
      
      Supplying arguments to the `Invoke()` method using the `CreateArgs()` helper.

    Conclusion

    In this tutorial, you’ve learned how to create an esxcli interface in PowerCLI using the Get-EsxCli cmdlet. You then learned how to investigate the namespace hierarchy and invoke esxcli commands with or without arguments, as required. I hope you’ve found this interesting and thanks for reading!

    ×