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.

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
  }
}

Rudolf Kleijwegt

I am an experienced IT professional with over 20 years of hands-on experience designing, deploying, and maintaining IT infrastructure in both enterprise and service provider environments. My skills span across Linux and Windows and a multitude of server applications, allowing me to excel in a wide range of IT roles. Currently, my primary focus is on Software Defined DataCenter and DevOps. I am passionate about staying up to date with the latest trends in the industry to achieve superior outcomes.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *