[Powercli] Script : deploy VMs with random configurations

Ce message est également disponible en : French

[Powercli] Script : deploy VMs with random configuration

How to deploy VMs with random configurations based on a template for your lab or infrastructure

For my lab, i need really often to deploy several virtual machines very quickly based on a VM or a template.

This is why, i have create a script to deploy VMs based on a template with random configurations (vCPU/Memory) and for your infrastructure too, like your host and datastore. The idea was to create VMs as fast as possible on all my infrastructure to generate a random load.

 

##########################################################################
## Version : 1.0                                                        ##
## Created by : Julien VARELA                                           ##
## Release Date : 12/12/2016                                            ##
## Name : DeployVMLAb.ps1                                               ##
##                                                                      ##
##########################################################################
##Verif des modules VMware
$PSNAP= Get-PSSnapin VMware.VimAutomation.Core -erroraction ‘silentlycontinue’
if ($? -eq $false)
{
##Load VMware Modules
Write-Host " Loading VMware modules "
asnp vmware.*
}
Else
{
Write-host " VMware modules are already loaded "
}

$vCenter = Read-Host "vCenter FQDN"
$login = $host.ui.PromptForCredential("User/Password", "please type your log in and password ", " ", " NetBiosUserName ")

$VMs=read-host "Number of VMs to deploy"

connect-viserver -server $vcenter -credential $login

$i=1 # Initialize variable to 1.

[string]$NamingConvention = "LABTEST-VM-" # Naming convention for your VMs
[string]$folderLoc = "Discovered virtual machine" # Name of the folder where the VMs will be deployed
$SourceVM = "W2K12R2_Template" #Template Name
$OsConfiguration =Get-OSCustomizationSpec -name "W2K12R2_CUSTOM" #Name of the customization spec

while ($i -le $VMs) {
    $NumCPUs = (Get-Random -Minimum 1 -Maximum 6) #vCPU configuration (Between 1 and 6)
    $MemoryGB = ("4","6","8","10","12") | Get-Random #Memory configuration (Bewteen 4 and 12 GB)

    $targetDatastore = Get-Datastore | Where {($_.ExtensionData.Summary.MultipleHostAccess -eq "True") -and ($_.FreeSpaceMB -gt "50000")} | Get-Random
    $targetVMhost = Get-VMHost | Where { $_.ConnectionState -eq "Connected" } | Get-Random # Select a connected host


    $VMName = $NamingConvention + $i


    if ((Get-VM $VMName -ErrorAction SilentlyContinue).Name -eq $VMName) { #Check if VM already existe
        Write-Host "$VMName already exist, task will be skipped" -ForegroundColor Yellow
        }
    else {
        Write-Host "Deployment of the VM $VMName in the folder $folderLoc ..." -ForegroundColor Green

#Create our VM
        $task = New-VM -name $VMName -Template $SourceVM -OSCustomizationSpec $OsConfiguration -Datastore $targetDatastore -VMHost $targetVMhost -RunAsync #VM creation based on the template
        while($task.ExtensionData.Info.State -eq "running"){
  sleep 2
  $task.ExtensionData.UpdateViewData('Info.State')
}
        $task = Set-VM $VMName -NumCpu $NumCPUs -MemoryGB $MemoryGB -Confirm:$false -RunAsync #VM configuration
        while($task.ExtensionData.Info.State -eq "running"){
  sleep 2
  $task.ExtensionData.UpdateViewData('Info.State')
}
sleep 3
        $task =Start-VM -VM $VMName -Confirm:$false # Start of the VM
        while($task.ExtensionData.Info.State -eq "running"){
  sleep 2
  $task.ExtensionData.UpdateViewData('Info.State')
}
        }
    $i++
}

Once the script has been executed, you can see the current task in your vCenter task tab.
vCenter Task

Et Voila, you will have VMs with different memory/cpu configurations.

VM créées

 

You can find the script here : DeployVMLAb.ps1

Enjoy !

 

Leave a Reply