PowerCLI

 View Only
  • 1.  PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Oct 08, 2013 11:59 AM

    I am trying to find a way to create a new-vm using powercli, specifying -location as a subfolder of a subfolder.

    This is easy to do if the subfolder of a subfolder is a unique name, however if there are more than one

    i.e. in this rough sketch of a VM's and Templates view through vCenter:

    [DataCenter]VMDataCenter

                             [Folder]VM Stack 1

                                  [Folder]Internal Network

                                                 VM's

                                  [Folder]External Network

                                                 VM's

                             [Folder]VM Stack 2

                                 [Folder]Internal Network

                                                 VM's

                                 [Folder]External Network

                                                 VM's

    ...there is no direct way to do this that I have found.  How can I specify that I want to create a new VM in the folder "VM Stack 2" under "External Network" ?  Any help would be greatly appreciated.



  • 2.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]
    Best Answer

    Posted Oct 08, 2013 12:25 PM

    Have a look at my Folder by Path post.



  • 3.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Oct 08, 2013 12:45 PM

    Thank you for the quick response.  I was hoping there was a direct way to specify the path.



  • 4.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Oct 08, 2013 01:15 PM

    I'm afraid there is no option in the current PowerCLI build to use folder paths.

    But you can place my function in one of your profile files, and then you can always use it, just like a cmdlet



  • 5.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Oct 08, 2013 03:03 PM

    If my Datacenter name is Dat

    My first subfolder name is Folder1

    My second subfolder name is Folder2

    My third subfolder name is Folder3

    The -path is "Dat/Folder1/Folder2/Folder3"

    When I execute the code:

    New-VM -Name $VM1 `

         -Location (Get-FolderByPath -Path "Dat/Folder1Folder2/Folder3")

    The VM is created in the correct folder, however I get errors that:

    Folder with name 'Datacenters' was not found...

    Inventory with name 'Dat' was not found...

    Inventory with name 'Folder1' was not found...

    Inventory with name 'Folder2' was not found...

    Inventory with name 'Folder3' was not found...

    What am I missing?



  • 6.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Oct 08, 2013 03:29 PM

    Could it be that you have more than 1 connection to a vSphere server open ?

    Do a

    $global:defaultviservers

    Does this return more than 1 object ?



  • 7.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Oct 08, 2013 03:37 PM

    Gah!

    I had an open session with a test server from earlier today.  Done.

    Thank you for all of your help.



  • 8.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Mar 23, 2018 08:30 PM

    This is what I have used with the following folder structures as an example:

    -Datacenter

         -Production

              -Servers

         -Management

              -Servers

    New-VM -Template "TemplateName" -name "VMName" -Location (Get-Folder servers | where {$_.Parent -like "Production"}) -VMHost "VMHost" -RunAsync

    If you have two similar folder structures with several levels of identical folders you can use run the "Get-Folder | FL" and find the folder you need to identify. You should get an output similar to this:

    ParentId                                       : Folder-group-v000

    Parent                                          : ParentFolder

    IsChildTypeVm                            : True

    IsChildTypeComputeResource    : False

    IsChildTypeDatacenter                : False

    IsChildTypeDatastore                  : False

    Type                                             : VM

    Name                                            : ChildFolder

    CustomFields                               : {}

    ExtensionData                              : VMware.Vim.Folder

    Id                                                  : Folder-group-v001

    Uid                                                : /VIServer=Domain\DomainUser@FQDN:443/Folder=Folder-group-v001/

    Client                                            : VMware.VimAutomation.ViCore.Impl.V1.VimClient

    By utilizing the information in the ID field, you can call out that specific folder with the following command:

    New-VM -Template "TemplateName" -name "VMName" -Location (Get-Folder | where {$_.ID -like "Folder-Group-v001"}) -VMHost "VMHost" -RunAsync

    Hopefully this helps someone!!



  • 9.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Sep 17, 2019 08:58 AM

    we used the LucD script and it worked fine!

    we have at least 5 child folders with same name in different parent dirs

    $folder = 'PARENT\CHILD'

    $vmpath = get-folderbypath -folder $folder -separator '\'

    New-vm ... -Location $vmpath ....

    hope this will be helpful too



  • 10.  RE: PowerCLI: New-VM -Location [subfolder of subfolder]

    Posted Dec 22, 2020 05:15 PM

    If you have an existing VM in the location you'd like:

    $existingVM = get-vm VMNAME

    Then:

    New-VM -Template "TemplateName" -name "VMName" -Location $existingVM.Folder -VMHost "VMHost" -RunAsync