Create a VM with a script in VMWare

  • Mission: Easily and fast create a VM in VMWare via Script

  • Symptoms
    • You don’t want to create VM’s via GUI as it takes long to conclude
      • OR
    • You want to accelerate VM creation to a few steps

  • Prerequisites
    • Have VMware.PowerCLI module installed

In this script, where you have a [] you need to change the information for the ones appropraite to your environment. For instance:

New-VM -Name $VM_Name -NumCpu $VM_CPU -MemoryGB $VM_Memory -DiskGB $VM_Disk -GuestId windows9Server64Guest -ResourcePool [Your_Ressource_Pool]

You need to replace [Your_Ressource_Pool] to the name of your environment’s ressource pool

There are 4 places in total in this script you need to change the data. There are comments in the script of where do you need to change it.


The Script

# Connect to the host/vcenter
# If a certificate error message pops-up run Set-PowerCLIConfiguration -InvalidCertificateAction choosing the most appropriate option for you
# The Connect-VIServer command will ask you the id to connect to the host/vcenter. This ID needs to have the privileges to create VMs or else it will fail
# In this section you need to replace the [Host_OR_Vcenter_Name] inputting the host/vcenter name of your environment 
Connect-VIServer [Host_OR_Vcenter_Name]

# Enter the name of the VM
$VM_Name = Read-Host "Please enter the name of the new VM"

# Check if the name already exists in VMWare environment
# It compares the variable with the existent machines names
# !!! If the name is ok, i.e. it does not exist yet, an error message will be displayed but the script will proceed anyways
$VM_Name_Validation = Get-VM $VM_Name -ErrorAction SilentlyContinue | Select Name -ExpandProperty Name
if (Compare-Object $VM_Name, $VM_Name_Validation -DifferenceObject $VM_Name_Validation -ErrorAction SilentlyContinue) {
Write-Host "This name is already in use. Please rerun the script and choose another name."
Break
}

# Amount of CPUS
$VM_CPU = Read-Host "Please specify the ammount of CPU"

# Amount of RAM
$VM_Memory = Read-Host "Please specify the amount of RAM (GB)"

# Disk size
$VM_Disk = Read-Host "Please specify the disk size (GB)"

# Machine Creation
# In this section you need to replace the [Your_Ressource_Pool], [Name_of_your_datastore] and [Name_of_your_virtual_network] inputting the information from of your environment 
New-VM -Name $VM_Name -NumCpu $VM_CPU -MemoryGB $VM_Memory -DiskGB $VM_Disk -GuestId windows9Server64Guest -ResourcePool [Your_Ressource_Pool] -Datastore [Name_of_your_datastore] -NetworkName [Name_of_your_virtual_network]

# Set the SCSI controller to Paravirtual (most performant and current)
$scsiController = Get-HardDisk -VM $VM_Name | Select -First 1 | Get-ScsiController
Set-ScsiController -ScsiController $scsiController -Type "ParaVirtual"

# Network adapter settings
Get-VM $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -Type Vmxnet3 -Confirm:$False

# CPU/RAM HotAdd and EFI settings
$vmName = $VM_Name
$vm = Get-VM -Name $vmName
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.CpuHotAddEnabled = $true
$spec.MemoryHotAddEnabled = $true
$spec.Firmware = [VMware.Vim.GuestOsDescriptorFirmwareType]::efi
$vm.ExtensionData.ReconfigVM($spec)

About me

Over 20 years working with IT for multiple fields (logistics, Olympic games, oil and gas, insurance, pharmaceuticals, etc).

Sometimes find solutions on the internet can be challenging. That’s why I decided to create techmission.ca, where I’ll gather some solutions I have to apply on my environments as I receive my “missions” (that’s the way I name client’s requests).

Hope the solutions published here can help you guys as it helps me 🙂

Featured Posts