A couple of days ago i received a request from a customer asking for a PowerCLI script to create VMs. There were a couple of requirements for the script.
- Specify the number of VMs to be created.
- The VMs must be powered on after the creation.
- The VMs must be placed on all ESXi hosts in the vSphere cluster.
- Specify a vCenter Server Folder in the Virtual Machine & Templates view where the VMs will be placed.
- Specify VM datastore/datastore cluster placement.
- Specify the number of VM vCPUs.
- Specify the number of VM RAM.
- Specify the VM disk size.
- Specify the VM disk type (Thin, Thick or Eager Zeroed Thick)
- Specify the VM guest OS type.
- Specify the VM prefix
- Use numeric incremental number after the VM prefix.
Below you’ll find the script i send to my customer that he used for creating and powering on 500 VMs, yes on one single NFS based datastore, and placed them randomly on the ESXi hosts in the vSphere cluster.
I can imagine the next question will be to clone VMs but i’m not sure:)
Updated 2014-02-25 – As i suspected, here you can find a post in which i create multiple VMs, based on one existing vSphere VM, using PowerCLI.
Change the following parameters in the below script to match your environment:
- $vCenter=”vc-demo01.vcdx56.com”
- $vCenterUser=”vcdx56@magnus”
- $vCenterUserPassword=”notsecret”
- $vm_count = “500”
- $numcpu = “2”
- $MBram = “2048”
- $MBguestdisk = “4096”
- $Typeguestdisk =”Thin”
- $guestOS = “winNetStandardGuest”
- $ds = “VCDX56-DS01”
- $Folder = “Scripttest”
- $Cluster = “VCDX56-02”
- $VM_prefix = “VCDX56-“
# # PowerCLI to create VMs # Version 1.0 # Magnus Andersson RTS # # Specify vCenter Server, vCenter Server username and vCenter Server user password $vCenter="vc-demo01.vcdx56.com" $vCenterUser="vcdx56magnus" $vCenterUserPassword="notsecret" # # Specify number of VMs you want to create $vm_count = "500" # # Specify number of VM CPUs $numcpu = "2" # # Specify number of VM MB RAM $MBram = "2048" # # Specify VM disk size (in MB) $MBguestdisk = "4096" # # Specify VM disk type, available options are Thin, Thick, EagerZeroedThick $Typeguestdisk ="Thin" # # Specify VM guest OS $guestOS = "winNetStandardGuest" # # Specify vCenter Server datastore $ds = "VCDX56-DS01" # # Specify vCenter Server Virtual Machine & Templates folder $Folder = "Scripttest" # # Specify the vSphere Cluster $Cluster = "VCDX56-02" # # Specify the VM name to the left of the - sign $VM_prefix = "VCDX56-" # # End of user input parameters #_______________________________________________________ # write-host "Connecting to vCenter Server $vCenter" -foreground green Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0 1..$vm_count | foreach { $y="{0:D1}" -f + $_ $VM_name= $VM_prefix + $y $ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random write-host "Creation of VM $VM_name initiated" -foreground green New-VM -Name $VM_Name -VMHost $ESXi -numcpu $numcpu -MemoryMB $MBram -DiskMB $MBguestdisk -DiskStorageFormat $Typeguestdisk -Datastore $ds -GuestId $guestOS -Location $Folder write-host "Power On of the VM $VM_name initiated" -foreground green Start-VM -VM $VM_name -confirm:$false -RunAsync }
This script was tested using the following software versions:
- VMware vCenter Server 5.5
- VMware ESXi 5.5
- VMware PowerCLI 5.5
- Microsoft PowerShell 2.0
Thanks to Luc Dekens & Christoffer Zettermark for script input.
26 pings
Skip to comment form ↓
Create multiple VMs using PowerCLI | TUSHAR TOPALE
February 4, 2014 at 2:11 pm (UTC 0) Link to this comment
[…] Create multiple VMs using PowerCLI. […]
Newsletter: February 3, 2014 | Notes from MWhite
February 5, 2014 at 5:00 am (UTC 0) Link to this comment
[…] And you never know when you might find that handy! I have several times. See the article here and be aware he is going to update the article in the near future to support virtual machines from […]
Create multiple VMs from existing vSphere VM using PowerCLI | VCDX56
February 25, 2014 at 6:36 pm (UTC 0) Link to this comment
[…] recently published the blog post “Create multiple VMs using PowerCLI” and as i mentioned in the post i thought the next request would be a PowerCLI script to create […]