PowerCLI Cookbook
上QQ阅读APP看书,第一时间看更新

Creating a virtual datacenter in vCenter

vSphere has several defined objects that are used to create virtual datacenters. For example, an object named Datacenter sits at the root of vSphere and allows the clusters and other host infrastructures to be placed inside the virtual datacenter. Installations might have multiple datacenters; however, most VMware administrators use additional datacenter objects in vSphere to represent a physical site and use the datacenter object as a boundary where the infrastructure exists. In this recipe, you will take a look at the code needed to create your new datacenter object in vCenter.

Getting ready

For this recipe, you will need to open a PowerCLI prompt, you need the DNS name or IP address of your vCenter host, and the password for the default administrator account in vCenter.

How to do it…

In order to create a virtual datacenter, and to create new datacenter object in vCenter, perform the following steps:

  1. The first step is to connect to your vCenter server. You need to use the same cmdlet to connect to vCenter that you use to connect to a single ESXi host: the Connect-VIServer cmdlet:
    Connect-VIServer vcentersrv.domain.local
    
  2. Log in with the default Administrator@vsphere.local account created during the VCSA deployment or vSphere SSO installation. When you log in successfully, a prompt will be displayed that shows the server you are successfully connected to, as shown in the following screenshot:
    How to do it…
  3. To start, let's run the Get-Datacenter cmdlet with no additional parameters to see whether there are any existing objects in vCenter. On a fresh vCenter install, there is no output:
    Get-Datacenter
    
  4. To configure a new datacenter, the cmdlet is New-Datacenter, which is very straightforward. Logically, you just need to provide a name for your datacenter, and it will create a datacenter on the vCenter:
    New-Datacenter -Name "Primary"
    
  5. If you run the preceding cmdlet, you'll receive an error that a mandatory parameter and location is missing. However, if this is a brand new vCenter installation, which location would you possibly pass into this cmdlet?
  6. To answer this question, you will run the Get-Folder cmdlet to see whether there are any folder locations that you might be able to use. You will add a -NoRecursion parameter because you only want to return the top-level results:
    Get-Folder -NoRecursion
    
    Name Type
    ---- ----
    Datacenters Datacenter
    
  7. Interestingly, there is a root folder called Datacenters that exists by default. That's a location you can pass into this cmdlet.
  8. So, the next step is to put the two cmdlets together and create our datacenter. You will repeat the New-Datacenter cmdlet and you will specify -Location this time with the cmdlet in Step 4 returning the Datacenters folder:
    New-Datacenter -Name "Primary" -Location (Get-Folder -NoRecursion)
    
    Name
    ----
    Primary
    
  9. The resultant output confirms that a new datacenter called Primary has been created. If you rerun the Get-Datacenter cmdlet, now, it has the same output.

How it works…

The cmdlet that actually creates a new datacenter is very straightforward. The only point of confusion is a required -Location parameter. This requirement is confusing because there are no objects in vCenter on a fresh install. However, as you explore the Get-Folder cmdlet, you will find that a default Datacenters folder is created during the installation of vCenter and it is meant to hold new datacenter objects. The following diagram depicts the hierarchy:

How it works…

At the top level is the root folder of vCenter. Inside the root folder is the Datacenters folder. Before a datacenter is created, root is the only folder that exists in vCenter. The new Primary datacenter that you created is located inside the Datacenters folder. Inside Primary, four additional folders are automatically created that correspond to the four views that you see in the vSphere Client. Each of these are special folders used by vCenter services to house the inventory items.

By passing location in the Datacenters folder using Get-Folder -NoRecursion, you know that you are passing location in the root folder where our datacenter named Primary should be created. If you rerun Get-Folder | Select * after creating the datacenter, you will see the additional objects in the preceding figure, and you will see that their Parent parameter is defined as Primary.