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. 😉

6 comments

  1. Hello,

    When I’m running your command to create a vm folder from the command line directly, it works.
    But from a ps1 script it returns:
    PS C:\Users\Administrateur.CLOUDIS> ./createFolder.ps1
    Au caract?re C:\Users\Administrateur.CLOUDIS\createFolder.ps1:4 : 59
    + … nter ???filter @{“name”=$Datacenter}).vmfolder).CreateFolder($folder)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Le terminateur ” est manquant dans la cha?ne.
    Au caract?re C:\Users\Administrateur.CLOUDIS\createFolder.ps1:4 : 106
    + … nter ???filter @{“name”=$Datacenter}).vmfolder).CreateFolder($folder)
    + ~
    Parenth?se fermante ??)?? manquante dans l?expression.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

    And I don’t know why…

    Any idea ? Thanks 🙂

    1. I believe that the problem in your script is the Next:

      Line un your script
      @{«name»=$Datacenter}).vmfolder).CreateFolder($folder)

      The issue are the chars <>

      You must try with this

      @{“name”=$Datacenter}).vmfolder).CreateFolder($folder)

      Please let me know

      1. Hello and thanks for your answer. I think the copy / paste here created this <> but actually it’s “”. The two line of code face the same problem on your message and are exactly the same 😀
        So I suppose while I’m writing this answer, the problem will be present here as well when I’ll throw my today’s message.

        Anyway, let’s use words. I’m using already double quotation mark but it have been translated here to angle quotes. Using double quotation generate the same error. So the problem should be in another location.

        I’m trying to dig more into this problem today.

        Thanks!

        1. I finally found it was a copy / paste problem inside my text editor that cause the problem. Writing manually all the create folder line manually resolved the problem.

          Thanks 🙂

        2. Hi Thierry,

          I have executed the commands of my post, and it’s working fine.

          PS C:\> $folder = “MyFolderVMs”
          PS C:\> $Datacenter = “PRUEBA”
          PS C:\> Write-Host “Create Folder VM with name $folder at Datacenter with name $Datacenter …”
          Create Folder VM with name MyFolderVMs at Datacenter with name PRUEBA …
          PS C:\> (Get-View (Get-View -viewtype datacenter -filter @{“name”=$Datacenter}).vmfolder).CreateFolder($folder)

          Type Value
          —- —–
          Folder group-v950

          PS C:\> Get-Folder -Type VM -Name MyFolderVMs

          Name Type
          —- —-
          MyFolderVMs VM

          Also I have created a Script with this lines:

          $folder = “MyFolderVMs-Test1”
          $Datacenter = “PRUEBA”
          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)

          And it’s working fine.

          If you want you can send me your script and I could review it.

  2. ### This is the right syntaxt to create a folder

    ### 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)

Leave a Reply

Your email address will not be published. Required fields are marked *