PowerCLI is a versatile Windows PowerShell interface used for managing VMware vSphere. It enables us to perform automation of various aspects of vSphere management. One of the main advantages of PowerCLI is that it helps us to perform repetitive or complicated tasks that are hard to do on the web client very easily.

The automation tasks can be performed in PowerCLI through PowerCLI cmdlets or through VMware APIs. But writing script using PowerCLI cmdlets or VMware APIs is a daunting task, as the documentation is sparse and there is no proper training and certifications for mastering its usage.  For example; The vSphere API has over 450 functions. Some functions hide dozens of use cases, like the single argument to the ReconfigVM contains 306 nested properties. When we implement automation, the potential of the VMware vSphere environment can be achieved at full length. In this blog, we explain how to use Vsphere Power CLI And Project Onyx for easy automation.

Introduction to the basics of PowerCLI

The first cmdlets we need to run will connect us to our vSphere environment. We can either connect directly to an ESXi host or to the vCenter Server. In vSphere PowerCLI, you can have more than one connections to the same server. If you want to disconnect from a server, you must close all active connections to this server running the “Disconnect-VIServer” cmdlet. It should be noted that the current method explained here is only relevant up to vSphere version 6.0.

To connect, run the following command in a PowerCLI enabled PowerShell session:

  • Run the Connect-VIServer cmdlet with the server name: Connect-VIServer -Server esxi3.example.com

When you run the command, you will be prompted for entering login credentials. Type in your username and password for either your ESXi server or vCenter Server. One of the basic commands we can run when we connect to either ESXi or vCenter is to list all the VMs in the environment. We can use the “get-vm” commandlet to get the information about all VMs.

  • Get-VM – The get-vm commandlet lists all VMs on the connected host
  • Get-VM | where-object {$_.PowerState –eq “PoweredOff”}

This displays only the VMs that are in the “PoweredOff” PowerState and can be extremely useful. We can use the result of the above command in the Start-VM commandlet. So essentially we can get all the powered off VMs and then power those VMs on.

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

In vSphere PowerCLI, all parameters that take as arguments inventory objects ( Cluster, Datacenter, Folder, ResourcePool, Template, VirtualMachine,  VirtualSwitch ), datastores, OSCustomizationSpec objects, and VIServer objects can be specified by strings and wildcards. This PowerCLI approach is called Object-by-Name selection (OBN). If a provided object name is not recognized, an OBN failure occurs. In such cases, PowerCLI generates a non-terminating error and runs the cmdlet ignoring the invalid name.

These are the basic PowerCli cmdlets, You can use these cmdlets to know more details.

  • Get-PowerCLIHelp   This command will help for all powerCLI commands.
  • Get-VICommand   This command list all cmdlets.
  • Get-Help This cmdlets help to display the more information about all cmdlets

Example: Get-Help Set-VM check out the cmdlets result below.

Example: Get-Help Set-VM -full   OR   Get-Help Set-VM -examples . Any of the above cmdlets will give you the full technical information with examples about “Set_VM” cmdlets.

A sample result of which is shown below:

What is VMware Project Onyx.?

Here is one tool called “ Onyx “, It’s a script recorder for vSphere Client and is basically a proxy application that sits between your vCenter server / ESXi server and your vSphere Client or PowerCLI. It is easy to translate actions you do in VI client into PowerShell Code with Project Onyx.

It intercepts the communication between the two and generates the outputs of the commands in the language of your choice. Options include the raw SOAP messages, C#, Powershell, and JavaScript. This tool is extremely useful to create scripts. For VMware admins, you can learn how to perform mass administration of vSphere and your VMs using the command line / shell interface. This tool will help you essentially show the code that Vsphere client use to perform the action and enable you to follow best practices that VMware developers use.

You can simply download it from here. If you are using Vsphere 6.0 or older version, then you should download Onyx and run at the client side, that must installed VSphere Client. Let’s go through the steps.

Onyx Installation Procedure

  • Here we are using ESXi 6.0, so we downloaded and install Vsphere client. You should remember to Install Windows .NET Framework 3.5 for a smooth installation.
  • While you download Onyx, it will be a Zipped file. Just extract files and run “Onyx.exe” file.

     

  •  Once you’ve accepted the EULA and the application has launched, click Connect.

  • Enter the IP of the vCenter Server/ ESXi  on which you want to work and click Start.

  1. click on “Launch a client after connected” and select VMware VI client
  2. Enter the Credentials of vCenter Server/ ESXi and click start.

Now Vsphere client will open to the Vcenter or ESXi server. Now select the output mode as “PowerCLI.NET”  click on start/play button in onyx.

This helps you to run on port 1545 locally, and the vsphere client is connected here

Then Onyx will start to intercepts the communication between the two and outputs the commands in Powershell. The code Onyx generates makes heavy use of direct API calls rather than PowerCLI cmdlets. So even if you use Onyx to power on a VM, instead of using the Start-VM cmdlet it will use the API’s PoweronMultiVM_Task. To put it in another way, Onyx is best for automating things that can’t be automated using native PowerCLI cmdlets. You will need to edit scripts that Onyx produces in order to turn them into reusable code. The code Onyx produces today includes a lot of parameters that are very specific to the object you select and have to be removed.

For Example: Here we have an ESXi server with 27 VMs and out from those, we are going to upgrade 15Vm Memory as 4GB, CPU count as 2 as well as 5GB additional Hard disk.

Re-configuring a VM is very simple in Vsphere environment using Vsphere client OR Vsphere Web interface. But here we need to perform this task only 15 times. When you look up to a very large environment that maintains 300 or above VMs, it will be a complex task to upgrade all Vm’s manually. Here, in this scenario, we automate the upgrade of VM’s.

Our VM list:

Below showing the Powershell script that generated by Onyx to re-configure one VM’s Memory, CPU and Hard disk.

This code is only usable to the VM with VM ID: ‘VirtualMachine-23’. This code is not completely functional, you have to add supporting logic as shown below.

# ------- ReconfigVM_Task -------
$vms = Import-CSV "C:\Users\lenovo\Desktop\vms.csv"
foreach ($vm in $vms)
{
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.numCPUs = 2
$spec.numCoresPerSocket = 2
$spec.memoryMB = 4096
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "add"
$spec.deviceChange[0].fileOperation = "create"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device.key = -100
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$spec.deviceChange[0].device.backing.fileName = ""
$spec.deviceChange[0].device.backing.diskMode = "persistent"
$spec.deviceChange[0].device.backing.split = $false
$spec.deviceChange[0].device.backing.writeThrough = $false
$spec.deviceChange[0].device.backing.thinProvisioned = $true
$spec.deviceChange[0].device.backing.eagerlyScrub = $false
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $false
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.controllerKey = 1000
$spec.deviceChange[0].device.unitNumber = 1
$spec.deviceChange[0].device.capacityInKB = 52428800
$vmname = Get-VM -Name $vm.name
$vmid = $vmname.id
$_this = Get-View -Id $vmid
$_this.ReconfigVM_Task($spec)
}

Here you can see that we edited the script and added some lines, like line number 2,3, and 30 – 35.

Now, this script is modified to read the VM names from one CSV file, enabling it to reconfigure the memory, CPU, and HDD according to the Script.

Through the Onyx we can generate the Powershell code for the operations that we are doing through Vsphere client and by editing, we can change these code to use full scripts. This will help you to do more automation task in your Vsphere environment.

Here you can see the “vms.csv” file.

And here you can see the VMs current details after running the above script with this CSV file. To run the script call the file from Vsphere PowerCLI.

You can see the listed VMs in CSV files has been upgraded to have RAM as 4GB, and the CPU count is now 2. This is only a simple example, with onyx you can create a lot of very use full power shell code. It will help you to administrate your Vsphere environment efficiently.

Let Onyx list out the details for you, Let’s say you need to figure out how to set a VM’s MAC address. 

Old way:

  • Open Vsphere API documentation
  • Try to find a network card object
  • Discover there is no network card object, so check around in the VM object.
  • Discover the Virtual Hardware array, poke around looking for network cards
  • (about 5 more steps omitted.)

With Onyx:

  • Launch Onyx and log in.
  • Edit VM’s properties.
  • Change MAC address.
  • Look at the code that Onyx generated for you.

It’s that simple.

Now we have shown how to achieve automation in PowerCLI with a simple example. You can use Vsphere Power CLI And Project Onyx for easy automation for more complex processes very easily.  

Thanks for dropping by. Ready for the next blog?

Top 10 Factors to Consider While Choosing a Web Hosting Company

Vineesh
Author : Vineesh
Written on : 30 Jul, 2018

Who we are ?

Founded in 2010, we are a team of a sysadmins with super awesome server management skills who likes to give super quality support at super affordable price.

Services