Table of Contents
How to Resize VDI Virtual Disks with PowerCLI Script
To resize virtual disk in multiple Virtual Machines, a scripted process is recommended as it will make the job quicker and manageable.
This document is based on the following technology:
- VMware ESX 4.1.0
- Windows 7 Enterprise 32-bit SP0 Virtual Machine
If different version is used, the manual process and scripted process might need to be adjusted as some PowerCLI functions might have been deprecated.
Tools for Scripted Process
The script has been created to help the whole process quicker and less error prone. The script is developed with VMware vSphere PowerCLI, sysinternal and DiskPart.
There are 2 major steps to resize a disk in Virtual Machine:
- Resize the virtual disk of the Virtual Machine
- Extend the Guest Operating Systems’ volume
Those 2 steps are explained in more details on the section below
Resize the virtual disk of the Virtual Machine
To resize multiple or potentially hundred or thousand Virtual Machine, doing the manual resizing can definitely lead to error, not to mention the time it is going to take to do it.
A Script has been created to do this particular task. The script is based on VMware vSphere PowerCLI and sysinternal tools. PowerCLI is used to call the vSphere Web Service API to resize the virtual disk on each Virtual Machine. Sysinternal is used to execute the diskpart utility remotely on each Virtual Machine
Script Requirements
To be able to execute the script, the following file(s) and application are required:
- vSphere PowerCLI installed on the machine that is going to be used to run the PowerCLI script
- PsExec.exe file, downloaded from microsoft.com
- A text file contains a list of the computer name per line
- A text file contains a list of command for DiskPart
Extend the Guest Operating Systems’ volume
[source]
PsExec.ext @C:\Temp\Computers.txt –u DOMAIN\Username –h diskpart /s \\computer\share\Diskpart.txt
DiskPart-Checker.TXT
list volume
exit
Diskpart-Extend.TXT
Select Volume 2
extend
exit
[/source]
vSphere PowerCLI VDI-Extend.PS1
[source language=”powershell”]
#Get the vCenter Server Name
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$vC = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the vCenter computer name", "Computer", "$env:computername")
#Connect to vCenter
Connect-VIServer -Server $vC
#Prompt File Function
function PromptFor-File
{
param
(
[String] $Type = "Open",
[String] $Title = "Select Computer File (One Computer Name per Line)",
[String] $Filename = $null,
[String[]] $FileTypes,
[switch] $RestoreDirectory,
[IO.DirectoryInfo] $InitialDirectory = $null
)
[void][System.Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)
if ($FileTypes)
{
$FileTypes | % {
$filter += $_.ToUpper() + " Files|*.$_|"
}
$filter = $filter.TrimEnd("|")
}
else
{
$filter = "All Files|*.*"
}
switch ($Type)
{
"Open"
{
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Multiselect = $false
}
"Save"
{
$dialog = New-Object System.Windows.Forms.SaveFileDialog
}
}
$dialog.FileName = $Filename
$dialog.Title = $Title
$dialog.Filter = $filter
$dialog.RestoreDirectory = $RestoreDirectory
$dialog.InitialDirectory = $InitialDirectory.Fullname
$dialog.ShowHelp = $true
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
return $dialog.FileName
}
else
{
return $null
}
}
#File Content
$file = PromptFor-File
$content = Get-Content $file
#Get Hard Disk Name
$hdd = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the VM Disk Name", "VM Disk Name", "Hard disk 1")
#Get Hard Disk Size
$hddsGb = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the Disk Size in GB", "VM Disk Size", "")
$hddsKb = [int]$hddsGb * 1024 * 1024
foreach($c in $content)
{
try{
#Extend the vmdk file
Get-VM -Name $c | Get-HardDisk | Where-Object {$_.Name -eq $hdd} | Set-HardDisk -CapacityKB $hddsKb
}
catch{
Write-Host "This VM: " + $c + " is not recognized" -ForegroundColor Red
}
}
[/source]