Objective 7.1 – Execute Vmware Cmdlets and Customize Scripts Using PowerCLI

 

The Objectives of 7.1 are probably the hardest to summarise within a blog post, the possibilities of PowerCLI & scripting are so vast that no matter how much content I put below I simply wouldn’t do the objective much justice.

With that said, nearly all of the objectives set out within this blueprint can be accomplished using PowerCLI. Spending time understanding the syntax of PowerCLI will help you accomplish tasks quicker & more efficiently when it comes to exam time.

Since PowerCLI is built on Powershell, learning Powershell is essential. Of course this is of benefit if (like me) you’re a MS Windows admin as well as a VMware admin.

There’s a number of good books to learn Powershell including

Learn Windows Powershell in a month of Lunches by Don Jones & Jeffery Hicks the two leading MVPs in Powershell

http://www.amazon.co.uk/Learn-Windows-PowerShell-Month-Lunches/dp/1617291080

In addition to that, the MVA (Microsoft Virtual Academy) has a number of free courses relating to PowerShell, the one below is relating to PowerShell 3.0 and is presented by the Lead PowerShell Architect Jeffrey Snover

http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start

It’s a great way to introduce yourself to the powershell Syntax if you’ve never used it before and will aid you greatly when it comes to using PowerCLI.

  • Identify vSphere PowerCLI requirements

Prerequisites for Installing and Running vSphere PowerCLI

Before installing and running vSphere PowerCLI, verify that you have installed the required software on the same machine.

If you want to work with VMware vSphere PowerCLI 5.5 Release 2, make sure that the following software is present on your system:

  •  Windows PowerShell 2.0
  •  A supported version of .NET Framework
  • .NET Framework 2.0 with Service Pack 2
  • NET Framework 3.0 or .NET Framework 3.0 with Service Pack 1, or Service Pack 2
  • NET Framework 3.5 or .NET Framework 3.5 with Service Pack 1

OS Type 64-Bit

  • Windows Server 2012 R2
  •  Windows Server 2008 R2 Service Pack 1
  •  Windows 8.1
  • Windows 7 Service Pack 1

You can run VMware vSphere PowerCLI 5.5 Release 2 guest cmdlets against virtual machines with the following guest operating systems:

  • Windows Server 2008 R2 Service Pack 1 (64-bit)
  •  Red Hat Enterprise Linux 5.10 (64-bit)

 

  • Identify Cmdlets concepts

vSphere PowerCLI cmdlets are created to automate VMware environments administration and to introduce some specific features in addition to the PowerShell concepts.

vSphere PowerCLI Concepts can be found here

  • Identify enviroment variables usage
  • Install and configure vSphere PowerCLI
  • Install and configure Update Manager Powershell Libary
  • Use basic and advanced Cmdlets to manage VMs and ESXi Hosts

If you’ve watched the videos above and have managed to learn the syntax  you should be able to perform basic functions using PowerCLI.

Really the best way to learn is to build a lab exclusively using PowerCLI. Adding hosts to the Virtual Center, Setting host configurations etc etc.

Powercli becomes extremely powerful when you start piping commands to perfom bulk reconfiguration or creation tasks, I imagine that this is probably the only way they’ll be able to test your PowerCLi knowledge within the VCAP-DCA exam, performing tasks that would take too long to manually complete.

Come of the most useful commands to know are as follows

Get-vicommand – This will return all the VMware specfic commands (if you simply run a get-command then you will also get Windows Powershell commands)

You can refine the search by using wild cards

Get-Vicommand *host* This will return all the cmdlets containing the word “host” for example, the Get-VMHost, Set-VMHost, Move-VMhost etc.

This will help in the exam when searching for the command you want to use. Once you have found the command you’re looking for you can run a Get-Help command against it

Get-Help Set-VMhost will return the basic help file for VMhost. At the bottom of the help file you’ll see a list of “REMARKS”

cmdlethelp

They’re self explanitory, the -Examples will return examples of the cmdlet, -full will return the entrie help file.

Get-Help Set-VMhost -full

In addition to this you may wish to find the properties of a CMDLET so you know what command options you have.  Here we use the Get-Member piped into your specified cmdlet for example

Get-VM | Get-Member

This returns all methods & properties of the CMDLET.

get-member

Once we know the properties we can return using the get-vm command, you can specifiy these when calling the cmdlet

Get-VM | select Name, UsedSpaceGB, ProvisionedSpaceGB

This will return the used space & the provisioned space of a Virtual machine along with the VMs name.

The data may not be readable so you may have to format the output.

Get-VM | select Name, UsedSpaceGB, ProvisionedSpaceGB | FL This command will format the output as a List which maybe more readable in this instance.

The idea when learning powercli is to imagine exam scenerios and try and apply your knowledge.

Imagine a question where you’re asked to ensure all  CDDrives on all virtual machines in the enviroment dont have any media attached.

Let me work through this scenerio.

First stage would be we want to get all Virtual Machines, which would be the Get-VM command

Get-VM

This will return all virtual machines, we could in the scenrio above use the Get-VM * to return all virtual machines or use the wild card to return only virtual machines with a certain name. For instance, if we had to reconfigure 100 virtual machines that had a naming convention of UKDEV01, UKDEV02…UKDEV100 then we could run the Get-VM UKDEV* command to return all those virtual machines

Once we have all the virtual machines, we want to get the CDDrive of each Virtual machine

Get-VM | Get-CDDRIVE

get-cd

The output from this command is not great, it simply shows us the first 3 CD-DRIVE settings of each VM. Ideally here we want a list of all CD-DRIVE settings per virtual machine.

In this instance we can run a Get-VM |Get-CDDRIVE | Select * This will return all the settings rather than the first 3 shown above. Piping outputs to Select * is useful in a number of scenerios as we can then select the settings we’d like returned once we have a list of those availble.

Get-VM | Get-CDDRIVE | Select *

Select.

In the output above we can see the Virtual Machine name is listed as “parent” and the CD Drive is listed as “Name”

IsoPath, HostDevice are the settings within the VM CD-Drive

cddriveout

So we could run the following

Get-VM | Get-CDDRIVE | select Parent, Name, IsoPath, HostDevice, ConnectionState

Which will return the name of the VM, the name of the CD-DRIVE, whether its connfigured to connect to the HostDevice whether it has an ISOPath attached and also the connectionState details (not Connected etc)

cdv

For Instance if we wanted to detach all media from all the VMs we could run

Get-VM | Get-CDDRIVE | Set-CDDRIVE -NoMedia

Running the Get-VM | Get-CDDRIVE | select Parent, Name, IsoPath, HostDevice, ConnectionState again will show us that the HostDevice is now removed

nomedia

We could go further and ensure all CD Drives are disconnected within the Virtual Machine

Get-VM | Get-CDDrive | Set-CDDrive -Connected $False -StartConnected $False

Further still by removing the CD Drive alltogether

Get-VM | Get-CDDrive | Remove-CDDrive

Other Scenerios may ask you to create multivirtual machines with certain settings, here we can use a command simiailr to the following (which would create 150 Virtual Machines with the prefixed name MYVM, so MYVM1, MYVM2… MYVM150

1..150 | foreach {New-VM -Name MYVM$_ -VMHost MyHost -Datastore Datastore1 -Template MyWin7Template -OSCustomizationSpec MyOSCustSpec}

You could of course specifiy compute resource settings rather than build multiple VMs from a template, but you get the idea.

  • Use Web Servcie Access Cmdlets
  • Use Datastore and Inventory Providers
  • Given a samle script, modify the script to perform a given action

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s