Eric Monjoin
Staff Consulting Architect but also pilot, spending time in front of my computer or flying in the air...

PowerShell : Automatically Expand Writable Volumes

One of my customer raised an issue about managing writable volumes as they are mainly used by developers who are unable to restrain their desires to fill disk with tons of data and can’t do some clean up 🙂

The problem of course is that the writable volume grow up to no free space and unfortunately the user is not aware of that as the “ReportSystemFreeSpace” Reg Key works only for App Volumes 2.x and not App Volumes 4.x

The following script can be used manually or integrated with Task Scheduler but requires App Volumes Server 2206 (4.7) to handle “requested_mb” parameters. You can use it with previous version but you don’t know if a request to expand disk was previously initiated.

Disclaimer : The script it self is not supported by me or VMware so use it at your own risk however it’s using official VMware API for App Volumes, and thank to Chris Halstead as his initial script helped me a lot (https://github.com/chrisdhalstead/appvolumes-expand-wv)

<#
.SYNOPSIS
  Script to update the size of VMware App Volumes Writable Volumes based on free space, if Writable Volumes is detached and no expand is already requested
  It checks how much free space is available and then takes the current size of the WV and expands it by the desired size.

  For the connection to App Volumes, 
	
.INPUTS
  Fill the following parameters :
  AppVolumesServerFQDN       : Specify the App Volumes Server
  AppVolumesDomain           : Domain Name used to connect to App Volumes
  MinimumSpaceLeft           : Trigger to set the minimum space left on the Volumes before expanding the disk
  Ammount_To_Increase_In_MB  : Specify by how much MB you want to expand the disk

.OUTPUTS
  

.NOTES
  Version:        1.0
  Author:         Eric Monjoin - emonjoin@vmware.com
  Creation Date:  9/8/2022
  Product :       For VMware App Volumes 2206 and later
  Purpose/Change: 
  **This script is not supported by VMware but use official API **
  New sizes won't be reflected until a user logs in and attaches the Writable Volume	

    
.EXAMPLE
 .\AVDiskExpand_v1.0.ps1                                     

#>


#----------------------------------------------------------[Declarations]----------------------------------------------------------

#App Volumes Info
$AppVolumesServerFQDN = "myappvolsrv.domain.dom"
$AppVolumesDomain = "domain.dom"
$MinimumSpaceLeft = "4096"                 # Specify minimum space left before resizing Writable Volumes
$Ammount_To_Increase_In_MB = "4096"      # Specify increment size in MB to add to existing Writable Volumes

#----------------------------------------------------------[Logging]----------------------------------------------------------

# Uncomment / Comment to set login method 

# App Volumes automatic login 
$AppVolumesUser = "username"
$AppVolumesPassword = "password"

<#
# App Volumes login with credential requested
Write-Host "Enter AppVolumes Username (no domain)" -ForegroundColor Green
$AppVolumesCred       = Get-Credential
$AppVolumesUser       = $AppVolumesCred.UserName
$AppVolumesPassword   = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($AppVolumesCred.Password))
#>



$Credentials = @{
  username = "$AppVolumesDomain\$AppVolumesUser"
  password = "$AppVolumesPassword"
}

#-----------------------------------------------------------[Run]------------------------------------------------------------

#Connect to App Volumes Manager
Write-Host "Logging on to App Volumes Manager: $appvolumesserverfqdn"
Invoke-RestMethod -Method Post -Uri "https://$AppVolumesServerFQDN/app_volumes/sessions" -Body $Credentials -SessionVariable avsession

#Get Writable Volumes
$Get_WV = Invoke-RestMethod -WebSession $avsession -Method Get -Uri "https://$AppVolumesServerFQDN/app_volumes/writables" -ContentType 'application/json'

$json = $Get_WV.data

foreach ($item in $json)
    {
  if (
        ($item.free_mb -lt $MinimumSpaceLeft) -and ($item.attached -match "Detached") -and ($item.requested_mb -eq $null)
        
        )
  {
    $avid = $item.id
    $Actual_Size_In_MB = $item.total_mb
    $New_Size_In_MB = $Actual_Size_In_MB + $Ammount_To_Increase_In_MB
    
    Invoke-RestMethod -WebSession $avsession -Method Post -Uri "https://$AppVolumesServerFQDN/app_volumes/writables/grow?bg=0&size_mb=$New_Size_In_MB&volumes%5B%5D=$avid" -ContentType 'application/json'
    }
    
}

You may also like...

Leave a Reply

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