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):

Tuesday, March 8, 2022

PowerCLI on my Ubuntu desktop

 

I think many of us one day may require to deploy and work with useful VMware PowerCLI cmdlets through our Linux-based workstations. At the first step, you need to install Microsoft PowerShell first of all. So let's do it. Remember you can login to the root access, via running the sudo -i to avoid input "sudo" in each line of command execution:

1. Keep the OS up-to-date:

# sudo apt-get update

# sudo apt-get upgrade

Keep in mind you can upgrade to the latest official version of your Ubuntu OS via:

# sudo apt-get dist-upgrade

2. You need the "Curl" as the primary prerequisite, so run this if you didn't yet:

# sudo apt-get install curl

3. Then we need to add the corresponding Microsoft Repository to get the PowerShell: 

# sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add –

In earlier versions, you may encounter some warning like deprecation of some commands like apt-key

 

# sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/21.04/prod.list

4. Execute the following commands to install and, then run the Microsoft PowerShell:

# sudo apt-get install powershell-preview

# sudo pwsh-preview  

5. Finally you got the PowerShell. Now it's time to install the latest VMware PowerCLI and even enjoy some other useful SDK of VMware products, like Horizon View:

> Install-Module -Name VMware.PowerCLI

> Install-Module -Name VMware.vSphere.Sdk

> Import-Module -Name VMware.VimAutomation.HorizonView

In most cases you need to choose your response for VMware CEIP:

> Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true

 

 

 

 

To list all available scripts about the VMware tools and solutions:

> Get-Module *vmware* -ListAvailable 

Basically, you need to connect to the vCenter server to run other required commands:

> Connect-VIServer -Server 10.10.10.100 -User administrator@vsphere.local -Password YourPass

However, you may see the invalid certificate error but you can ignore the certificate checking procedure, especially for the self-signed certificate. 


 

Although it's highly recommended to use a valid certificate in the vSphere environment and the below action is not a reliable action as the security consideration, so I think it's better to set it at least in the warning level:

> Set-PowerCLIConfiguration -InvalidCertificateAction warn

Now it's possible to use the VMware PowerCLI cmdlets through the Ubuntu Terminal, for example, you can get related information of virtual machines that are named like "DC":

> Get-VM | where-object ({$_.name -like "*DC*"}) | fl *

I hope this post can be helpful for you.



I will start a new journey soon ...