Why?

I’m quite bored of creating new VM from ISO image with vCenter GUI. You know it:

  • open browser
  • upload iso image to datastore from local machine
  • run wizard for creating new VM
  • go through all required parameters
  • and finally boot VM from ISO image

I have a little project in my mind, which requires deploying 4 different Linux distros. So it is a good time to test creating new VMs with PowerCLI.

PowerCLI is a command line tool for interacting with vSphere management. It runs on top of PowerShell.

Prerequisites

Install powershell (Mac in my case):

brew install --cask powershell

Run powershell:

pwsh

And connect to vCenter with valid credentials:

connect-VIServer -Server vc.demo.corp -Protocol https

Some useful commands before starting

Get Datacenters:

Get-Datacenter

Get Datastores:

Get-Datastore

Get ESXi servers:

Get-VMHost

Say my datacenter calls ‘DC’ and datastore where I want to upload ISO image, and also deploy VM is ‘DATASTORE’, then:

Get folders inside given datastore:

Get-ChildItem vmstore:/DC/DATASTORE/

I will also need to assign valid OS family to my new VM. To get all available:

$anyHost = Get-VMHost | Select-Object -First 1
$envBrowser = Get-View -Id (Get-View -Id $anyHost.ExtensionData.Parent).EnvironmentBrowser
$vmxVersion = ($envBrowser.QueryConfigOptionDescriptor() | Where-Object {$_.DefaultConfigOption}).Key
$envBrowser.QueryConfigOption($vmxVersion, $null).GuestOSDescriptor | Select-Object Id, FullName

I will need also network (distributed port group in my case), list them all:

Get-VDPortgroup

As we have VMs organized under different folders, I can check current folders:

Get-folder

Now I have all information for creating new VM.

Uploading iso

Say my image is located locally here: /Users/stefanbezo/Downloads/Rocky-10.1-x86_64-minimal.iso And I want to upload image here: DATASTORE/ISO

Then command for uploading is:

Copy-DatastoreItem -Item /Users/stefanbezo/Downloads/Rocky-10.1-x86_64-minimal.iso -Destination "vmstore:/DC/DATASTORE/ISO"

Creating new VM

First I need to assign my parameters for new VM under variables - it is usefull for next builds, everything I have on one place. Here:

$vmName     = "SB-Rocky-Template"
$hostName   = "lab-esx4.demo.sk"
$datastore  = "DATASTORE"
$network    = "DPG-VLAN116"
$isoPath    = "[$datastore] ISO/Rocky-10.1-x86_64-minimal.iso"
$folder     = "SB"
$cpu        = 2
$mem        = 4
$disk       = 40
$guestID    = "rockylinux_64Guest"

Parameters are self-explanatory.

And some object definitions required during creating VM:

$vmHostObj = Get-VMHost -Name $hostName
$folderObj = Get-Folder -Name $folder
$DPGObj = Get-VDPortgroup -name $network

And finaly, script for VM creating:

$vm = New-VM `
    -Name $vmName `
    -VMHost $vmHostObj `
    -Datastore $datastore `
    -Location $folderObj `
    -NumCpu $cpu `
    -MemoryGB $mem `
    -DiskGB $disk `
    -Portgroup $DPGObj `
    -GuestId $guestID

Next I need to add CD drive with iso file for VM boot:

New-CDDrive `
    -VM $vm `
    -IsoPath $isoPath `
    -StartConnected `
    -Confirm:$false

And after few seconds I can start my shiny new VM with:

Start-VM $vm

Now VM is ready to connect and setup with console.

Some other usefull commands

Power VM Off

Stop-VM -VM $vm -Confirm:$false -ErrorAction SilentlyContinue

Reload VM object if needed base on VM name:

$vm = Get-VM -Name $vmName

Delete VM from disk

Remove-VM -VM $vm -DeletePermanently -Confirm:$false