Automatically create multiple hard drives on separate controllers in VMware

As mentioned earlier, my place of business sells and supports AppAssure. This means we have to create large volumes for the repository. For me, it became tedious to manually add these large drives, especially because of the need to power off the VM, set the SCSI LUN to the correct number, etc, etc. So, I created a script to do it for me!

Purpose:	Script to add multiple SCSI hard drives for guest
Author:		Michael Kenning (
Version:	1.4 (13 AUG 2014)
Notes:		Be sure to change the Variables!

$vmName = 'VMNAME'
$hdSize = '10500' # Size in GB
$numDisks = '1'
$datastore = 'DATASTORE'
$format = 'EagerZeroedThick '
$controllerType = 'Paravirtual'

# --- Connect to the host
try {
	Connect-VIServer -Server $myHost -ErrorAction Stop
catch [Exception]{
	Write-warning "Connecting to the vCenter server failed. Please check credentials and try again."
if ($error) {
	$Null = Disconnect-VIServer -Server * -confirm:$False
	Throw $error

$vm1 = Get-VM -Name $vmName

if ($vm1.PowerState -eq "PoweredOn") {
	$initialState = "On"
	Write-Host "Shutting Down" $vmName
	Shutdown-VMGuest -VM $vmName -Confirm:$false
	#Wait for Shutdown to complete
	do {
		#Wait 5 seconds
		Start-Sleep -s 5
		#Check the power status
		$vm1 = Get-VM -Name $vmName
		$status = $vm1.PowerState
	} until ($status -eq "PoweredOff")

# Create the Hard Disk and Controller
$vm2 = get-vm $vmName
$vm2 | New-HardDisk -CapacityGB $hdSize -Datastore $datastore -StorageFormat $format| New-ScsiController -Type $controllerType
$hd = $vm2 | Get-HardDisk | select -last 1

# Edit the controller to be the correct LUN or "Unit Number"
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "edit"
$spec.deviceChange[0].device = $hd.ExtensionData
$spec.deviceChange[0].device.unitNumber = 0


if ($initialState -eq "On") {
	Start-VM $vmName -Confirm:$false
Tagged with: ,
Posted in PowerCLI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: