Mass Hard Disk Creator (Ver 0.1)

Think about scenario which two or more hard disks should be created and added to a specific VM(s); there are multiple ways like using GUI (vSphere Client) or PowerCLI.   😉
If normal GUI (vSphere Client) going to be use, it would be a time-consuming process but when trying to do it with PowerCLI (automated way), it’d be so much faster.

I had two VMs which needed “8 hard disks on SATA and 2 hard disk on SSD” to be added, which means adding 20 disks in total so for preventing suffering from same process in future I made bellow script. ( script demo on YouTube )

Warning:
1. This is first version of script and only work in interactive mode and might have some bug; I’d be really grateful if you could kindly let me know any possible bug/problem you saw. I’d try to update it in a way so it can get parameters or/and accepting file as source.
2. Please test it on one test VM before using it on any production VM.

[wpdm_file id=9]

###################################
# Title: Mass Hard Disk Creator
# Ver: 0.1
# Author: Sohrab Kasraeian Fard (@Kasraeian)
# Date: 11/15/2012
###################################
CLS
# Get VM List
Write-Host "VM List"
Get-VM | sort Name

# Selecting the target VM
$VM = Read-Host ("`nPlease select the target VM to add new hard disk(s) to it")

# Checking VM current state
$CH = Get-HardDisk -VM $VM
$CC = Get-VM -Name $VM | Get-ScsiController

if ($CH.count -ge 2) {
$AHS = 60 - $CH.count
} elseif ($CH.count -ne "") {
$AHS = 59
}
if ($CC.count -ge 2) {
$ACS = 4 - $CC.count
} elseif ($CC.count -ne "") {
$ACS = 3
}

$vmh = Get-VM -Name $VM | select VMHost
$vmh = $vmh.VMHost.Name
$vmhds = Get-VMHost $vmh | Get-Datastore | sort name

#Main Process
if ($AHS -lt 60 -or $CH.count -ne "") {
Write-Host "`nThere are $ahs free slot available for adding hard disk for selected VM."

[Int]$Qty = Read-Host ("How many hard disk(s) do you want to add (Max $ahs)?")
[Int]$Cap = Read-Host ("Please define new hard disk(s) capacity in GB [Range: 0.001-1024]")
$NDSR = Read-Host ("Should new hard disk(s) be saved on other datastore?")
if ($NDSR -like "yes") {
$k=1; Write-Host "`n"
$vmhds | %{ Write-Host "$($k).`t$($_.Name)`t$($_.CapacityGB)`t$($_.FreeSpaceGB)"; $k++ }
$SNDSI = Read-Host ("Please select new datastore from above list")
$SNDS = $vmhds[$SNDSI-1].Name
}

if ($ACS -le 4 -and $ACS -gt 0) {
$NCR = Read-Host ("Want to add new controller for new disks?")
if ($NCR -like "yes") {
$RNCT = Read-Host ("`n[BLP] BusLogic Parallel`n[LLP] LSI Logic Parallel`n[LLS] LSI Logic SAS`n[VPV] VMware Paravirtual`nWhich controller type should be added [Other = OS Default]?")
switch($RNCT) {
"BLP" { $NCT = "VirtualBusLogic" }
"LLP" { $NCT = "VirtualLsiLogic" }
"LLS" { $NCT = "VirtualLsiLogicSAS" }
"VPV" { $NCT = "ParaVirtual" }
default { $NCT = "Default" }
}
}
} else {
Write-Host "There is no empty slot on selected VM for adding any new SCSI Controller."
}
$Approve = Read-Host "Please approve to create new $qty disk(s) for $vm with capacity of $cap GB"

if ($Approve -eq "Yes" -and $Qty -le $AHS -and $Cap -ge 0.001 -and $Cap -le 1024) {
for ($j = 1; $j -le $Qty; $j++) {
if ($j -eq 1 -and $NCR -like "yes" -and $NDSR -notlike "yes") {
New-HardDisk -VM $VM -CapacityGB $Cap -StorageFormat Thin | New-ScsiController -Type $NCT -Confirm:$false
$SC = Get-VM -Name $VM | Get-ScsiController
$SCT = $SC.count - 1
} elseif ($j -eq 1 -and $NCR -like "yes" -and $NDSR -like "yes") {
New-HardDisk -VM $VM -CapacityGB $Cap -StorageFormat Thin -Datastore $SNDS | New-ScsiController -Type $NCT -Confirm:$false
$SC = Get-VM -Name $VM | Get-ScsiController
$SCT = $SC.count - 1
} elseif ($j -ge 2 -and $NCR -like "yes" -and $NDSR -notlike "yes") {
New-HardDisk -VM $VM -CapacityGB $Cap -StorageFormat Thin -Controller "SCSI controller $SCT" -Confirm:$false
} elseif ($j -ge 2 -and $NCR -like "yes" -and $NDSR -like "yes") {
New-HardDisk -VM $VM -CapacityGB $Cap -StorageFormat Thin -Controller "SCSI controller $SCT" -Datastore $SNDS -Confirm:$false
} elseif ($NDSR -notlike "yes") {
New-HardDisk -VM $VM -CapacityGB $Cap -StorageFormat Thin -Confirm:$false
} elseif ($NDSR -like "yes") {
New-HardDisk -VM $VM -CapacityGB $Cap -StorageFormat Thin -Datastore $SNDS -Confirm:$false
}
}
} elseif ($Approve -ne "Yes") {
Write-Host "Process terminated by user." -BackgroundColor Yellow -ForegroundColor Red
} elseif ($Qty -gt $AHS) {
Write-Host "Can't create disk more than available free slot!" -BackgroundColor Yellow -ForegroundColor Red
} elseif ($Cap -lt 0.001 -or $Cap -gt 1024) {
Write-Host "Selected capacity is out of possible range!" -BackgroundColor Yellow -ForegroundColor Red
} else {
Write-Host "Please check given parameter and try again!" -BackgroundColor Yellow -ForegroundColor Red
}
} elseif ($AHS -eq 0) {
Write-Host "There is no empty slot on selected VM for adding any hard disk."
}

It's your kindness to leave a reply/feedback