Contents
Need an expert to help you with
" How to Connect ESXCLI with PowerCLI" ?

Get Help

How to Connect ESXCLI with PowerCLI

How to Access ESXCLI with PowerCLI
How to Access ESXCLI with PowerCLI
  PowerShell’s PowerCLI can be a door into ESXCLI command line interface. Let’s have a look at how to connect ESXCLI with PowerCLI using ESXi host. I have talked about the Access ESXCLI with Power CLI earlier, the details of which you can read in these articles:

Vsphere

If you have ever worked with vSphere, the odds are that you needed to perform certain tasks on an ESXi host using ESXCLI. This command line interface created by VMware enables you to perform tasks such as installing software, configuring NIC adapters, or setting firewall rules on ESXi. As ESXi runs only Linux, using ESXCLI can be a little daunting for Windows admins who do not have a lot of experience with Linux. Luckily, if you know PowerShell, you can use PowerCLI as a door into ESXCLI.

Connect ESXCLI with PowerCLI

First, you have to connect to either a vCenter server or ESXi host. You may do this with the Connect-VIServer ‘cmdlet’. Remember that you can also connect to your vCenter server as well.
Connect-VIServer -Server devhost -Credential (Get-Credential)

Exposing ESX CLI

Now, since you are connecting to your ESXi host ‘Devhost’, you can use the Get-EsxCli cmdlet in PowerCLI to expose the ESXCLI functionality by placing it into a variable.
$DevHost = Get-EsxCli -VMHost devhost -V2
  Notice that the example used the –V2 parameter. This means you will be using version 2 of this cmdlet; version 1 is deprecated and compatibility is not guaranteed. Now if you run the output of ‘$DevHost’, you see the ‘elements’ you need to work for this ESXi host.
$DevHost = Get-EsxCli -VMHost devhost -V2

C:> $DevHost

=============================
EsxCli: devhost

   Elements:
   ---------
   device
   elxnet
   esxcli
   fcoe
   graphics
   hardware
   iscsi
   network
   rdma
   sched
   software
   storage
   system
   vm
   vsan
  If you want to see which tasks you can do with each element, you can drill down by running ‘$DevHost.<element>’ to view the ‘CodeMethods’ and ‘CodeProperties’. For instance, if you run ‘$DevHost.system.account | Get-Member –MemberType Code*’, then you will see that you can add, list, remove, and set accounts on this host.
C:> $DevHost.system.account | Get-Member -MemberType Code*

   TypeName: VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliElementImpl

Name   MemberType   Definition
----         ----------      ----------
Help      CodeMethod     string Help()
add       CodeProperty    VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomization.SimpleExtensionProperty
list        CodeProperty    VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomization.SimpleExtensionProperty
remove  CodeProperty   VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomization.SimpleExtensionProperty
set        CodeProperty    VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomization.SimpleExtensionProperty
 

Removing VIBS

For example, you are having an issue with a VIB and you want to delete it; Iin this case, the Dell OpenManage agent for ESXi. First, you want to find the name of the VIB. You can do this by piping a list of VIBS to the Where-Object cmdlet.
$DevHost.software.vib.list.Invoke() | Where-Object {$_.Name -like "*OpenManage*"}

AcceptanceLevel : PartnerSupported
CreationDate    : 2015-07-29
ID              : Dell_bootbank_OpenManage_8.2.0.ESXi600-0000
InstallDate     : 2016-09-08
Name            : OpenManage
Status          :
Vendor          : Dell
Version         : 8.2.0.ESXi600-0000
  You can see the name of the VIB is ‘OpenManage’. Now you can remove this VIB by using the ‘Invoke()’ method, however, it needs some arguments in the form of a hash table. To find what arguments you can run, you can use the ‘Help()’ method. Like it appears, one of the parameters is ‘dryrun’, this should let you see the result of removing the VIB but not do any system changes. This is similar to a ‘-Whatif’ parameter in PowerShell.
C:> $DevHost.software.vib.remove.Invoke(@{'dryrun' = $True;'force' = $False;'maintenancemode' = $False;'noliveinstall' = $False;'vibname' = 'OpenMan
age' })

Message        : Dryrun only, host not changed. The following installers will be applied: [BootBankInstaller]
RebootRequired : true
VIBsInstalled  :
VIBsRemoved    : {Dell_bootbank_OpenManage_8.2.0.ESXi600-0000}
VIBsSkipped    :
  The result of the dry run looks good. So you can remove the VIB by just switching ‘dryrun’ to ‘$True’.
C:> $DevHost.software.vib.remove.Invoke(@{'dryrun' = $False;'force' = $False;'maintenancemode' = $False;'noliveinstall' = $False;'vibname' = 'OpenMa
nage' })

Message        : The update completed successfully, but the system needs to be rebooted for the changes to be effective.
RebootRequired : true
VIBsInstalled  :
VIBsRemoved    : {Dell_bootbank_OpenManage_8.2.0.ESXi600-0000}
VIBsSkipped    :
 

Get system time

Retrieving the system time on an ESXi host is very simple as there are no parameters for the ‘Invoke()’ method.
C:> $DevHost.system.time.get.Invoke()
2017-07-07T15:54:25Z
 

View running processes

Have you ever wondered what processes are running on your host at the moment? Using PowerCLI, you can get a list of processes with names and id.
C:> $DevHost.system.process.list.Invoke() | Select-Object -Property Name,id | Sort-Object -Property Name
 

Pinging other hosts

As illustrated by Alan Renouf, there is a very innovative way to ping others hosts using ‘Get-EsxCli’. First, you need to create a hash table of our arguments needed with ‘CreateArgs()’.
C:> $arguments = $DevHost.network.diag.ping.CreateArgs()
  Next, set the value of the host you want to ping, in this case ‘8.8.8.8’.
C:> $arguments.host = '8.8.8.8'
  Finally, pass the hash table of arguments to ‘Invoke()’ and pipe to ‘Select-Object’ to view the output of your ping. You see that all pings were transmitted successfully.
C:> $DevHost.network.diag.ping.Invoke($arguments) | Select-Object -ExpandProperty Summary

Duplicated     : 0
HostAddr       : 8.8.8.8
PacketLost     : 0
Recieved       : 3
RoundtripAvgMS : 6762
RoundtripMaxMS : 7310
RoundtripMinMS : 6296
Transmitted    : 3
 

Automating pings from every host

For this example we will try to see if all the ESXi hosts can ping a particular IP address, for example a server hosting an NFS share. First, begin by connecting to the ‘vCenter’ server in PowerCLI.
C:> Connect-VIServer vcenter
  Next, let’s place all of the hosts into the ‘$ESXiHosts’ variable with Get-EsxCLI.
C:> $ESXiHosts = Get-EsxCli -VMHost (Get-VMHost) -V2
  Now run a one-liner in PowerCLI to attempt a ping from all of the hosts to the IP address in the ‘$Arguments’ hash table. The output will be the hostname of each ESXi host and the number of pings received by the destination.
C:> $ESXiHosts | ForEach-Object {$Received = ($_.network.diag.ping.Invoke($arguments) | Select-Object -ExpandProperty Summary).Recieved;$VMHost = $_.VMHost.Name;Write-Output "$VMHost,$Received"}

Devhost-1,3
Devhost-2,3
Devhost-3,3
Devhost-4,3
Devhost-5,3
Devhost-6,3
Devhost-7,3
Devhost-8,3
Updated on 11 Sep 2018
0 Comments
Add a comment
Login for Comment