VMware Cloud Community
sambemyfriend
Contributor
Contributor

create new vm in drs cluster without selecting specific host

Hello VMware Gurus,

I want to create a vm using script, but I don't want to specify the host name, I want to place that vm on a host which has least resource utilization in terms of memory and cpu in a cluster.

is this possible using powercli? and if yes can you please share the script with me?

Thanks in advance   

0 Kudos
11 Replies
LucD
Leadership
Leadership

You want to actually calculate that, or let DRS decide ?


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

0 Kudos
sambemyfriend
Contributor
Contributor

hi LucD,

thanks for the response.

is it possible to calculate while creating the vm using script?

and for let DRS decide, while creating vm using script is it necessary to specify hostname or just clustername will work and the put the vm on the correct host?

waiting for your reply

Thanks

0 Kudos
LucD
Leadership
Leadership

Yes, you can calculate/retrieve the CPU and memory usage on the ESXi nodes in a cluster, before you execute the New-VM cmdlet.

Something like the following for example, it will return the name of the ESXi node with the lowest load (at this moment).

Note that the weighing factors for CPU and memory are both set to 1. Which means CPU & memory are considered equally important in the decision process.

But there are many other selection criteria possible.

$clusterName = 'MyCluster'

$cpuWeight = 1

$memWeight = 1

$esx = Get-Cluster -Name $clusterName | Get-VMHost

Get-Stat -Entity $esx -Realtime -MaxSamples 1 -Stat cpu.usage.average,mem.usage.average -Instance "" |

Group-Object -Property {$_.Entity.Name} | %{

    $cpu = ($_.Group | where{$_.MetricId -eq 'cpu.usage.average'} | Select -ExpandProperty Value) * $cpuWeight

    $mem = ($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | Select -ExpandProperty Value) * $memWeight

    New-Object PSObject -Property @{

        Name = $_.Name

        Value = $cpu + $mem

    }

} | Sort-Object -Property Value | Select -First 1 -ExpandProperty Name

The New-VM cmdlet will not accept a clsuter on the VMHost cmdlet I'm afraid.


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

0 Kudos
sambemyfriend
Contributor
Contributor

Hi LucD,

Thanks for the response, as you mentioned that "The New-VM cmdlet will not accept a clsuter on the VMHost cmdlet I'm afraid."

is there anyway that we can achieve this?

Thanks in advance.

0 Kudos
LucD
Leadership
Leadership

No, not on the New-VM cmdlet.

The previous script I gave will give you the name of the VMHost in the cluster with the least CPU & Memery usage, as you requested


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

0 Kudos
sambemyfriend
Contributor
Contributor

Hi LucD,

I tried to execute the script, but it throws an error:

Get-Stat : 12/21/2015 4:01:59 PM    Get-Stat        Stat with instance '' was n

ot found using the specified filter(s).

At C:\clust.ps1:6 char:9

+ Get-Stat <<<<  -Entity $esx -Realtime -MaxSamples 1 -Stat mem.usage.average,c

pu.usage.average -Instance "" |

    + CategoryInfo          : ObjectNotFound: (:) [Get-Stat], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA

   utomation.ViCore.Cmdlets.Commands.GetViStats

and also every time I execute, it gets the same host which has the highest memory utilization

waiting for reply

thanks

0 Kudos
LucD
Leadership
Leadership

Which PowerCLI version are you running ?

Do a Get-PowerCLIVersion from the PS prompt.


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

0 Kudos
sambemyfriend
Contributor
Contributor

Hi LucD,

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.5 Release 1 build 1295336

---------------

Snapin Versions

---------------

   VMWare AutoDeploy PowerCLI Component 5.5 build 1262826

   VMWare ImageBuilder PowerCLI Component 5.5 build 1262826

   VMware vCloud Director PowerCLI Component 5.5 build 1295337

   VMware License PowerCLI Component 5.5 build 1265954

   VMware VDS PowerCLI Component 5.5 build 1295334

   VMware vSphere PowerCLI Component 5.5 build 1295334

waiting for your reply.

Thanks

0 Kudos
LucD
Leadership
Leadership

Can you upgrade to the latest release ?


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

0 Kudos
sambemyfriend
Contributor
Contributor

Hi LucD,

I upgraded PowerCLI version to 6.0 R3, I am not getting error which I used to get in earlier version but the result is still same. it presents the host with highest Memory and CPU utilization.

am I missing something?

Thanks

0 Kudos
LucD
Leadership
Leadership

I updated the script above, I removed the Descending switch on the Sort-Object cmdlet.

Now you should be getting the name of the ESXi node with the lowest usage.


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

0 Kudos