VMware Cloud Community
khaliqamar
Enthusiast
Enthusiast

vm creation by script

I hope someone can help me to create a script to deploy 100 VMs or share if anyone got a similar one.

- virtualcenter is 4.1

- i have to deploy VMs from a template and script should keep 20% free space in datastores..

- script should use a template.

I am very new powercli.

thanks in advance!

6 Replies
LucD
Leadership
Leadership

The basics of such a script are quite simple.

For example:

$templateName = "MyTemplate"
$nrOfVM = 100
$tgtCluster = "MyCluster"

$resourcePool = Get-Cluster -Name $tgtCluster | Get-ResourcePool -Name Resources

1..$nrOfVM | %{
 
New-VM -Name "VM-$_" -Template $templateName -ResourcePool $resourcePool
}

But you will need to provide some more info on some of the requirements you provided.

Do you create the new VMs in a cluster, a specific resourcepool.... ?

Do you know the size of the template, or should the script determine that first ?

How do you select the target datastores ? Is that a restricted list, or can any of the available datastores be used ?


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

khaliqamar
Enthusiast
Enthusiast

Thanks lucD,

i have a seprate cluster and no resourcepool. I have to deploy new VMs from template.

the size of the template is 30 gb.

any datastore we can use which is attached with the cluster. 

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this

$templateName = "MyTemplate"
$templateSizeGB = 30
$nrOfVM = 100
$tgtCluster = "MyCluster"

$cluster = Get-Cluster -Name $tgtCluster
$resourcePool =  Get-ResourcePool -Name Resources -Location $cluster

1..$nrOfVM | %{
 
$selectedDS = Get-Datastore -RelatedObject $cluster |
   
where {($_.FreeSpaceGB - $templateSizeGB) / $_.CapacityGb -gt 0.2} |
   
Get-Random
 
if($selectedDS){
   
New-VM -Name "VM-$_" -Datastore $selectedDS -Template $templateName -ResourcePool $resourcePool
  }
 
Else{
   
Write-Error "No datastore found with sufficient free space"
  }
}

The Resouurces resourcepool is the hidden resourcepool that is present in every cluster.

The VMs will be created in the root of the cluster.

If the script can't find any datastore with 20% free space, no VM will be created.


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

khaliqamar
Enthusiast
Enthusiast

Hi,

i have changed these values with the actual values  i.e i changed the value of $templateName = <original template name>

i changed number of VMs to 5.

$templateName = "MyTemplate"
$templateSizeGB = 30
$nrOfVM = 100
$tgtCluster = "MyCluster"

I am getting these errors

error script.JPG

Reply
0 Kudos
LucD
Leadership
Leadership

Which PowerCLI version are you running ? I suspect you are running a somewhat older version.

Do a Get-PowerCLIVersion.


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

Reply
0 Kudos
khaliqamar
Enthusiast
Enthusiast

powercliversion.JPG

My virtual center is 4.1. and powercli version is 5.1

I even run this script on virtualcenter 5.1 and powercli version 5.1 but having a same output.

any suggestion...

Reply
0 Kudos