Tuesday, March 22, 2022

Running ESXCLI commands through PowerCLI

ESXCLI is a set of useful namespaces to get (retrieve) or set (change) ESXi host configuration and truly is the real successor of ESXCFG commands in the roadmap of VMware CLI development. In addition, PowerCLI is a perfect option that we can use to gain the benefits of automation! Because it's possible to execute the ESXi shell's command through PowerCLI, as the network administrator you may require to run some ESXCLI commands via PowerCLI.

In this post, I explain how to achieve this goal by running a sample of retrieving options: Get the current Syslog configuration of all the ESXi hosts. So if we want to do it via esxcli, I should run this for all the ESXi hosts one by one through the shell and running of this command:

esxcli system syslog config get

Although I need to get this via for all the ESXi (named like VH*) by a foreach loop, First I need to create a variable ($MyESXs) to get the required ESXi value (for example its name):

 

 

 

 
Note: Because the first version of Get-EsxCli is deprecated and will be removed soon, it's recommended to use the second version.

 

 

 
 
Of course, you can change some considered attributes via this method, but you need to create some arguments before the execution of the final ESXCLI command. (CreateArgs). Then you can set any required namespace, like LogDir, LogHost, LogLevel, and so on, such as the following way:

  

 

If the result is successful, it returns True. However, you may also be familiar with the old-fashioned method of PowerCLI cmdlet execution to get and set some of Advanced Configuration. In my case (Syslog) I did it by some pipe-lining like this:   
In Windows PowerShell environment

  

 
 
In Linux pwsh-preview environment

 
 
 
 
 
You can also do it via editing and saving a *.ps1 file (power-shell extension), but In some circumstances like mine, you need to run them in a Linux environment. So I prefer to create a .ps1 file via touch command, then edit the generated file via nano or vi, and import the required content.

 




 

At last you can easily run the corresponding ps1 file inside your pwsh-preview (PowerShell of the Linux Shell):

No comments:

Post a Comment

I will start a new journey soon ...