Thursday, March 20, 2025

VMware PowerCLI (PowerShell) on Linux

VMware PowerCLI is very handy and flexible automation tool allowing automation of almost all VMware features. It is based on Microsoft PowerShell. I do not have any Microsoft Windows system in my home lab but I would like to use Microsoft PowerShell. Fortunately enough, Microsoft PowerShell Core is available for Linux. Here is my latest runbook how to leverage PowerCLI in Linux management workstation leveraging Docker Application packaging.

Install Docker in your Linux Workstation

This is out of scope of this runbook. 

Add yourself to User Group docker

sudo usermod -aG docker $USER

Note: you must logout and login back to be able to run docker.

Pull official and verified VMware PowerCLI

docker pull vmware/powercli:latest

Pull official and verified Microsoft Powershell

docker pull mcr.microsoft.com/powershell:latest

List container images

docker image ls

How to run container?

Now you can run any of two above powershell/powercli containers interactively (-i) and in allocated pseudo-TTY (-t).

docker run  -it  CONTAINER-NAME

docker run  -it vmware/powercli 
or
docker run  -it mcr.microsoft.com/powershell
 

VMware PowerCLI container

Using VMware PowerCLI container is easier because it contains PowerShell with PowerCLI prepared by VMware

This command will run VMware PowerCLI and you have everything ready to go.

docker run  -it \
-v /home/dpasek/powercli/scripts:/tmp/scripts \
-v /home/dpasek/powercli/settings:/root/.local/share/VMware/PowerCLI \ 
vmware/powerclicore

The -v option in Docker is used to mount volumes, allowing data to persist or be shared between the host and the container. 

We map our scripts at /home/dpasek/powercli/scripts directory to container directory /tmp/scripts.
 
We are keeping PowerCLI config at /home/dpasek/powercli/settings directory. This allows us to keep PowerCLI configuration persistent and we can run the following configuration commands just once and they stay persistent. Following commands are necessary to initialize PowerCLI configuration.

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true -Confirm:$false
Set-PowerCLIConfiguration -InvalidCertificateAction:ignore -Confirm:$false

... and now you can connect to vCenter and list VMs

Connect-VIServer -Server <vcenter-server> -User <username> -Password <password> | Out-Null
Get-VM | Select-Object -ExpandProperty Name

... if we have PowerShell script prepared, we can run it directly from the host system. Following example will run script /tmp/scripts/get-vms.ps1

docker run  -it \
-v /home/dpasek/powercli/scripts:/tmp/scripts \
-v /home/dpasek/powercli/settings:/root/.local/share/VMware/PowerCLI \
--entrypoint='/usr/bin/pwsh'  \
vmware/powerclicore \
/tmp/scripts/get-vms.ps1
 

Microsoft PowerShell container

If you want use Microsoft PowerShell container, you can, but you have to install PowerCLI manualy. 

Following command will run Microsoft PowerShell

docker run -it -v /home/dpasek/scripts:/root/scripts mcr.microsoft.com/powershell

Inside Microsoft PowerShell container, you must install PowerCLI manualy

Install-Module -Name VMware.PowerCLI -Scope CurrentUser -Force

You must allow untrusted certificates and enable (or disable) participation in VMware CEIP.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true

And now you can connect to vCenter, display vCenter Instance UUID, list ESXi hosts, and list VMs

Connect-VIServer -Server <vcenter-server> -User <username> -Password <password>

(Get-View ServiceInstance).Content.About.InstanceUuid

Get-VMhost

Get-VM

 


No comments: