VMware Cloud Community
brysonjk
Contributor
Contributor
Jump to solution

Clone a template to a specific folder on datastore, with powercli???

I am deploying about 135 VM's from a single template using powercli from a CSV file. 

Each line in the CSV has a login, course, section, instructor 

Example...

Login          Course     Section     Instructor

123456       EE123      5555          Foo

Here is the script

Connect-VIServer -Server 10.0.0.60 -Credential (Get-Credential)
$Users = Import-Csv c:\CSV\Data.csv
foreach ($User in $Users) {
     $Login = $User.Login
     $Course = $User.Course
     $Section = $User.Section
     $Instructor = $User.Instructor
     $ResPool = $User.Section + " " + $User.Instructor
     New-VM -VMHost 10.0.0.10 -Name $Login -ResourcePool "$ResPool" -Template "VM-template" -Datastore "datastore1"
     write "$Login Created" | Out-File "log.txt" -append
}
Disconnect-VIServer -Server 10.0.0.60 -confirm:$false

I am trying to make the datastore a little cleaner when I deploy these machines, and put all the machines in a folder located in the datastore eg. datastore1\spring2013\EE123\  (would actually like to have $Course in place of EE123)

Can someone help me out with this.


Thanks in advance

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes, you can use the ResourcePool parameter on the New-VM cmdlet for that.

But since you have resourcepools with the same name in the tree structure, you will have to make sure you select the correct one.

You use the Location parameter to indicate which branch in the tree you want.

For example:

$rpRoot = Get-ResourcePoo -Name "Sprint 2013"

$rpCourse = Get-ResourcePoo -Name "5555 Foo" -Location $rpRoot

$rpServers = Get-ResourcePoo -Name "Servers" -Location $rpCourse

$rpStudents = Get-ResourcePoo -Name "Students" -Location $rpCourse

New-VM -Name .... -ResourcePool $rpStudents

You can build this logic into a loop and use the names from a CSV file for example


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik there is no cmdlet available in the current release that allows you to do such a thing.

In fact the VirtualMachineRelocateSpec object, which controls the CloneVM_Task method, only foresees a datastore for the new VM.

That is the SDK API that is used internally by the New-VM cmdlet for cloning a VM from a template.

The only way I can think of to do this would be something like this

  • create/clone the new VM
  • unregister the new VM
  • set up a datastore provider to the datastore where the new VM is located
  • create, in the datastore provider, the new folder structure
  • move all the files that constitute the VM from the original folder to the new folder
  • register the VM with the VMX file from the new folder

But I'm not sure if nesting folders like that on a datastore is a "best practice".

I would go for dedicated folders under the VM and Templates view in the vCenter.

Then you would still have the folder organisation, not on the datastore but in the vCenter.


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

brysonjk
Contributor
Contributor
Jump to solution

That is pretty much what I was thinking, but it seems like too much work to go through to organize the datastore only for my benefit.  I do have another question though.  In my script, my resource pool is something like "5555 Foo".  Would it be possible to add the new machine to a nested resource pool (child resource pool).  Ex.  I have resource pools like this

Sprint 2013

     1111 Smith

          Servers

          Students

    3333 Bar

          Servers

          Students

     5555 Foo

          Servers

          Students

I would be using resource pools only for organization, and not changing any of the setting when it is created.  I am sure this isn't "best practices" but I like organization.  Right now my script will put the new machine at the 5555 Foo.  It would be nice to have it go directly into the Students.


Is this possible?  Does that make sense?


Thanks again

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, you can use the ResourcePool parameter on the New-VM cmdlet for that.

But since you have resourcepools with the same name in the tree structure, you will have to make sure you select the correct one.

You use the Location parameter to indicate which branch in the tree you want.

For example:

$rpRoot = Get-ResourcePoo -Name "Sprint 2013"

$rpCourse = Get-ResourcePoo -Name "5555 Foo" -Location $rpRoot

$rpServers = Get-ResourcePoo -Name "Servers" -Location $rpCourse

$rpStudents = Get-ResourcePoo -Name "Students" -Location $rpCourse

New-VM -Name .... -ResourcePool $rpStudents

You can build this logic into a loop and use the names from a CSV file for example


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

Reply
0 Kudos
brysonjk
Contributor
Contributor
Jump to solution

That is awesome. Thank you very much.

Reply
0 Kudos