Select Page

This morning I need to clone a couple dozen VMs to test a dHCI system so I created the following script to do just that!

 

#
## PowerCLI to create VMs
## Version 1.0 12/20/2020
## Tom Fenton
##
#
# Specify vCenter Server, vCenter Server username and vCenter Server user password
$vCenter=”192.168.0.36″
$vCenterUser=”administrator@vmware.local”
$vCenterUserPassword=”FatChance!”
#
# Specify number of VMs you want to create
$vm_count = “24”
#
# Specify the VM you want to clone
$clone = “Win10_Template”
#
# Specify the Customization Specification to use
# TJF $customspecification=”Win10-customization”
#
# Specify the datastore or datastore cluster placement
$ds = “SR-DS1”
#
# Specify vCenter Server Virtual Machine & Templates folder
$Folder = “CallCenter”
#
# Specify the vSphere Cluster
$Cluster = “dhci-cl”
#
# Specify the prefix for the VMs
$VM_prefix = “CCWin10Desktop_”
#
# 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 -VM $clone -VMHost $ESXi -Datastore $ds -Location $Folder –      RunAsync
# Uncomment the line below and comment the line above if a cust spec is used.

# New-VM -Name $VM_Name -VM $clone -VMHost $ESXi -Datastore $ds -Location $Folder –       OSCustomizationSpec $customspecification -RunAsync

 

   ## the power function needs a little work as the clones may not be fully created before the power on is attempted.

   write-host “Power On of the VM $VM_name initiated” -foreground green
   Start-VM -VM $VM_name -confirm:$false -RunAsync
}

 

 

 

 

Social Share Buttons and Icons powered by Ultimatelysocial