Remotely expanding partitions after expanding them in the hypervisor

Once you have expanded an underlying disk in a virtual machine, you still need to expand the disk in the guest. If, like me, you see logging on to a server as an admission of failure, you need a way of scripting it remotely. Here is a function for doing that. You may want to adjust the “includes” to match your possible drive letters.


Function Invoke-RemoteVolumeExpand
{
Param
(
$Server
)
Function Invoke-VolumeExpand
{
$Include = "C|W|D|E|F"
$Partitions = Get-Partition | Where-Object {$_.DriveLetter -Match $Include}
Foreach ($Partition in $Partitions)
{
$Type = $Partition.Type
$Letter = $Partition.DriveLetter
If ($Type -eq "Basic")
{
$Size = $Partition | Get-PartitionSupportedSize
$MaxSize = $Size.sizeMax
$MinSize = $Size.sizeMin
If ($MaxSize -ne $MinSize)
{
Try
{
Write-Output "$Server Expanding $Letter to $MaxSize"
$Partition | Resize-Partition -size $MaxSize -ErrorAction SilentlyContinue
}
Catch
{
Write-Output "$Server No further expansion possible for $Letter"
}
}
}
}
}
Invoke-Command -ComputerName $Server -ScriptBlock ${Function:Invoke-VolumeExpand}
}