How to manage ESXi hosts remotely with PowerCLI

PowerCLI is a PowerShell-based framework that allows admins to create scripts that handle repetitive processes. Here's how to use PowerCLI to manage VMware ESXi hosts remotely.

Futuristic Circuit Board Render With Bokeh Effects

Image: yucelyilmaz, Getty Images/iStockphoto

When choosing to administer VMware-based virtual machines (VMs), administrators have a few decisions to make when prepping their bare-metal hosts and configuring the guest OSes, the storage spaces, and switches used to communicate with each other and across networks. The choices center on how to configure the hosts and VMs once the initial specs are chosen.

SEE: How to become a network administrator: A cheat sheet (free PDF) (TechRepublic)

Some admins will choose to install ESXi manually via removable media; others will prefer to use the kick start script method to initially configure hosts during the installation process. Both approaches require some form of onsite presence--a technician to manually boot the server and some form of bootstrap to run the installation from. Admins can also  use vCenter to centrally manage VMs using host profiles. Though this solution is arguably the best, not all organizations will be licensed for vCenter, which may be expensive.

But lucky for us, there is another option called PowerCLI, which acts as a PowerShell (PS) snap-in and is supported on Windows, macOS, and Linux. PowerCLI is flexible enough to be run post-installation and allows IT to create scripts that may be used to carry out management tasks. Like all scripts, they may be saved for future use, regardless of how the server was initially provisioned.

Since each ESXi server will be configured differently depending on the needs of the organization, this article won't be diving into the particulars of what commands to run to perform specific management tasks. The aim of this article is to help admins install PowerCLI on their management stations, as well as how to navigate the console and locate resources used in the management of their hosts and VMs.


Prerequisites:

  • .NET Framework 4.5 or later (Windows only)

  • PowerShell 3.0 or later

How to install PowerCLI

1. Launch PowerShell. If you're using Windows, use Run as Administrator.
2. Execute the following command:
            Install-Module VMware.PowerCLI
3. If prompted, allow the installation to continue.
Once complete, all the necessary cmdlets and their dependencies will be ready for use.

Getting around PowerCLI

Since PowerCLI is based on PowerShell's framework, the cmdlets and syntax will be familiar to admins with PS experience. For those with little to no experience using PS, here are common cmdlets (commands in PowerShell) to better understand how to find commands, what they do, and how to use them to execute management tasks for automation.

PowerShell's execution policy

PowerShell includes an execution policy that restricts running scripts by default; this is to prevent users from inadvertently running a script that could irrevocably cause a system issue. But if you wish to allow it, executing the cmdlet below will allow scripts to run without restriction.
            Set-ExecutionPolicy Unrestricted

List of available cmdlets in PowerShell

To obtain a full list of all cmdlets available to PS--including the PowerCLI commands--execute the cmdlet below.
            Get-Command

How to get PowerShell's help index

PowerShell includes a built-in help index that provides insight into the cmdlets, including the syntax and examples.
            Get-Help Connect-VIServer -examples
With a few basics on obtaining help from within PowerShell, let's review a few cmdlets that will be used to configure hosts.
Connect to ESXi hosts
            Connect-VIServer -Server IP_ADDRESS -Protocol https -User USER -Password PASS
Obtain system information
            Get-VMHost | Format-List
Get a list of VMs and status
            Get-VM | Format-List
Get detailed information on a specific VM
            Get-VMGuest GUEST_NAME | Format-List
View Datastore information
            Get-HardDisk -Datastore DATASTORE_NAME
Start a specified VM
            Start-VM -VM VM_NAME -Confirm
Restart a specified VM's OS
            Restart-VMGuest -Guest GUEST_NAME -Confirm
Restart a specified host server
            Restart-VMHost -VMHost HOSTNAME -Force

This is by no means an extensive list, but it should get admins started managing their ESXi infrastructure remotely. Pair this knowledge with researching VMware community forums and other resources, such as GitHub, for scripting guidance and, of course, hands-on learning to help carve out scripts that best meet the organization's needs.

Also see