PowerCLI – New Disk by VM Memory Size

PowerCLI

You have a list of VMs that need a new disk added (ex. windows pagefile).  You want that new disk to be 1.5 times the size of each VMs memory.

Note – The new disk drive will be on the first SCSI adapter.

# found https://vmscribble.com

Write-Host "
The script will add a new disk drive on the first SCSI adapter based on the VM memory size times 1.5
" 

# Connect to your vCenter
$vCenter = Read-Host "Enter the vCenter FQDN"
Connect-VIServer $vCenter

Write-Host " "
$textfile = Read-Host "Enter the location and text file name of VMs in scope"
$listofvms = Get-Content -path $textfile

# change 1.5 to another multiple in line:  $size = $thevm * 1.5 
foreach ($vm in $listofvms)
{
    $thevm = get-vm $vm | Select-Object -ExpandProperty MemoryGB
    $size = $thevm * 1.5
    get-vm $vm | New-HardDisk -CapacityGB $size | Select-Object Parent, Filename, CapacityGB
}


# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false