RobMokkink
Expert
Expert

VirtualMachineRelocateSpec + CloneVM_Task

Jump to solution

The script is working so far.

The next challenge is that i have 2 cluster, each in a different site. Each site is active. each cluster has the same resourcepools and the resourcepools have the same names + the -<sitecode>. So each resourcepool is unique.

The script clones a template and put's the vm in a resourcepool on a cluster. But if i don't want to put a vm in a resourcepool, i looked at the SDK concerning VirtualMachineRelocateSpec.

In the VirtualMachineRelocateSpec there is a line that when you clone a template to vm, you must specify a resourcepool, but how to do that if i want to use the cluster default resourcepool?

The code for getting the resourcepool is:

#RESOURCE POOL FROM CLUSTER

$pool = get-resourcepool $vmpool

$poolview = get-view $pool.id

$vmrSpec.pool = $poolview.moref

But how to use the default resourcepool of a cluster?

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

No, like ykalchev said, the property ResourcePool in the ClusterComputeResource object, which is an extension of the ComputeResource object, points to the "...root resource pool".

The line is in fact

(Get-View (Get-Cluster <cluster-name>).ID).ResourcePool

- we get the cluster <cluster-name> which returns a ClusterImpl object

- with Get-View we get to the ClusterComputeResource object

- and in that object we get the property ResourcePool, which is a MoRef


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

View solution in original post

0 Kudos
7 Replies
RobMokkink
Expert
Expert

I was thinking.

Every host and cluster has a resource pool, i think the pool is called resources.

If i do this interactively:

get-cluster ClusterName | get-resourcepool

I see the resourcepools in the cluster, but also the default one.

But how to use it in the script?

I tried it using

get-cluster ClusterName | get-resourcepool $vmpool

$poolview = get-view $pool.id

$vmrSpec.pool = $poolview.moref

But then vmrSpec.pool is null and the script fails.

Anyone an idea?

0 Kudos
ykalchev
VMware Employee
VMware Employee

According to VI API documentation the ClusterComputeResource object has resourcePool property that is reference to root resource pool.

So In you case you can do

$cl = Get-View (get-cluster ClusterName).Id

$vmrSpec.pool = $cl.ResourcePool

Is this is the resource pool you are looking for ?

Yasen Kalchev, vSM Dev Team
LucD
Leadership
Leadership

Yes, that property points to the default pool called Resources.

Just one caveat, the property is a MoRef so you will have to cast it to a ResourcePool object.

Short example with the MoveIntoResourcePool method


$respool = [http://VMware.Vim.ResourcePool|http://VMware.Vim.ResourcePool](Get-View (Get-Cluster <cluster-name>).ID).ResourcePool
$vm = Get-View (Get-VM <guest-name>).ID
 
$respool.MoveIntoResourcePool($vm.MoRef)
 

Since the forum SW doesn't like square brackets I attached the code


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

RobMokkink
Expert
Expert

ykalchev luc,

Thanks for giving me some input.

@luc

I have a question about this line:

(Get-Cluster &lt;cluster-name&gt;).ID).ResourcePool

Doesn't this line get all the resourcepools in the cluster?

I have a long weekend i won't be able to update the script until monday.

0 Kudos
LucD
Leadership
Leadership

No, like ykalchev said, the property ResourcePool in the ClusterComputeResource object, which is an extension of the ComputeResource object, points to the "...root resource pool".

The line is in fact

(Get-View (Get-Cluster <cluster-name>).ID).ResourcePool

- we get the cluster <cluster-name> which returns a ClusterImpl object

- with Get-View we get to the ClusterComputeResource object

- and in that object we get the property ResourcePool, which is a MoRef


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

View solution in original post

0 Kudos
RobMokkink
Expert
Expert

oke thanks for explaining that.

0 Kudos
RobMokkink
Expert
Expert

The script now completely works oke:

$cluster = Get-View (get-cluster ClusterName).Id

$vmrSpec.pool = $cl.ResourcePool

Put's the vm in the default resourcepool of the cluster.

Next thing to do is, get the default resourcepool of a standalone esx server to make the script even more flexibel.

0 Kudos