5. vCloud vApp Provisioning : 5.5 Workflow Overview : 5.5.3 Loop Through Each VM in vApp
   
5.5.3 Loop Through Each VM in vApp
This section processes the loop through each virtual machine. It checks to see if there are additional virtual machines to process. If there are, it sets the current virtual machine, performs custom naming operations and passes on to the Active Directory Handler.
5.5.3.1. Apply Custom Naming Convention to Each VM in the vApp
At the start of the workflow, especially when being called from the API, the number of child virtual machines is unknown. This makes it quite challenging to provide user-supplied names or other per-VM properties to the virtual machines prior to deployment. A programmatic method of generating virtual machine names can be developed that matches client requirements. For demonstration purposes, the custom naming convention applied in the example workflow is as follows:
<OS Initial><4 digit number><First initial of organization name><First initial of virtual datacenter name>
In this naming convention, <OS Initial> is either W for Windows or L for non-Windows. The four-digit number begins at 0001 and is incremented if the final virtual machine name is discovered to already exist in the target organization. The final two parts of the name are the upper-case first initial of the organization and virtual datacenter respectively. For relevant scripting, see the VM Customization scriptable task of the sample workflow.
5.5.3.2. Base VM Name on vApp Name
In many cases where a single virtual machine is all that each vApp contains, it is desirable to give the virtual machine the same name as the vApp. This allows for user-specified names upon request, an option not easily available for multi-virtual machine vApps. The example workflow auto-adds a numeric suffix to the virtual machine name if the name is found to already exist in the organization. For relevant scripting, see the VM Customization scriptable task of the sample workflow.)
5.5.3.3. Customize the Guest OS Customization Script
vCloud Director lets you specify a guest OS customization script on each virtual machine. The custom script runs on the virtual machine upon deployment, after the general Sysprep and hardware customization have taken place. This is similar to the RunOnce commands of a Windows Sysprep operation. Using vCenter Orchestrator, you can establish variables in this customization script to be substituted at workflow run time. An example of this is demonstrated in section 5.5.3.4, Add Specified Domain\User to Local Administrators Group. For relevant scripting, see the VM Customization scriptable task of the sample workflow.
5.5.3.4. Add Specified Domain\User to Local Administrators Group
You can use the guest OS customization script to add a domain\user account to the Local Administrators group of Windows virtual machines that are joined to a domain.
To add a domain\user account to the Local Administrators group of Windows virtual machines that are joined to a domain
1. Configure the vCloud Director organizations to Enable domain join for virtual machines in this organization, specifying appropriate domain and credentials.
2. Specify the domain in full format, such as vmware.com, rather than the NetBIOS form of vmware. This setting is located under the Organization > Administration > Settings > Guest Personalization page of the vCloud Director UI.
3. Prepare the vApp template with one or more Windows virtual machines that have had the Guest OS Customization tab of their properties properly populated. Use the following settings:
*Enable guest customization – Selected.
*Change SID – Selected.
*Password Reset – As desired.
*Join Domain – Populated as desired using either the domain configured for the organization or specified domain and credentials.
*An appropriately formatted customization script. The following is a sample used in a custom workflow:
if '%1%' == 'postcustomization' (net localgroup administrators
@domain@\\@username@ /add)
The variables @domain@ and @username@ are replaced at workflow execution time. For scripting details, see the VM Customization scriptable task of the sample workflow.