VCAP6-DCV Deployment – Objective 5.1 – Execute VMware Cmdlets and Customize Scripts Using PowerCLI


Main Study Page

Objectives 5.1 are broke down as the following

  • Install and configure vSphere PowerCLI
  • Use basic and advanced PowerCLI Cmdlets to manage a vSphere deployment
  • Analyze a sample script, then modify the script to perform a given action
  • Use PowerCLI to configure and administer Auto Deploy (including Image Builder)
  • Create a report from a PowerCLI script

Install and configure vSphere PowerCLI

I will be using the latest version of vSphere PowerCLI as of the time of writing which is version 6.3 Release 1 running on a Windows box.  Supported OS for this release include Windows 2012 R2, Windows 2008 R2 SP1, Windows 10, Windows 8.1 and Windows 7 SP1.  Also make sure Powershell and .Net 4.5 are installed.  The install itself is straightforward.

Run the installer and pick which modules you want to install

vcap5.1-1  vcap5.1-2

I then need then set the execution policy of Powershell to RemoteSigned to be able to run scripts and load configuration files.  I need to open PowerCLI and run

>Set-ExecutionPolicy RemoteSigned

vcap5.1-3

For the exam its unclear how much configuration is required for PowerCLI.  As PowerCLI is started up the default modules are loaded from the Initialize-PowerCLIEnvionment.ps1 script located in the install directory script folder.

vcap5.1-4

To add a custom script to run after this script, copy the script into this folder and call it Initialize-PowerCLIEnvionment_Custom.ps1.  On startup the Initialize-PowerCLIEnvionment.ps1 will automatically run another script with this name should one exist.

vcap5.1-5


Use basic and advanced PowerCLI Cmdlets to manage a vSphere deployment

I will go over some example Cmdlets but it would be impossible to show them all, people have wrote books on the subject and it difficult to know what you will be asked to do in the exam.  Auto Deploy configuration via PowerCLI falls into the remit so I would expect that to be included.  Pluralsight have an advanced management course on the subject that can be found here.  The VMware documentation can be found here.  Regardless of the questions that might get asked, if you can use PowerCLI you can save a lot of time in the exam instead of waiting for the Web Client to respond / refresh.

First I need to connect to vCenter

>connect-VIServer -Server lab-vc01.vjenner.com -User Administrator@vsphere.local -Password Password123!

vcap5.1-6

The following powers on a VM – migrates it to another host then initiates a guest shutdown

>Get-VM Lab-DB01 | Start-VM

>Get-VM Lab-DB01 -Location lab-esxi01.vjenner.com | Move-VM -Destination lab-esxi02.vjenner.com

>Stop-VMGuest Lab-DB01

vcap5.1-7

To put a host into maintenance and exit I run

>Set-VMHost -VMHost lab-esxi-01.vjenner.com -State “Maintenance” -RunAsync

>Set-VMHost -VMHost lab-esxi-01.vjenner.com -State “Connected” -RunAsync

vcap5.1-8

To set a varible for the host object I run

>$vmhost = Get-VMHost -Name lab-esxi-01.vjenner.com

To clone out a template from a VM I can run

>New-Template -VM Lab-DB01 -Name Windows_Template -Location “Template Folder” -Datastore lab-vmfs-01

vcap5.1-11

To create a snapshot I can run

>New-Snapshot -VM Lab-DB01 -Name PreUpgradeSnapshot

vcap5.1-12

To create a new distributed switch under my Production datacenter I run the following

>New-VDSwitch -Name PowerCLIvDS -Location Production

I then need to add the host to the vDS

>AddVDSwitchVMHost -VDSwitch PowerCLIvDS -VMHost lab-esxi02.vjenner.com

I then can add an uplink but to do this I first create a variable that captures the host I want with the NIC I want to use as an uplink.

>$hostPhysicalNic = Get-VMhost -Name lab-esxi-02.vjenner.com | Get-VMHostNetworkAdapter -Name vmnic7 

Once I have the variable I run the following

>Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $hostPhysicalNic -DistributedSwitch PowerCLIvDS

vcap5.1-13

To create a new customization and deploy a VM from template I can run the following, first I create the customization

>New-OSCustomizationSpec -Type NonPersistent -Name Spec -OSType Windows – Workgroup vJenner.com -OrgName vJenner.com -Fullname -vJenner -ProductKey 1111.1111.1111.1111.1111 -ChangeSid -TimeZone “Central European” -NamingScheme VM

vcap5.1-19

Then to add a static IP address I run

>Get-OSCustomizationNicMapping -Spec Spec | Set-OSCustomizationNicMapping -Ipmode UseStaticIP -IPaddress 192.168.0.246 -SubnetMask 255.255.255.0 -DefaultGateway 192.168.0.1 -DNS 192.168.0.200

vcap5.1-20

Then to deploy a VM with this customization I run

>New-VM -Name PowerCLI04 -VMHost lab-esxi-01.vjenner.com -OSCustomizationSpec Spec -Template Win2012Template

vcap5.1-21


Analyze a sample script, then modify the script to perform a given action

See VMware’s documentation for examples of scripts here, I will illustrate some of these examples.

To create a VM from a XML file first create a xml file with the settings for the VM.  I created a very basic on that only specifies the name and size of the vDisk.

vcap5.1-9

Save this to the PowerCLI machine and I run the following

>[xml]$s = Get-Content C:\OSFiles\newvm.xml

>$s.CreateVM.VM | foreach {New-VM -VMHost $vmHost1 -Name $_.Name -DiskGB $_.HDDCapacity -Datastore lab-vmfs-04}

vcap5.1-10

I can then expand the xml file to include a host, set the disk to be thin provisioned and set the CPU and memory settings.

vcap5.1-14

I then need to amend the script to match the entries in the xml file.

vcap5.1-15

If I want to set a VM share resources using a script I can do the following.  First I check what shares have already been assigned

>Get-VMResourceConfiguration -VM Lab-01

>Get-VMResourceConfiguration -VM Lab-DB01 | fc -Property DiskResourceConfiguration

vcap5.1-16

I then run the following to set the memory shares to Low and CPU shares to High

>Get-VM Lab-DB01 | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemSharesLevel Low -CpuSharesLevel High

vcap5.1-17

Then I set the disk shares, I need to set some variables here once I have those set I run the below command

>$vm1 – Get-VM Lab-DB01

>$vm1disk = Get-HardDisk $vm1

>Get-VMResourceConfiguration $vm1 | Set-VMResourceConfiguration -Disk $vm1disk -DiskSharesLevel custom -NumDiskShares 300

vcap5.1-18


Use PowerCLI to configure and administer Auto Deploy (including Image Builder)

PowerCLI is critical for using Auto Deploy, its here you have to create image profiles and create deploy rules to build your hosts.  I have covered this in a previous exam objective, see here.  At that point I didnt cover the Image Builder which I will illustrate it here.  Image Builder can be used to update an existing image profile to include new or updated vibs, this can be from VMware from a new release of ESXi or from a 3rd party vendor that has updated one of their vibs.

First I connect to vCenter using PowerCLI and see what image profiles I have previously configured, I have one configured called LabProfiles6.0 which is using the ESXi 6.0 GA release image.

>Get-EsxImageProfile

vcap5.1-32

For this example I have downloaded a newer version of ESXi and add it as a software depot.

>Add-EsxSoftwareDepot C:\OSFiles\ESXi-6.0.0.20160301001s.zip

vcap5.1-33-1

I then want to clone out my existing image profile to a new one that I want to use for the updated profile.

>New-EsxImageProfile -CloneProfile LabProfiles6.0 -Name LabProfiles6.0Update

vcap5.1-33

For this example I want to add the updated esx-base, first I to find the build number

>Get-EsxSoftwarePackage -name esx-base

vcap5.1-34

I then need to use the build number and add it to the following command

>Add-EsxSoftwarePackage -ImageProfile LabProfiles6.0Update -SoftwarePackage “esx-base 6.0.0-1.31.3568943”

vcap5.1-35

I can then export this profile to a .zip file if I want to or use it for a deployment rule.

You can also connect directly to the vendor software depot and export the updated package that way, VMware’s is available at https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml.  

First add the remote depot

>Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml

Then check what profiles are available and find the version you want to export

>Get-EsxImageProfile -name “ESXi6*”

vcap5.1-29

Find the version to export and run

>Export-EsxImageProfile -ImageProfile ESXi6.0.0-20160301001s-standard -FilePath C:\OSFiles\ESXi-6.0.0.20160301001s

vcap5.1-30

A remote software depot can be used for the image builder the same way as a local depot.


Create a report from a PowerCLI script

PowerCLI commands can be exported into different formats for creating reports, the following commands are the export options.

  • Export-Csv
  • Export-Clixml
  • ConvertTo-Csv
  • ConvertTo-Html
  • ConvertTo-Xml

To export all VM information to a CSV file I can run

>Get-VM | Export-CSV C:\OSFiles\Export.csv -NoTypeInformation

vcap5.1-22

Output looks like this

vcap5.1-23

To export all host information within a cluster to HTML I can run

>Get-Cluster “Compute Cluster 1” | Get-VMHost | ConvertTo-Html | Out-File C:\OSfiles\Hostincluster.html

vcap5.1-24

Output looks like this

vcap5.1-25

To get a list of powered on VMs and to limit the output to CSV I can run the following

>Get-VM | Where { $_.PowerState -eq “PoweredOn”} | Select Name, NumCPU, MemoryMB, PowerState, VmHost | Export-CSV C:\OSFiles\Export4.csv

vcap5.1-26

Output looks like this

vcap5.1-27

Leave a comment

Your email address will not be published. Required fields are marked *