PART I GH TE D MA TE RI AL Install, Configure, and Manage the vSphere Environment CHAPTER 1: RI AUTOMATING VSPHERE HYPERVISOR DEPLOYMENT AND CONFIGURATION CO PY CHAPTER 2: AUTOMATING VCENTER SERVER DEPLOYMENT AND CONFIGURATION CHAPTER 3: AUTOMATING STORAGE AND NETWORKING CHAPTER 4: USING ADVANCED VSPHERE FEATURES
CHAPTER 1 Automating vCenter Server Deployment and Configuration IN THIS CHAPTER, YOU WILL LEARN TO: PREPARE THE VCENTER SERVER INSTALLATION 4 CREATE AN AUTOMATED INSTALLATION 5 SET UP YOUR VCENTER SERVER FOLDER STRUCTURE 8 Creating a Folder Structure from Scratch . . . . . . . . . . . . . . . . . . .8 Exporting a Folder Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Importing a Folder Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CHAPTER 1 O ne of the focal points and key use cases of PowerCLI is the automation of tasks that are needed either as part of a disaster recovery (DR) solution or as part of an automated deployment solution that can be used repeatedly—You will be safe in the knowledge that the script will produce a consistent and easy-to-use solution. This chapter will take you through some common areas automated within vSphere, starting at the beginning of the virtual infrastructure.
The system is joined to a domain, and not a workgroup. While this is not a strict requirement, domain membership ensures that when you’re using advanced features like the vCenter Guided Consolidation Service, the vCenter Server will be able to find all domains and systems on the network for the purpose of converting physical systems to virtual machines (VMs). A supported database is already available, unless you’re using the bundled SQL Server 2005 Express Edition.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N items clearly defined within the script, using a script like this ensures each installation is configured correctly and no mistakes are made. LISTING 1.
C R E AT E A N AU T O M AT E D I N S TA L L AT I O N 7 $DrvPath = “C:\WINDOWS\system32\sqlncli10.dll” “Server” $DBSrv Install, Configure, and Manage the vSphere Environment New-Regkey “HKLM:SOFTWARE\ODBC\ODBC.INI\$ODBCName” ` New-RegKey “HKLM:SOFTWARE\ODBC\ODBC.INI\$ODBCName” ` PART I “Driver” $DrvPath New-RegKey “HKLM:SOFTWARE\ODBC\ODBC.INI\$ODBCName” ` “Description” $ODBCName New-RegKey “HKLM:SOFTWARE\ODBC\ODBC.INI\$ODBCName” ` “LastUser” $DBUser New-RegKey “HKLM:SOFTWARE\ODBC\ODBC.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N To add the Host Update Utility component, try this next code: # Install vCenter Client with Host Update Utility Write-Host “Installing vCenter Client with Host Update Utility” Invoke-Item “$VCMedia\vpx\VMware-viclient.exe /s /w /v /qn ` /L*v %TEMP%\vmvcc.
S E T U P YO U R VCE NTE R S E RVE R FO LD E R STR UC TU R E Sample CSV layout Install, Configure, and Manage the vSphere Environment F I G U R E 1.1 9 PART I In the example CSV file we created, there are two columns. The first column, Name, is used to define the name of the folder that you wish to create. The second column, Path, is used to show the path to where this folder is to be created in vCenter Server. As seen in Figure 1.1, in the Path column all entries begin with vm\.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N .EXAMPLE 1 PS> Import-Folders -FolderType “Blue” -DC “DC01” ` -Filename “C:\BlueFolders.csv” .EXAMPLE 2 PS> Import-Folders -FolderType “Yellow” -DC “Datacenter” -Filename “C:\YellowFolders.
S E T U P YO U R VCE NTE R S E RVE R FO LD E R STR UC TU R E 11 Both yellow and blue folder views can be exported to a CSV file. You will find this technique useful when you are rebuilding your vCenter Server from scratch or creating a DR replica of the current virtual infrastructure. The script in Listing 1.3 can be used to export either a blue or a yellow folder structure to a CSV.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N Function Export-Folders { <# .SYNOPSIS Creates a csv file of folders in vCenter Server. .DESCRIPTION The function will export folders from vCenter Server and add them to a CSV file. .NOTES Source: Automating vSphere Administration Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd, Alan Renouf, Glenn Sizemore .PARAMETER FolderType The type of folder to export .
S E T U P YO U R VCE NTE R S E RVE R FO LD E R STR UC TU R E 13 $Report | foreach { if ($type -eq “vm”) { $_.Path = ($_.Path).Replace($dc + “\”,”$type\”) } } $report | Export-Csv $filename -NoTypeInformation } Install, Configure, and Manage the vSphere Environment get-folder | Get-Folderpath PART I } Function Export-VMLocation { <# .SYNOPSIS Creates a csv file with the folder location of each VM. .DESCRIPTION The function will export VM locations from vCenter Server and add them to a CSV file. .
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N } } Export-Folders “Blue” “DC01” “C:\BlueFolders.csv” Export-VMLocation “DC01” “C:\VMLocation.csv” Export-Folders “Yellow” “DC01” “C:\YellowFolders.csv” Importing a Folder Structure You can import an existing blue or yellow folder structure into vCenter Server using the Import-Folders function previously shown in Listing 1.2.
DEFINE USERS AND THEIR PRIVILEGES 15 $key = @() Install, Configure, and Manage the vSphere Environment ) $key = PART I Process { $Report = @() $Report = import-csv $filename | Sort-Object -Property Path foreach($vmpath in $Report){ Split-Path $vmpath.Path | split-path -leaf Move-VM (get-datacenter $dc ` | Get-VM $vmpath.Name) ` -Destination (get-datacenter $dc | Get-folder $key) } } } Import-VMLocation “DC01” “C:\VMLocation.
CHAPTER 1 • F I G U R E 1. 2 AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N vCenter Server Privileges Roles are listed here Privileges are listed here How many privileges are there? Think of any action you have ever performed in the vCenter Client. Think about the actions you have not yet come across or used in your everyday job. Now add them up, and you will have some idea of how many privileges there are in vCenter Server.
Capacity planning Discover and convert physical host t... Script action Schedule an external script action Proxy Add or remove endpoints to or from t...
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N You can view which groups (collections of privileges) are available by using the Get-VIPrivilege cmdlet with the -PrivilegeGroup parameter, as shown here: [vSphere PowerCLI] C:\> Get-VIPrivilege -PrivilegeGroup | ` Select Name, Description Name Description ---- ----------- System System Global Global Folder Folder Datacenter Datacenter Datastore Datastore Network Networks vNetwork
Manage Baseline Manage baselines Upload file Upload file Configure General VMware vCenter Upd... Manage Patches and Upgrades Manage virtual machine and... Creating New Roles Roles can be found in the vSphere Client whenever you add a new permission. The Assigned Role drop-down box in the Assign Permissions dialog box shown in Figure 1.3 lists your existing roles. F I G U R E 1.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N VirtualMachinePowerUser Provides virtual machine interaction... VirtualMachineUser Provides virtual machine interaction... ResourcePoolAdministrator Supports delegated resource management VMwareConsolidatedBackupUser Used by the Consolidated Backup utility DatastoreConsumer Assigned to datastores to allow crea... NetworkConsumer Assigned to networks to allow associ...
A new role can also be created at a granular level. First choose the privileges you want to use: $Priv = @() $MyPriv = “Profile”, “VCIntegrity.Baseline”, ` “VApp.Move”, “Profile.Clear” And then add each of them into a custom object: Foreach ($CustPriv in $MyPriv){ $Priv += Get-VIPrivilege | Where {$_.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N After you’ve set up and tested individual permissions, you can export them to a readable, importable format. This eases multiple installations and the transfer of permissions to further vCenter Servers, and ensures consistency as well. We’ll show you how next. Exporting Permissions The script in Listing 1.
DEFINE USERS AND THEIR PRIVILEGES 23 $report = @() $row.Name = $foldername Install, Configure, and Manage the vSphere Environment foreach($perm in $permissions){ $row.Principal = $perm.Principal PART I $row = “” | select EntityId, Name, Role, ` Principal, IsGroup, Propagate $row.EntityId = $perm.EntityId $Foldername = (Get-View -id $perm.EntityId).Name $row.Role = $perm.Role $row.IsGroup = $perm.IsGroup $row.Propagate = $perm.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N $report | export-csv $Filename -NoTypeInformation } } Export-PermissionsToCSV -Filename “C:\Temp\Permissions.csv” Importing Permissions It is equally important to be able to import the permissions back into your vCenter Server. You can use the script in Listing 1.7. LISTING 1.7 Importing permissions function Import-Permissions { <# .SYNOPSIS Imports all Permissions from CSV file .
DEFINE USERS AND THEIR PRIVILEGES 25 $permissions = Import-Csv $Filename foreach ($perm in $permissions) { $entity = “” $entity = New-Object VMware.Vim.ManagedObjectReference $object = Get-Inventory -Name $perm.Name if($object.Count){ $object = $object | where {$_.Id -eq $perm.EntityId} } if($object){ switch -wildcard ($perm.EntityId) { Folder* { $entity.type = “Folder” $entity.value = $object.Id.Trimstart(“Folder-”) } VirtualMachine* { $entity.Type = “VirtualMachine” $entity.value = $object.Id.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N } else { $setperm.propagate = $false } $doactual = Get-View -Id ` ‘AuthorizationManager-AuthorizationManager’ Write-Host “Setting Permissions on ` $($perm.Name) for $($perm.principal)” $doactual.SetEntityPermissions($entity, $setperm) } } } } Import-Permissions -DC “DC01” -Filename “C:\Temp\Permissions.
C O N F I G U R E DATAC E N T E R S A N D C L U S T E R S 27 Datacenters are generally created as part of the initial setup process. The setup can be automated by using the following code, which will create a datacenter called Boston and store it in a variable.
CHAPTER 1 • LISTING 1.
To complete this same action on an existing cluster, you would again need to retrieve the cluster object and push the object through the pipe into the Set-Cluster cmdlet, as shown in Listing 1.11. LISTING 1.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N .DESCRIPTION This function will allow you to configure DPM on an existing vCenter Server cluster .NOTES Source: Automating vSphere Administration Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd, Alan Renouf, Glenn Sizemore .PARAMETER Cluster The cluster on which to set DPM configuration .
LICENSING 31 $spec = New-Object vmware.Vim.ClusterConfigSpecEx $spec.dpmConfig = New-Object VMware.Vim.ClusterDpmConfigInfo $spec.DpmConfig.DefaultDpmBehavior = $DPMBehavior $spec.DpmConfig.Enabled = $Enabled $clus.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N .NOTES Source: Automating vSphere Administration Authors: Luc Dekens, Arnim van Lieshout, Jonathan Medd, Alan Renouf, Glenn Sizemore .EXAMPLE 1 PS> Get-LicenseKey #> Process { $servInst = Get-View ServiceInstance $licMgr = Get-View ` (Get-View ServiceInstance).Content.licenseManager $licMgr.
LICENSING 33 LicenseKey : AAAAA-BBBBB-CCCCC-DDDDD-EEEEE EditionKey : vc Name : vCenter Server 4 Standard Total : 0 Used : 1 Install, Configure, and Manage the vSphere Environment DynamicProperty : CostUnit : server PART I Properties : {ProductName, ProductVersion, feature...} Labels : DynamicType : DynamicProperty : Licensing a Host Once you have a list of the keys, you can use that information to license the ESX hosts attached to the vCenter Server. Listing 1.
CHAPTER 1 • AU T O M AT I N G VC E N T E R S E R V E R D E P L OY M E N T A N D C O N F I G U R AT I O N -VMHost “esxhost01.mydomain.com” ` -Name $null #> param( [String]$VMHost, [String]$LicKey, [String]$Name ) Process { $vmhostId = (Get-VMHost $VMHost | Get-View).Config.Host.Value $servInst = Get-View ServiceInstance $licMgr = Get-View $servInst.Content.licenseManager $licAssignMgr = Get-View $licMgr.licenseAssignmentManager $license = New-Object VMware.Vim.LicenseManagerLicenseInfo $license.