Skip to content

Instantly share code, notes, and snippets.

@maxautomation
Created July 22, 2022 14:26
Show Gist options
  • Save maxautomation/912c2daff3a27933172fc850e99fecd4 to your computer and use it in GitHub Desktop.
Save maxautomation/912c2daff3a27933172fc850e99fecd4 to your computer and use it in GitHub Desktop.
Script to Extend the VM disk from Vcenter as well as from OS end
Connect-VIServer vcentername
$compName = Read-Host "Please provide the Server Name"
$inputdrive = Read-Host "Please provide the drive letter that needs to be extended"
$inputdrive1 = $inputdrive + ":"
[int]$sizetoincrease = Read-Host "Please provide the size you want to increase in GB"
$snapshot = Get-Snapshot -VM $compName
if(!$snapshot)
{
$details = Get-VM $compName | Get-DatastoreCluster
$percentagebefore =[math]::Round(($details.FreeSpaceGB/$details.CapacityGB)*100,2)
$freeafter = $details.FreeSpaceGB - $sizetoincrease
$percentageafter = [math]::Round(($freeafter/$details.CapacityGB)*100,2)
if($percentageafter -gt 20)
{
Write-Host "Enough Free Space in Datastore`nFreespace% Before $percentagebefore`nFreespace% After $percentageafter" -ForegroundColor Green
$volTab = @{}
$Results=@()
foreach($disk in (Get-CimInstance -ComputerName $compName -ClassName Win32_DiskDrive)){
foreach($partition in (Get-CimAssociatedInstance -InputObject $disk -ResultClassName Win32_DiskPartition)){
Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk | %{
$volTab.Add("$($disk.SCSIBus):$($disk.SCSITargetId)",($_.DeviceID,$_.VolumeName -join '|'))
}
}
}
$vm = Get-VM -Name $compName
foreach($ctrl in Get-ScsiController -VM $vm){
foreach($disk in (Get-HardDisk -VM $vm | where{$_.ExtensionData.ControllerKey -eq $ctrl.Key})){
$obj = [ordered]@{
VM = $vm.name
HD = $disk.Name
VMDK = $disk.Filename
Device = "$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)"
Drive = ""
Label = ""
}
if($volTab.ContainsKey("$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)")){
$obj.Drive = $volTab["$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)"].Split('|')[0]
$obj.Label = $volTab["$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)"].Split('|')[1]
}
$Results += New-Object -TypeName PSObject -Property $obj
}
}
foreach ($Result in $Results)
{
if ($inputdrive1 -like $Result.Drive)
{
$Drivenumber = $Result.HD
#Write-Host "Drive is $Drivenumber"
}
}
[int]$Dsize = Get-VM $compName | Get-HardDisk | Where-Object { $_.Name -eq $Drivenumber } | select -ExpandProperty CapacityGB
$finalsize = $Dsize + $sizetoincrease
#Write-Host $finalsize
$temp1 = Get-VM $compName | Get-HardDisk | Where-Object { $_.Name -eq $Drivenumber } | Set-HardDisk -Confirm:$false -CapacityGB $finalsize
$tempname = Get-VM $compName | Get-HardDisk | Where-Object { $_.Name -eq $Drivenumber }
#Start-Sleep -Seconds 60
Invoke-Command -ComputerName $compName -ScriptBlock { Update-HostStorageCache
$size = Get-PartitionSupportedSize -DriveLetter $using:inputdrive
Resize-Partition -DriveLetter $using:inputdrive -Size $size.SizeMax
}
}
Else
{
Write-Host " Datastore free space will reduce to $percentageafter% if increased by $sizetoincrease GB" -ForegroundColor Red
}
}
If($snapshot)
{
Write-Host "Snapshot exist for the VM. Please remove the snapshot and run the script again for extending the drive" -ForegroundColor Yellow
}
Pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment