We recently had a customer that we onboarded for a managed service. Part of this service is that we monitor the OS and it turned out there were a LOT of VMs that had very little free disk space left. Now this seemed like a perfect opportunity to apply some automation using PowerCLI.
I grew tired of stringing together cmdlets to achieve the desired result so I decided to write a small powershell function. I exported the disk space alerts from our monitoring system in such a format that I can use this as input to extend the disks. All of the alerts were related to C: drives so I was a bit lazy and just assumed that the C: drive resides on the first disk. Also I don’t bother checking for snapshots. The Set-HardDisk cmdlet will raise an error if a snapshot is present.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function Invoke-StorageExtension{ #This provides the function with Debug/Verbose/WhatIf parameters [CmdletBinding(SupportsShouldProcess)] Param( [parameter(Mandatory=$true, ValueFromPipeline=$true)] $VM, [parameter(Mandatory=$true)] [int] $DiskNum, [parameter(Mandatory=$true)] [decimal] $IncreaseGB ) # Get disk details and calculate new capacity in GB $Disk = Get-HardDisk -VM $vm | Where-Object {$_.Name -eq "Hard disk "+$DiskNum} $NewCapacityGB = $Disk.CapacityGB + $IncreaseGB # Set disk size. You can use WhatIf to simulate this change. if ($PSCmdlet.ShouldProcess($Disk.Name,"Setting CapacityGB: $NewCapacityGB")) { Set-HardDisk -HardDisk $Disk -CapacityGB $NewCapacityGB -Confirm:$false } } |