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 (
    [Parameter(Mandatory=$false)]
    [object] $WebhookData,

    [Parameter(Mandatory=$false)]
    [string] $vmName,

    [Parameter(Mandatory=$false)]
    [string] $UPN,

    [Parameter(Mandatory=$false)]
    [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 = ".domain.com"
$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 = {
    param($drive_letter)

    $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 {
    param(
        [Parameter(Mandatory = $true)]
        [string]$Message,

        [Parameter(Mandatory = $true)]
        [string]$Status
    )

    # 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
        $retryCount++

        # 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:
Share