Resize Partition After Adding Disk Space

This script can be used to resize the OS partition after you have added additional disk space in Azure on VMware. Current script is triggered by a webhook and uses Azure Automation but can be modified to fit your requirements.

param (
    [object] $WebhookData,

    [string] $vmName,

    [string] $UPN,

    [string] $TICKET

if ($WebhookData.RequestBody) {
    $payload = $WebhookData.RequestBody | ConvertFrom-Json

    if ($payload.vmName) {
        $vmName = $payload.vmName

    if ($payload.UPN) {
        $UPN = $payload.UPN
    if ($payload.TICKET) {
        $TICKET = $payload.TICKET

Write-Output "vmName: $vmName"
Write-Output "UPN: $UPN"
Write-Output "TICKET: $TICKET"

# Check Payload
Write-Output $vmName

# Add FQDN to the VMName
$domainName = ""
$fqdn = $vmName + $domainName

# Verify Host Name is correct
Write-Output $fqdn

# Get Credentials from Azure Automation and Use to Run PowerShell command to expand disk.
$creds = Get-AutomationPSCredential -Name "<AccountName>"

# PowerShell script to resize partition
$scriptToRun = {

    $size = (Get-PartitionSupportedSize -DriveLetter $drive_letter)
    Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax

# Define the log file path
$logFilePath = "C:\Temp\DiskSpaceIncrease.txt"

# Function to write log entries to the file
function Write-LogEntry {
        [Parameter(Mandatory = $true)]

        [Parameter(Mandatory = $true)]

    # Get the current timestamp
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

    # Create the log entry string
    $logEntry = "$timestamp - $Status - $Message"

    # Append the log entry to the file
    Add-Content -Path $logFilePath -Value $logEntry

# Set the maximum number of retries
$maxRetries = 4
# Set the current retry count
$retryCount = 0

do {
    try {
        # Execute the script on the target VM using PowerShell Remoting
        $session = New-PSSession -ComputerName $fqdn -Credential $creds
        Invoke-Command -Session $session -ScriptBlock $scriptToRun -ArgumentList "C" 
        Remove-PSSession -Session $session

        # Write a log entry for successful operations
        Write-LogEntry -Message "Disk space increase operation completed successfully." -Status "Success"
        # Reset the retry count as the operation was successful
        $retryCount = $maxRetries
    catch {
        # Increment the retry count

        # Write a log entry for the failed attempt
        Write-LogEntry -Message "Disk space increase operation failed. Retry attempt $retryCount of $maxRetries." -Status "Failure"

        # Wait 60 seconds before the next attempt
        if ($retryCount -lt $maxRetries) {
            Start-Sleep -Seconds 60
} while ($retryCount -lt $maxRetries)

# ToDo-Alert Team Chat if failure/or SNOW
if ($retryCount -eq $maxRetries) {
    # Take appropriate action on failure after all retries

Share or Save this: