Skip to content

Creating a vCloud Director Organization with PowerCLI

June 3, 2014

As we started ramping up our vCloud Director environments, I needed a way to automate creation of Organizations as well as Organization Virtual Data Centers. VMware has some documentation available here which is a good start, but didn’t provide everything that I needed.

After some trial and error and finding a few bits and pieces from other people, I’ve put together a couple of scripts that can do most of what I needed. They could be combined into a single script if desired. Not everything is available in the native vCloud cmdlets, but luckily the API is accessible through PowerCLI.

To give credit where credit is due, below are a few blogs and VMTN community posts from where i pulled some code snippets to help put it all together:

VMTN post from barnette08
Dave’s Notepad
vSential
Another from vSential

The parameters for certain items should be adjusted as needed in oder to fit your particular organization’s needs.

Below is the code to create an Organization:

# Uncomment the next three lines if you are not currently connected to a vCloud Director cell
#add-pssnapin VMware.VimAutomation.Cloud
#$vCloud = Read-Host "Enter a vCloud Director Server"
#Connect-CIServer $vCloud
# Prompt for the Organization name, full name, and description then create the Org
$OrgName = Read-Host "Enter the Organization name (to be used for creating the URL)"
$OrgFullName = Read-Host "Enter the Organization Full Name"
$OrgDescription = Read-Host "Enter the Organization Description"
New-Org -Name $OrgName -Fullname $OrgFullName -Description $OrgDescription 

$org = Get-Org -Name $OrgName

# Configure Org to use the system LDAP
$org.ExtensionData.Settings.OrgLdapSettings.OrgLdapMode = "SYSTEM"
$org.ExtensionData.UpdateServerData()
$org.ExtensionData.UpdateViewData()

# Allow sharing of catalogs to other organizations
$org.ExtensionData.Settings.OrgGeneralSettings.CanPublishCatalogs = $True
$org.ExtensionData.UpdateServerData()
$org.ExtensionData.UpdateViewData()

# Set vApp lease times
$leases = $org.ExtensionData.Settings.GetVAppLeaseSettings()
$leases.DeploymentLeaseSeconds = 7776000
$leases.StorageLeaseSeconds = 10368000
$leases.DeleteOnStorageLeaseExpiration = $False
$leases.UpdateServerData()

# Set vApp template lease times
$templateleases = $org.ExtensionData.Settings.GetVAppTemplateLeaseSettings()
$templateleases.StorageLeaseSeconds = 31536000
$templateleases.DeleteOnStorageLeaseExpiration = $False
$templateleases.UpdateServerData()

# Set Org operations limits
$limits = $org.ExtensionData.Settings.GetOperationLimitsSettings()
$limits.OperationsPerUser = "3"
$limits.OperationsPerOrg = "6"
$limits.UpdateServerData()

And the code to create the Organization Virtual Datacenter:

# Uncomment the next three lines if you are not currently connected to a vCloud Director cell
#add-pssnapin VMware.VimAutomation.Cloud
#$vCloud = Read-Host "Enter a vCloud Director Server"
#Connect-CIServer $vCloud

# Prompt for details need to create the Org VDC, then create it.  The storage will be allocated using the *(Any) storage profile as there doesn't seem to be a way around this while using PowerCLI.  We will remove that storage profile later in the script.
$orgName = Read-Host "Enter the Organization name to which resources will be allocated"
$orgvDCName = "$orgName-VDC"
$providerVDC = Read-Host "Enter the name of the Provider VDC from which resources will be allocated"
$CPU = Read-Host "Enter the CPU allocation in GHz"
$MEM = Read-Host "Enter the memory allocation in GB"
$storage = Read-Host "Enter the Storage Allocation in MB"
$storageProfile = Read-Host "Enter the Storage Policy to be used by this Org VDC"
$FastProvision = Read-Host "Should fast provisioning be enabled for this org VDC?  Answer either Y or N"
$NetworkPool = Read-Host "Enter a Network Pool to be used by this Org VDC"
$ExternalNetwork = Read-Host "Enter the name of an external network to be used by this Org VDC"

New-OrgVdc -Name $orgvDCName -AllocationModelAllocationPool -CPUAllocationGHz $CPU -MemoryAllocationGB $MEM -Org $orgName -ProviderVDC $ProviderVDC -StorageAllocationGB 100

# Add a Storage Profile to the newly created Org VDC
# Find the desired Storage Profile in the Provider vDC to be added to the Org vDC
$orgPvDCProfile = search-cloud -QueryType ProviderVdcStorageProfile -Name $storageProfile | Get-CIView

# Create a new object of type VdcStorageProfileParams and configure the parameters for the Storage Profile
$spParams = new-object VMware.VimAutomation.Cloud.Views.VdcStorageProfileParams
$spParams.Limit = $storage
$spParams.Units = "MB"
$spParams.ProviderVdcStorageProfile = $orgPvDCProfile.href
$spParams.Enabled = $true
$spParams.Default = $false 

# Create an UpdateVdcStorageProfiles object and put the new parameters into the AddStorageProfile element
$UpdateParams = new-object VMware.VimAutomation.Cloud.Views.UpdateVdcStorageProfiles
$UpdateParams.AddStorageProfile = $spParams 

# Get the Org VDC and create the Storage Profile
$orgVdc = Get-OrgVdc -Name $orgvDCName
$orgVdc.ExtensionData.CreateVdcStorageProfile($UpdateParams)

#Set the new storage profile as default
$orgvDCStorageProfile = search-cloud -querytype AdminOrgVdcStorageProfile | where {($_.Name -match $storageProfile) -and ($_.VdcName -eq $orgvDCName)} | Get-CIView
$orgvDCStorageProfile.Default = $True
$orgvDCStorageProfile.UpdateServerData()

# Delete the *(Any) Storage Profile
# Get object representing the * (Any) Profile in the Org vDC
$orgvDCAnyProfile = search-cloud -querytype AdminOrgVdcStorageProfile | where {($_.Name -match '\*') -and ($_.VdcName -eq $orgvDCName)} | Get-CIView

# Disable the "* (any)" Profile
$orgvDCAnyProfile.Enabled = $False
$orgvDCAnyProfile.UpdateServerData()

# Remove the "* (any)" profile form the Org vDC completely
$ProfileUpdateParams = new-object VMware.VimAutomation.Cloud.Views.UpdateVdcStorageProfiles
$ProfileUpdateParams.RemoveStorageProfile = $orgvDCAnyProfile.href
$orgvDC.extensiondata.CreatevDCStorageProfile($ProfileUpdateParams)

#Set Org VDC CPU and Memory guarantees
Get-OrgVdc $orgvDCName | Set-OrgVdc -CpuGuaranteedPercent 20
Get-OrgVdc $orgvDCName | Set-OrgVdc -MemoryGuaranteedPercent 50

#Enable Thin Provisioning
Get-OrgVdc $orgvDCName | Set-OrgVdc -ThinProvisioned $True

#Enable Fast Provisioning if the input answer is "Y"
if ($FastProvision -eq "Y")
{
   Get-OrgVdc $orgvDCName | Set-OrgVdc -UseFastProvisioning $True
}

#Create a private Catalog and share it to all Org members with Read/Write access
$privateCatalog = New-Object VMware.VimAutomation.Cloud.Views.AdminCatalog
$privateCatalog.name = "$orgName Private Catalog"
(Get-Org $orgName | Get-CIView).CreateCatalog($privateCatalog)
New-CIAccessControlRule -Entity $privateCatalog.name -EveryoneInOrg -AccessLevel ReadWrite -Confirm:$False

#Assign Network Pool and number of available networks
Get-OrgVdc $orgvDCName | Set-OrgVdc -NetworkPool $NetworkPool
Get-OrgVdc $orgvDCName | Set-OrgVdc -NetworkMaxCount 25

#Create a direct connect network
$orgVdcView = Get-OrgVdc $orgvDCName | Get-CIView
$extNetwork = $_.externalnetwork
$extNetwork = Get-ExternalNetwork | Get-CIView | ?{$_.name -like "$ExternalNetwork"}
$orgNetwork = new-object vmware.vimautomation.cloud.views.orgvdcnetwork
$orgNetwork.name = "External-Lab"
$orgNetwork.Configuration = New-Object VMware.VimAutomation.Cloud.Views.NetworkConfiguration
$orgNetwork.Configuration.FenceMode = 'bridged'
$orgNetwork.configuration.ParentNetwork = New-Object vmware.vimautomation.cloud.views.reference
$orgNetwork.configuration.ParentNetwork.href = $extNetwork.href

$result = $orgVdcView.CreateNetwork($orgNetwork)

$orgVdcView

 

Advertisements

From → vCloud Director

3 Comments
  1. tjurgens permalink

    It looks like from your script you make use of the * (Any) storage profile to create your OrgVDC, then change that profile after the fact. Have you been able to create an OrgVDC without the use of the * (Any) profile? If so, I would be interested to see your code, as we do not have that * (Any) storage profile to use as an intermediary step.

    • No, I couldn’t get this working without using the *(Any) storage profile unfortunately. So I had to have it there for the script to work, even though it is not in use.

Trackbacks & Pingbacks

  1. Provisionner un VDC sur vCloud Director via PowerCLI | vBlog.io

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: