VMware Cloud Community
jpLeo
Contributor
Contributor
Jump to solution

PowerCLI: New-VM -Location [subfolder of subfolder]

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.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Have a look at my Folder by Path post.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Have a look at my Folder by Path post.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jpLeo
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jpLeo
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jpLeo
Contributor
Contributor
Jump to solution

Gah!

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

Thank you for all of your help.

Reply
0 Kudos
JoshTheIT
Contributor
Contributor
Jump to solution

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!!

Reply
0 Kudos
SSIContract01
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
MDGUO
Contributor
Contributor
Jump to solution

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