Working with PowerCLI and VMs Folders

Hi! This is my first post in English, we decided to make some post in English. In this post, I will talk about VM Folders and about what use we can give to them. I will also include some commands to work with them using PowerCLI.

To start, Do we know what is a vCenter folder? A folder is a container used to improve object grouping within your inventory. Folders provide a natural structure on which to apply permissions.

An Example, could be, putting together virtual machines or templates according to the OS, the application or purpose. Another example, could be arranging Datacenters by geographic location.

Which type of folders can we create?

folders

There are the following type of folders:

  • Host and Cluster Folder
  • Network Folder
  • Storage Folder
  • VM and Template Folder

We must remember that objects within a folder must be the same type. For example, we cannot place Host and virtual machines together in the same folder.

Depending on the type of folder, we can view it from one inventory view or another. We must remember that there are the following type of inventory views (Host and Clusters, Virtual Machines and Templates, Networks and Datastores and Datastores Clusters).

A month ago, I found the need to group machines according to the environment or application, in order to organize them for a movement of machines in a group across Datacenters with Extended Storage.

The idea of organizing the virtual machines by folders came up, in that way I would be able to write a script, that will collect the name of the virtual machines inside a folder and it would be able to perform actions with them. For example, if we need to power off the VMs by environment, change the machines within a DRS Group in order to perform vMotion to the opposite Datacenter, etc.

To be able to work with VMs and Templates folders, we have very useful PowerCLI commands, and I will detail them below.

  • Get data from a Folder VM

As I mentioned earlier, we may have the need to know which objects a folder contains. For example, we can create a VMs folder structure, in order to stablish an orchestrated virtual machines shutdown.


### Function that obtains the list of VMs that contains a VM Folder

Function Get-VMs([string]$folder)

{

$List_VMs = Get-Folder –Name $folder | Get-VM

return $List_VMs

}

### Function that obtains the list of Folders VM that contains a Datacenter

Function Get-Folders([string]$datacenter)

{

$List_Folders = Get-Folder –Location $datacenter –Type “VM”

return $List_Folders

}

  • Create a Folder VM

Creating VM folders is not easy. There is a cmdlet  for that, “Create-Folder”, but you cannot specify which folder type to create, and by default creates Host Folders. Therefore to create a VM folder you have to bind several cmdlets.


### Command to create a Folder VM

$folder = “MyFolderVMS”

$Datacenter = “MyDatacenter”

Write-Host "Create Folder VM with name $folder at Datacenter with name $Datacenter ..." 

(Get-View (Get-View -viewtype datacenter –filter @{"name"=$Datacenter}).vmfolder).CreateFolder($folder)

  • Move Folder to other destination

We can also find the situation of having to move VM folders to other locations.


### Command to move Folders to other location.

$Folder = “FolderVMtoMove”

$ObjectDestination = “NameObjDestination”

Move-Folder -Folder $Folder -Destination $ObjectDestination

**Specify the datacenter or folder where you want to move the folders

  • Remove Folder

Another need we may have is to remove folders.


### Command to remove Folder.

$Folder = “FolderVMtoRemove”

Remove-Folder -Folder $Folder 

** If you need to autoconfirm the deletion of folders. For example for a mass deletion, add "-confirm: $ false" 

Here you have a link that helped me to achieve my needs:

https://www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/Move-Folder.html

I hope you find it useful. Thank you very much for sharing. 😉

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInBuffer this pageEmail this to someonePrint this page

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *