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.
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
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?
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 ?
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
ykalchev luc,
Thanks for giving me some input.
@luc
I have a question about this line:
(Get-Cluster <cluster-name>).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.
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
oke thanks for explaining that.
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.