Lab Automation with PowerCLI Part 3: Create & Delete Virtual Machines


PowerCLI provides interfaces to create/modify/delete a number of things in your VMWare environment:

  • Virtual Machines
  • Snapshots
  • Virtual Switches
  • Hard Disks
  • Roles
  • Permissions
  • etc...

Today we are going to discuss Virtual Machines

 

Previous Article: Gathering Performance Statistics

Next Article: Working with Snapshots

Notes:

To Create a virtual machine using PowerCLI we can use the New-VM CmdLet. A complete list of available parameters for the command are available on the reference page. To begin, Open Powershell ISE and follow along below.

Note: Uncomment the Add-PSSnapin line if this is the first time you are running the script. That line needs to be run once per ISE session.

Basic VM Creation (Basics):

#Add-PSSnapin "Vmware.VimAutomation.Core"
#    Uncomment if the script doesn't work

#Connect to ESXi host, Create VM and disconnect
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password $0mething!

    New-VM -name TestVM -DiskMB 400 -MemoryMB 256

Disconnect-VIServer -Server $ESXIHost -Confirm:$false

 

This command will create a new VM named TestVM and give it a 400MB disk with 256MB RAM. If you have vSphere open while you run the script you will see an entry like this:

CreateTestVMSuccess.png

 

Create a VM, Specify the Datastore location, Set # of CPUs

#Connect to ESXi host, Create VM and disconnect
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password $0mething!

    New-VM -name TestVM2 -DiskMB 500 -MemoryMB 512 -Datastore RAID0 -NumCpu 2

Disconnect-VIServer -Server $ESXIHost -Confirm:$false


Ok- what we did in the first example was nice, but don't we want to have more granularity when we setup VMs? This script shows how to add datastore and number of CPUs when creating a VM

 

Comprehensive VM Setup (OS Type, Floppy/CD, Thin provision, etc...):

#Connect to ESXi host, Create VM and disconnect
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password $0mething!

    New-VM -name TestVM3 -NumCpu 2 -DiskStorageFormat Thin -DiskMB 500 -MemoryMB 512 -Floppy -CD -GuestId rhel4_64Guest -Datastore datastore1

Disconnect-VIServer -Server $ESXIHost -Confirm:$false

While there are more options you can give New-VM, what we've covered here are the essentials. This final script adds on what we've done in the first 2 examples by:

  • Specifying a Thin provisioned disk ( DiskStorageFormat Thin )
  • Adding a floppy ( -Floppy )
  • Adding a CD drive ( -CD )
  • Specifying the OS Type  ( -GuestId rhel4_64Guest )
    Note: This creates a NIC per VMWare recommendations
    Note 2: For a complete list of GuestId types, see the Reference page

 

I'll wrap-up Part 3 by noting the usage of the Remove-VM command (It's pretty simple) using a couple of examples:

Example 1: Remove from Inventory

#Connect to ESXi host, Remove VM, disconnect
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password $0mething!

    Remove-VM TestVM2 -Confirm:$false

Disconnect-VIServer -Server $ESXIHost -Confirm:$false

This example will remove a VM from Inventory (should be non-destructive). I recommend trying it out to validate the non-descructive behavior as VMWare may change it in some later version

Note: -Confirm:$false means that the script won't prompt you to continue.

 

Example 2: Delete permanently

#Connect to ESXi host, Delete VM, disconnect from host
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password $0mething!

    Remove-VM TestVM2 -DeletePermanently -Confirm:$false

Disconnect-VIServer -Server $ESXIHost -Confirm:$false


This usage will Permanently delete the VM Files

 

Closing Comment: You can download the Scripts from Today's article by clicking on Example Script Download (Adfly)

 

Previous Article: Gathering Performance Statistics

Next Article: Working with Snapshots