Setting up folders to organize objects in vCenter

vSphere folders are flexible containers with other vSphere objects inside. Folder objects in vSphere are meant to be a logical organizational structure for objects that are not tied to physical resources. This means that VM objects from different clusters or even different datacenters can be logically grouped together. The same applies to port groups, switches, or datastores.

This is important as you begin to look at delegating access from VMs to operators, developers, and other users in the organization, so that you can group together all of the VMs that a user needs to access. Folders also help administrators to easily locate objects and report on objects for a particular business unit or group within their companies.

In this recipe, you will look at the simple cmdlets used to create folder structures in vSphere, and move objects into these folders with simple PowerCLI cmdlets.

Getting ready

To begin, you need to open a PowerCLI window and connect to a vCenter server. You should also read and review the Creating a virtual datacenter in vCenter recipe earlier in this chapter, since it discusses the hierarchy of folders inside a vSphere datacenter. This recipe uses a lot of the concepts introduced in the earlier recipes.

For this recipe, you will use the New-Folder cmdlet and understand the different types of folders that it can create inside vCenter. Folders are used in multiple areas of vCenter for organizational purposes. You will also take a look at the use of Get-Folder and Remove-Folder.

The New-Folder cmdlet is another cmdlet that relies on the -Location parameter to determine where to create the object you're defining. As you observed in the Creating a virtual datacenter in vCenter recipe, you are able to use the Get-Folder cmdlet to return the four special folders automatically provisioned under a datacenter object.

For this example, you will create several folder structures. You will create two, two-level folders under the VM and Templates view for Infrastructure and App Servers. You will create two subfolders called Domain Controllers and VMware under Infrastructure. You will create a Standard vSwitches folder in the Networks view and you will create an NFS and iSCSI folder under the Datastores view. Finally, you will create a Finance and IT folder under the Host and Clusters view to store clusters owned by these businesses. The following illustrates this structure:

Getting ready

How to do it…

In order to set up folders to organize objects in vCenter, perform the following steps:

  1. The first step is to retrieve the datacenter where you want these folders to be created. By first getting the datacenter object, you ensure that if you had more than one datacenter defined, you would be operating in the correct datacenter. So, the first step is to run Get-Datacenter and find our Primary datacenter:
    Get-Datacenter -Name "Primary"
    

    Note

    In this particular example, since Primary is the only datacenter object, you do not have to pass in the datacenter object to the next cmdlet. By specifying Primary, you ensure that you have selected the desired datacenter if you have multiple objects with the same name in your infrastructure.

  2. The next step is to pipe this datacenter object into a New-Folder cmdlet, which will limit the results to within this datacenter. If you start with the Infrastructure folder, you need to return the root folder with type vm:
    Get-Datacenter -Name "Primary" | Get-Folder -name "vm"
    
  3. Now that you have a single folder, this will serve as our location parameter for our new Infrastructure folder. Using New-Folder, you will pass in our desired name and the location parameter from the previous step:
    New-Folder -Name "Infrastructure" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "vm")
    
  4. Next, you can repeat the same step with our App Servers folder. The only parameter that should change is the name parameter:
    New-Folder -Name "App Servers" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "vm")
    
  5. The next step is to create a subfolder under Infrastructure for Domain Controllers. To do this, you change the name and the location of the same cmdlet. Instead of searching for the folder named vm, you will search for the one you just created named Infrastructure. While we're at it, you can create the VMware folder as well by repeating the cmdlet with a different name defined:
    New-Folder -Name "Domain Controllers" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "Infrastructure")
    
    New-Folder -Name "VMware" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "Infrastructure")
    
  6. Next, you will move to create the Standard vSwitches folder under the Networking area. To do this, you need to run the New-Folder cmdlet and search for the root network folder in the datacenter for the location:
    New-Folder -Name "Standard vSwitches" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "network")
    
  7. With this successfully created, you will write three additional New-Folder cmdlets creating the NFS and iSCSI datastore folders and the Finance and IT host folders. These follow the same format:
    New-Folder -Name "NFS" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "datastore")
    
    New-Folder -Name "iSCSI" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "datastore")
    
    New-Folder -Name "Finance" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "host")
    
    New-Folder -Name "IT" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "host")
    
  8. With all of the folder structures now created, you can take a look at moving objects into these locations. You will use multiple cmdlets that begin with Move- in order to relocate objects into these folders you have created. Let's begin with the VM folder VMware, and relocate our vCenter server into that folder. Since you know that this is the only folder named VMware in vCenter, you will use the shortcut and just use Get-Folder with the name:
    Move-VM -VM "vCenterSrv" -Location (Get-Folder -Name "VMware")
    
  9. The next object you want to relocate is the cluster you created named BigCluster using the Move-Cluster cmdlet. Move-Cluster requires the -Location parameter and also a cluster name with the -Cluster parameter. You can relocate BigCluster into the IT host folder:
    Move-Cluster -Cluster "BigCluster" -Location (Get-Folder -Name "IT")
    
  10. Lastly, you can reorganize our datastores logically using the Move-Datastore cmdlet. This cmdlet uses the parameter -Destination instead of -Location, but accepts the input of a Folder object:
    Move-Datastore -Datastore "NFSDatastore1" -Destination (Get-Folder -Name "NFS")
    
    Move-Datastore -Datastore "iSCSIDatastore1" -Destination (Get-Folder -Name "iSCSI")
    
  11. Unfortunately, there is no native cmdlet to move PortGroups from Standard vSwitches into folders in the Networking view, but it can be done in the vSphere Client GUI.

How it works…

The New-Folder cmdlet automatically determines what type of folder to create based on the location passed to it. There are five types of folders: datacenter, vm, host, network, and datastore. If you pass the vm folder location, a vm folder will be created. The type of folder determines where the folder is visible in the GUI. In PowerCLI, you can also use these types to scope the results returned from the Get-Folder cmdlet by passing the -Type parameter.

If you can see, each of the Move- cmdlets accepts a name parameter that is specific to the type of object it expects: Move-VM uses the -VM parameter, Move-Datastore uses -Datastore, and Move-Cluster uses the -Cluster parameter. This follows a logical pattern that you can expect even without using Get-Help to see instructions.

There's more…

In this recipe, you worked with host, vm, network, and datastore groups within a datacenter. vSphere also allows you to create datacenter folders at the same level as datacenter objects to arrange datacenters logically.

See also

  • The Deploying new virtual machines from a template and Creating basic reports of VM properties using VMware Tools and PowerCLI recipes in Chapter 3, Managing Virtual Machines