How to Connect PowerCLI to vCenter and Run Basic Scripting

The true power of a VMware vSphere environment is unlocked when you see the potential of using automation to control vSphere. Please refer to this article to know PowerCLI connect to vCenter and start scripting.

Crystal

By Crystal / Updated on December 2, 2022

Share this: instagram reddit

What is VMware PowerCLI

VMware PowerCLI is a set of PowerShell modules from VMware, which extend PowerShell capabilities to understanding additional commands and interacting with VMware environments. When you install PowerCLI, VMware-specific PowerShell commands called commandlets (or cmdlets) are installed, and these allow you to work with VMware vSphere environments.

PowerCLI is very easy to install and learn, even for newbies who have never used PowerShell before, it is easy to get started and learn some simple type scripts. PowerCLI is able to automate VMware vSphere. There are many advantages of scripting in automation that include:

  • Easy to learn and use
  • Minimum programming knowledge or experience required
  • Allow complex tasks to be performed in relatively few steps
  • Allow editing with a variety of text editors
  • Allow the addition of dynamic and interactive activities to web pages
  • Faster Editing & Execution

Scripting in automation saves time on our basic tasks. In this article, we will cover how to connect PowerCLI to vCenter and introduce some basic scripting.

How to Connect Power CLI to vCenter or ESXi Host

PowerCLI can be used to connect to both vCenter and also the vSphere host independently. It will ask for username and password to access vCenter.

1. Enter this command to connect to vCenter.

> connect-viserver –server 〈yourserver〉

2. You will be prompted for your login credentials. Type in your username and password for either your ESXi server or vCenter Server.

enter username and password

3. Enter the following cmdlet to connect PowerCLI to vCenter:

> Connect-VIServer  “vCenter Name / IP ”  -user “username”  -password “password “

powercli connect to vcenter

If you are using a self-signed or untrusted SSL certificate on your vCenter Server, you will see an error connecting.

untrusted-certificate-error-when-connecting-to-vcenter-server

To work around this, there is a PowerCLI cmdlet we can use to ignore the certificate errors.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

ignoring-an-untrusted-ssl-certificate-when-you-connect-powercli-to-vcenter

You can either connect directly to an ESXi host or to vCenter Server using the PowerCLI cmdlet above.

Basic PowerCLI Scripting

One of the most basic commands we can run when we connect to either ESXi or vCenter is seeing all the VMs in the environment.

Get-VM - The get-vm commandlet lists VMs running on the connected host

get vm commandlet

Get-VM | fl – Displays more verbose information such as the Guest Operating System, Resource pools, VM Hardware version level, folder it is located in, and many other details.

get-vm-full-in-information

Get-VM | where-object {$_.PowerState –eq “PoweredOff”}

Displays only the VMs that are in the “PoweredOff” PowerState. After getting all the powered off VMs and then power those VMs quickly on when you need.

display-poweredoff-vm

Get-VM 〈yourvm | Stop-VMguest

stop-vm

If you do not want to receive the confirmation of the action, we can add the –confirm:false parameter:

Stop-VMguest –confirm:false

PowerCLI is very powerful to execute daily tasks. In addition to mastering these basic automation scripts, you must know how to back up VMware ESXi which allows fast and secure recovery. The backup process is easier and faster than setting up a new computer if you lose your data in the process of VM migration.

Batch backup your VMs on vCenter Server with an efficient software

If you only own 1 or 2 devices, you can clone virtual machines, copy virtual machine disk files and export VM as OVF template to complete ESXi backup. However, for long-term data protection, it is better to use a professional virtual machine to create standalone and automated backups. Here, I will try a professional and VMware backup tool – AOMEI Cyber Backup to perform automated backups for multiple virtual machines. It supports various versions including ESXi 6.0-7.0, and it offers automated backup task and instant disaster recovery. You are able to backup your multiple virtual machines with its flexible strategies.

With AOMEI Cyber Backup, you can enjoy these features easily.

Secure VM Backup: schedule VMware or Hyper-V virtual machine backups and perform hot backup to protect crucial data continuously without human errors.
Flexible vSphere Backup: batch backup large numbers of VMs managed by vCenter Server, or multiple VMs on a standalone ESXi host.
Instant Disaster Recovery: instantly restore the whole virtual machine to the previous state from any selected history version.
Role-Assignment: allow one administrator to create sub-accounts with limited privilege, effectively reduce administration cost and manual errors.

Please hit the button below to download and use AOMEI Cyber Backup 30-day free trial:

Download Free TrialVMware ESXi & Hyper-V
Secure Download

*You can choose to install this VM backup software on either Windows or Linux system.

If you want to protect the security of your virtual machines comprehensively, please refer to the following steps.

1. Add devices: download AOMEI Cyber Backup and click Source Device >> VMware >> + Add VMware Device to Add vCenter or Standalone ESXi host. And then click  >> Bind Device.

bind vcenter

2. Create backup task: click Backup Task >> Create New Task.

★ Enter task name and batch select large numbers of VMs managed by vCenter Server for centralized backup.

★ Schedule: schedule backup as full / incremental / differential backup and specify time to run the backup.

★ After configuring the backup task, review the information and click Start Backup.

create vm backup

3. Restore: click “…”>> Restore to select restoration content and destination.

You can choose to Restore to original location. It allows you to recover entire VM easily and quickly. It saves time to recreate or configure ESXi virtual machines.

Or you can also restore to new location to create a new VM in the same or another datastore/host directly from the backup, saving the trouble of re-configuring the new VM.

restore vm

Summary

Scripting in IT automation is being popular, and now you have learned how to connect to vCenter and a few more tricks. This cmdlet establishes a connection to a vCenter Server system.

Crystal
Crystal · Editor
Crystal is an editor from AOMEI Technology. She mainly writes articles about virtual machine. She is a positive young lady likes to share articles with peolpe. Off work she loves travelling and cooking which is wonderful for life.