PowerShell, developed by Microsoft, has evolved into a versatile tool that goes beyond the traditional command-line interfaces. It seamlessly integrates with Windows, allowing users to manage and automate tasks efficiently. From simple one-liners to complex scripts, PowerShell provides a scalable and robust platform for handling various tasks, making it an indispensable tool in the toolkit of IT professionals.

What makes it my favorite scripting language is two-folds. First it is its object-oriented nature that makes it incredibly versatile to work with the pipeline and process outputs effectively. Try moving to Bash after working with PowerShell for a while and you will understand what I mean. The second point is how forgiving it is when it comes to syntax. Casing doesn’t matter, nor do white spaces for instance. This is incredibly useful in the day to day life as it saves a lot of time.

Protect Your Data with BDRSuite

Cost-Effective Backup Solution for VMs, Servers, Endpoints, Cloud VMs & SaaS applications. Supports On-Premise, Remote, Hybrid and Cloud Backup, including Disaster Recovery, Ransomware Defense & more!

Now when it comes to the workings of PowerCLI, its biggest strength is its extensibility through the use of custom and third-party modules, often available through the PowerShell Gallery, a simple cmdlet away. Most software vendors offer PowerShell modules to interact with their products as they know they can’t afford to alienate the whole Windows minded admin-base. Along with those official modules, you are free to write your own (in the format *.psm1) in which you can store all your favorite functions for instance. Those modules will then be autoloaded when calling them from your prompt.

One such vendor offering third-party modules is obviously VMware with the famous PowerCLI which allows you to interact with most VMware products out there. The cmdlets are fairly comprehensive, unless you are trying to do something very obscure, in which case you may have to go dig a little deeper into the REST API.

In this article, we will explore how to get started with PowerCLI and enable you to start writing your own automation scripts.

Download Banner

Installing PowerCLI

First of all, know that PowerCLI (and PowerShell as a whole for that matter) isn’t reserved for Windows user anymore as you can perfectly well install it on Linux or MacOs. The only requirement is to have .NET Core 3.1 installed but it often already is.

Also, it is probably not worth mentioning anymore but if you still have the dinosaur version PowerCLI 6.5 R1 installed you must uninstall it first.

PowerShell on Linux and MacOS

First of all, if you are on Linux or MacOS, you must have PowerShell (pwsh) installed as a prerequisite if it isn’t already. I will not describe those procedures here as it is out of the scope but you can just head over to the official documentations:

Installing PowerCLI with Internet access

Thanks to the power of PowerShell Gallery, installing PowerCLI is just a cmdlet away when you have access to internet.

If you are on MacOS or Linux, open PowerShell by typing “pwsh” in the terminal.

  • Then install PowerCLI with this simple command:

Install-Module VMware.PowerCLI -Scope CurrentUser

If you see a warning that you are installing modules from an untrusted repository, press y and then press Enter to confirm the installation.

Installing PowerCLI without Internet access

If you work in a restricted environment or if your workstation is on the management network that is isolated from the internet, you can still download and install PowerCLI, it will just take a little longer.

  • First you need to head over to https://developer.vmware.com/web/tool/vmware-powercli/ and download the latest version of PowerCLI.
  • Installing-PowerCLI

  • Transfer the ZIP file to your workstation using a secure method according to your company’s security policies (USB key, FTP, …).
  • Extract the contents of the ZIP file to your machine’s PowerShell folder. You can decide whether to make it available to your own user or to all users.
  • Current User All Users
    %USERPROFILE%\Documents\WindowsPowerShell\Modules C:\Program Files\WindowsPowerShell\Modules
  • If you are on Windows, run the following command to unblock the copied files:

Get-ChildItem -Path -Recurse | Unblock-File

Updating PowerCLI

In order to perform a clean update of PowerCLI, the recommended method is to first uninstall the current version and avoid using the Update-Module cmdlet to update PowerCLI as it will not remove any files rendered obsolete by the new version.

  • To uninstall the existing version, run the following command:

Get-module VMware.* -listAvailable | Uninstall-Module -Force

  • Then install the latest version by following the installation method that fits your environment (online or offline).

Specifics of Execution Policies

In PowerShell an Execution policy is a security mechanism that determines if a file can or cannot be loaded in PowerShell. This includes config files, modules, scripts and so on. The current execution policy can be displayed with:

Get-ExecutionPolicy

Usually, you won’t need to change it on Windows Server OS, however, the default execution policy on Windows client OS is set to Restricted, meaning you you will have to change it to, at least, RemoteSigned to be able to run PowerCLI.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

If you work in a restricted environment that requires all scripts to be signed by an internal PKI (even those written in-house), you will need to set the execution policy to AllSigned. Meaning you will need to sign your scripts prior to running them (any change to a script will require a new signature).

Checking PowerCLI version

Like after any CLI package installation, you will want to check the version.

PS /Users/xavier> Get-PowerCLIVersion -WarningAction Ignore

PowerCLI Version
—————-
VMware.PowerCLI 12.7.0 build 20091289
—————
Component Versions
—————
VMware Common PowerCLI Component 12.7 build 20067789
VMware Cis Core PowerCLI Component PowerCLI Component 12.6 build 19601368
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 12.7 build 20091293

Starter PowerCLI settings

After installing PowerCLI, there are a few parameters that are worth taking a look at to make your life easier.

ParticipateInCEIP

You will get the warning when you run a command anyway but you should choose whether to participate in the Customer Experience Improvement Program (CEIP). Specify $true to opt in and $false to opt out.

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true | $false

InvalidCertificateAction

If the vCenter servers you are going to connect to don’t have certificates installed that are signed by a trusted CA (usually the default self-signed certificates), then you will get a warning every time you connect to it. It gets old quickly.

This parameter lets you ignore invalid certificate (expired, self-signed, not trusted…). To change this, set the following parameter:

Set-PowerCLIConfiguration –InvalidCertificateAction

  • Prompt: Prompt before continuing. Possible choices are Deny, Accept for once, Accept permanently, Accept for all users.
  • Fail: No connection established
  • Ignore: Connection established regardless of the certificate status
  • Warn: Connection is established but a warning is displayed (default)

DefaultVIServerMode

If your VMware environment consists of a single vCenter server, then you don’t have to worry about this. However, if you happen to connect to multiple vCenter servers as part of your day-to-day job, you may want to consider this parameter. “DefaultVIServerMode” defines whether you can be connected to more than one vCenter server at a time.

I specify this parameter because it can have unwanted consequences in case you run a command without a filter against multiple vCenter servers. Imagine you want to stop all the VM with a name starting with “sql-*” in the test environment but you forgot you were still connected to the production environment… No good.

To change this behaviour, you can use the “DefaultVIServerMode” parameter of the “Set-PowerCLIConfiguration” cmdlet.

Set-PowerCLIConfiguration -DefaultVIServerMode

  • Single: One vCenter server connected at a time
  • Multiple: Multiple vCenter servers can be connected in the session

How to connect to an endpoint in PowerCLI

In the title of this chapter I mentioned endpoint and not vCenter because you can also connect directly to an ESXi server using PowerCLI.

If your PowerShell session runs under a user that has access to vCenter server, you can simply type:

Connect-VIServer vcenter.foorbar.priv

If you need to specify a different user, you can use the same command but it will try your user, fail and prompt you for credentials. You can speed this up with:

Connect-VIServer vcenter.foorbar.priv –Credential (Get-Credential)

Interacting with your VMware SDDC

Now that you are connected to your environment, you can start having some fun with PowerCLI and start exploring its capabilities. As a rule of thumb, refer to the following when you aren’t sure where to go:

Get-Commands: Find which commands contain a specific string. E.g.

PS /Users/xavier> gcm *cnsvolume*

CommandType Name Version Source
———– —- ——- ——
Cmdlet Get-CnsVolume 12.7.0.20… VMware.VimAutomation.Storage
Cmdlet New-CnsVolume 12.7.0.20… VMware.VimAutomation.Storage
Cmdlet New-CnsVolumeAttachment 12.7.0.20… VMware.VimAutomation.Storage
Cmdlet New-CnsVolumeMetadata 12.7.0.20… VMware.VimAutomation.Storage
Cmdlet Remove-CnsVolume 12.7.0.20… VMware.VimAutomation.Storage
Cmdlet Remove-CnsVolumeAttachment 12.7.0.20… VMware.VimAutomation.Storage
Cmdlet Set-CnsVolume 12.7.0.20… VMware.VimAutomation.Storage

Get-Member: Find what is available to you on a PowerShell object. This will include properties and methods. E.g.

PS /Users/xavier> Get-VMHost | Get-Member

TypeName: VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl

Name MemberType Definition
—- ———- ———-
ConvertToVersion Method T VersionedObjectInterop.ConvertToVersion[T]()
Equals Method bool Equals(System.Object obj)
GetClient Method VMware.VimAutomation.ViCore.Interop.V1.VIAutomation VIObjectCoreInterop.GetClient()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
IsConvertableTo Method bool VersionedObjectInterop.IsConvertableTo(type type)
LockUpdates Method void ExtensionData.LockUpdates()
ToString Method string ToString()
UnlockUpdates Method void ExtensionData.UnlockUpdates()
ApiVersion Property string ApiVersion {get;}
Build Property string Build {get;}
ConnectionState Property VMware.VimAutomation.ViCore.Types.V1.Host.VMHostState ConnectionState {get;}
CpuTotalMhz Property int CpuTotalMhz {get;}

| Select *: Find all the properties of a PowerShell object. Sometimes Get-Member may not be enough and you need to see what the properties contain to really get a clear picture of what something is. Example,

PS /Users/xavier> Get-VMHost esxi02* | select *

State : Connected
ConnectionState : Connected
PowerState : PoweredOn
VMSwapfileDatastoreId :
VMSwapfilePolicy : Inherit
ParentId : ClusterComputeResource-domain-c7
IsStandalone : False
Manufacturer : RAUSCH
Model : 410074
NumCpu : 40
CpuTotalMhz : 83960
CpuUsageMhz : 24650

Get-Help: The equivalent of “man” in Unix systems. Get-Help (alias help) will be your best friend, especially with “-full” parameter.

Wrap up

In conclusion, we’ve explored the fundamental aspects of PowerCLI, delving into its robust command-line capabilities for VMware management. From concise one-liners to sophisticated automation, PowerCLI empowers users to manage virtual environments more efficiently.

This introduction serves as a stepping stone to start the journey with PowerCLI. Consider the myriad possibilities it offers for efficient system administration and the automation of routine operations within your VMware infrastructure.

Read More:

Virtualization Trends Series: VMware by Broadcom Licensing Changes: Part 9

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Rate this post