Search
  • Admin

PowerCLI 101 - Automate that Thing!


I have not used PowerCLI/PowerShell for some time so thought I would have a refresher and include it in this blog post. I previously used CLI for handy ways to manage and automate vSphere and other VMware solutions.


Introduction


vSphere PowerCLI is a product with over 400 cmdlets based on Microsoft PowerShell. The cmdlets can be used for automating vCenter, vSphere, vSAN, vROPS, VMC on AWS, Horizon View etc.

PowerShell can be installed on Windows with PowerShell and .NET. When running commands PowerCLI communicates with the API's of the target. When using API's there is no real dependency on the GUI so for example with product version upgrades the commands will still work and be backward compatible with older versions.


Installation


To begin with ensure that you are on the latest version of PowerShell, I upgraded to 5.1. Under the download page I got 5.1 for Server 2012 R2. Once upgraded you are ready to deploy PowerCLI.


To do this launch PowerShell.


PS C:\Find-Module -Name VMware.PowerCLI


The command above will confirm access to the PowerShell Gallery. I had an out of date NuGet (Used to automate install, upgrade, configure and remove software). I hit Yes to install and get an updated version of NuGet. Once done we can go ahead and install PowerCLI (This is done via PowerShell command line with no separate download required).


PS C:\Install-Module -Name VMware.PowerCLI -Scope CurrentUser

You will see the directory in C:\Users\Username\WindowsPowerShell being populated with modules.


PowerCLI is installed so now we can create a desktop shortcut that loads the executable and imports the module.


Location of Item

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c "Import-Module VMware.PowerCLI"


Were now ready to start automating!


Connect to vCenter


To start with we can connect to vCenter, as the command shows above


PS C:\Connect VIServer YourVCServer, this will open a credential prompt. The underline shows that this is an environment input.

We can then get details on the inventory of vCenter.


Some Simple Commands


PS C:\Get-Inventory (Lists the full inventory of objects in vCenter including VMs, Folders, Clusters, Resource Pool and Hosts).


This can be narrowed down easily so for example, lets say we want to look at a specific cluster and find VMs ending with "VM".


PS C:\Get-Inventory - Location YourDC -Name *VM

A handy option is to perform a vMotion, so with the command above I have found that there is a VM ending with "VM" in the machine name. I will now vMotion this to another host.Firstly I am going to power on the VM


PS C:\Start-VM -VM NameOfVM


Then vMotion with the following


PS C:\Get-VM -Name NameOfVM | Move-VM -Destination TargetHost


To view and manage snapshot's you can use the following


PS C:\Get-VM | Get-Snapshot | Select VM,Name,Description


To then write this information out to a file we can use the Out-File cmdlet


PS C:\Get-VM | Get-Snapshot | Select VM,Name,Description | Out-File C:\VMware_Snapshots.txt


To get things a little more automated we can read from a file into a variable and then output.


PS C:\$VMList = Get-Content C:\VMware_VMs.txt

Get-VMGuest -VM $VMList | Select-Object VMName,State,IPAddress,OSFullName


Creating a VM is really simple


PS C:\New-VM -Name 'TestVM' –VMHost 'VMHost-1' -Datastore 'TestDatastore' -DiskGB 40 -MemoryGB 8 -NumCpu 2 -NetworkName 'Virtual Machine Network'


Scheduling the Scripts


When you have the required commands in place you can add them to a PowerShell script and add the .ps1 extension. This can then be scheduled in Task Scheduler


Just ensure you have the following before scheduling


Get-Module -Name VMware* -ListAvailable | Import-Module

Connect-VIServer -Server YourVC


There is a huge amount of amazing PowerCLI scripts with GitHub having some really good examples


This is just a very high level refresher on PowerCLI, stay tuned as I will be expanding on this post in the future with more detail!

0 views