The next installment of my “Back to Basic” series about doing the typical “post-configuration” of vCenter/ESX this time without web-client, but instead uses VMware’s PowerCLI extensions to Microsoft’s PowerShell. It does include “bulk administration” methods which can substantially reduce the time it takes to complete repetitive tasks.

The article covers tasks such as:

  • Creating Datacenters
  • Adding ESX hosts (various methods!)
  • Creating VM Folders
  • Adding vCenter/vCloud License Keys
  • Assigning Licenses
  • Enabling NTP
  • Adding ESX hosts to Microsoft’s Active Directory

I’ve deliberately kept the PowerCLI as simple as possible. The assumption here is you have already cranked up PowerCLI and connected to the vCenter with the connect-viserver command. I’m not saying this is the “best” way to do your PowerCLI. Many people will want to add filters, error checking and messages to indicate what’s happening. I like to keep my PowerCLI very simple and crude. It means if someone else is reading its easy for them to decode…

Many thanks to Alan Renouf (and others) who helped me put this together…

Creating vCenter Datacenters

Screen Shot 2013-10-21 at 14.01.16.png

 Creating Datacenter Example:

New-Datacenter -Location (Get-Folder -NoRecursion) -Name “New York”

Adding VMware ESX hosts

Adding ESX host using PowerCLI can be done in a number of ways. Individually, by specifying each ESX hosts, or in a more bulk methods. One bulk method is to use a .CSV file to hold the names of the ESX hosts you wish to add, another uses a for each loop to add ESX hosts from 01-08. This methods assumes you have a naming convention that includes an number to differentiate one host from another such as esx01nyc, esx02nyc, esx03nyc and so on. The add-vmhost cmdlet does not support the enablement of the lockdown feature – and if required has to be configured as separate PowerCLI step.

Screen Shot 2013-10-21 at 14.01.16.png

Simple Add and Lockdown Example

add-vmhost esx01nyc.corp.com -location “New York” -user root -password Password1 -force:$true
(get-vmhost esx01nyc.corp.com| get-view).EnterLockdownMode()

Screen Shot 2013-10-21 at 14.01.16.png

Bulk Add by using a unique number

In this script ESX hosts are added by their unique number. The script loops round from 1 to 9 times using a “range” in PowerShell, adding esx01nyc, esx02nyc and so on. The “{0:00}” handles the situation where the naming convention contains a leading zero in the series such as 01, 02, 03, 04, 05, 06, 07, 08, 09, 10 and so on…

Acknowledgement: This script to was written with assistance from Alan Renouf. Alan’s personal blog is available at http://virtu-al.net and he tweets as @AlanRenouf

1..16 | Foreach {
$Num = “{0:00}” -f $_
add-vmhost esx”$Num”nyc.corp.com -location “New York” -user root -password Password1 -force
}

Screen Shot 2013-10-21 at 14.01.16.png

Bulk Add of ESX hosts by CSV File Example

Create a a CSV file called vmhosts.csv and formatted like so. CSV files can be populated with any number of variables that can be in turn called by your PowerCLI scripts. PowerCLI scripts are merely text files saved with the .PS1 extension, and executed from the PowerCLI prompt with a ./addhost.ps1 command.

Screen Shot 2013-11-04 at 10.29.18.png

Then create an addhosts.ps1 script file with a text editor:

Import-CSV c:\vmhosts.csv | ForEach-Object {
$hostname = $_.vmhost
add-vmhost $hostname -location “New York” -user root -password Password1 -force
}

Below is the results of the script using the CSV method to bulk add many ESX hosts:

Screen Shot 2013-11-04 at 11.01.48.png

Creating vCenter Folder Structure

Creating folders for VMs and Templates can be carried out using the New-Folder cmdlet. The default location if no location is specified is the “Hosts & Clusters” view in vCenter. Using a combination of Get-Datacenter and Get-Folder cmdlet we can force the folder to be created any view that supports the creation of folders. For example the following scripts creates this folder structure in the “VM & Templates” view.

It uses the variable $toplevel to find the upmost location in the inventory in a datacenter called “New York”, and then re-uses that variable to create the top level folder structure:

Screen Shot 2013-11-04 at 12.33.43.png

Screen Shot 2013-10-21 at 14.01.16.png

Simple VM Folder Creation Example

$toplevel = (Get-Datacenter “New York” | Get-Folder -name vm)
New-Folder -name “CorpHQ” $toplevel
New-Folder -name “COIG” $toplevel
New-Folder -name “iStocks” $toplevel
New-Folder -name “Quark” $toplevel
Remove-Folder -Folder “Discovered Virtual Machine” -Confirm:$False

Note: Remove-Folder supports a -DeletePermanently switch which could be dangerous. The Remove-Folder cmdlet can also delete and destroy VMs that are contained within the folder. Use with caution.

Licensing vCenter and ESX Hosts

Screen Shot 2013-10-21 at 14.01.16.png

Adding Licenses to vCenter

$si = Get-View ServiceInstance

$LicManRef=$si.Content.LicenseManager
$LicManView=Get-View $LicManRef
$license = New-Object VMware.Vim.LicenseManagerLicenseInfo
$license.LicenseKey = “XXXXX-XXXXX-XXXXX-XXXXX-XXXXX”
$license.EditionKey=”esxEnterprisePlus”
$LicManView.AddLicense($license.LicenseKey,$null)

$si = Get-View ServiceInstance
$LicManRef=$si.Content.LicenseManager
$LicManView=Get-View $LicManRef
$license = New-Object VMware.Vim.LicenseManagerLicenseInfo
$license.LicenseKey = “XXXXX-XXXXX-XXXXX-XXXXX-XXXXX”
$license.EditionKey=”esxEnterprisePlus”
$LicManView.AddLicense($license.LicenseKey,$null)

The result of this script outputs like so:

Screen Shot 2013-11-04 at 15.23.01.png
Screen Shot 2013-10-21 at 14.01.16.png

Assigning Licenses to ESX hosts

Foreach ($vmhost in (get-vmhost))
{

$targethostMoRef = (get-VMHost $vmhost | get-view).MoRef
$si = Get-View ServiceInstance
$LicManRef=$si.Content.LicenseManager
$LicManView=Get-View $LicManRef
$licassman = Get-View $LicManView.LicenseAssignmentManager
$licassman.UpdateAssignedLicense($targethostMoRef.value,”XXXXX-XXXXX-XXXXX-XXXXX-XXXXX”,”VMware vCloud Suite Enterprise”)

}

The result of this scripts outputs like so:

Screen Shot 2013-11-04 at 15.23.01.png

Screen Shot 2013-10-21 at 14.01.16.png

Retrieving License Data

Acknowledgement: This script to retrieve licensing data originally appeared in the official VMware PowerCLI blog and was written by Alan Renouf. Alan’s personal blog is available at http://virtu-al.net and he tweets as @AlanRenoufhttp://blogs.vmware.com/vipowershell/2012/05/retrieving-license-keys-from-multiple-vcenters.html

# Get the license info from each VC in turn
$vSphereLicInfo = @()
$ServiceInstance = Get-View ServiceInstance
Foreach ($LicenseMan in Get-View ($ServiceInstance | Select -First 1).Content.LicenseManager) {
Foreach ($License in ($LicenseMan | Select -ExpandProperty Licenses)) {
$Details = “” |Select VC, Name, Key, Total, Used, ExpirationDate , Information
$Details.VC = ([Uri]$LicenseMan.Client.ServiceUrl).Host
$Details.Name= $License.Name
$Details.Key= $License.LicenseKey
$Details.Total= $License.Total
$Details.Used= $License.Used
$Details.Information= $License.Labels | Select -expand Value
$Details.ExpirationDate = $License.Properties | Where { $_.key -eq “expirationDate” } | Select -ExpandProperty Value
$vSphereLicInfo += $Details
}
}
$vSphereLicInfo | Format-Table -AutoSize

Enabling Network Time Protocol (NTP) Settings

Screen Shot 2013-10-21 at 14.01.16.png

Enabling NTP Service

The cmdlet Add-VmhostNtpServer and Get-VMHostService can be used to both configure the NTP servers, the start-up policy as well as starting the service.

Foreach ($vmhost in (get-vmhost))
{
Add-VmHostNtpServer -NtpServer “0.north-america.pool.ntp.org”,”1.north-america.pool.ntp.org” -VMHost $vmHost | Out-Null
Get-VMHostService -VMHost $vmHost | where{$_.Key -eq “ntpd”} | set-vmhostservice -policy “on” -Confirm:$false
Get-VmHostService -VMHost $vmHost | Where-Object {$_.key -eq “ntpd”} | Start-VMHostService -Confirm:$false | Out-Null
}

Screen Shot 2013-10-21 at 14.01.16.png

Confirming NTP Service has started & configured correctly

Confirming the NTP Service has started and viewing its configuration can be achieved with this PowerCLI “one-liner”. This was found on Alan Renouf’s website – http://www.virtu-al.net/2009/08/14/powercli-do-you-have-the-time/

Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}}

Screen Shot 2013-11-11 at 13.43.04.png

Adding ESXi hosts to Microsoft Active Directory Domain

Screen Shot 2013-10-21 at 14.01.16.png

Adding ESXi Host to Active Directory & Assigning a Group Permissions

Foreach ($vmhost in (get-vmhost))
{
Get-VMHostAuthentication -VMHost $vmhost | Set-VMHostAuthentication -Domain corp.com -User administrator -Password Password1 -JoinDomain -Confirm:$false
}