Tuesday, February 26, 2019

Expanding drives via powershell

The drives on your Availability Group replicas are running low on free space...
So you put in a request to your storage admin to expand the volumes. If you're lucky, they'll also expand the partitions within windows for you as well. Otherwise you'll have to RDP into each replica and expand each partition by hand. This just happened to me, so I decided to leverage PowerShell

Well no more!!!

$nodes = @()
$nodes += {Replica1}
$nodes += {Replica2}
$nodes += {Replica3}

$cmd1 = {    
    Get-DISK |
        WHERE NUMBER -NE $null |
        Sort-Object number
    }

$cmd2 = {
    # Set the disk number
    $diskno = $args[0].number
        
    Get-Partition -DiskNumber $diskno |         
        where type -ne $null 
}

$cmd3 = {

    $diskno = $args[0].number
    $partno = $args[1].PartitionNumber
        
    Update-Disk -Number $diskno

    $size = (Get-PartitionSupportedSize -DiskNumber $diskno -PartitionNumber $partno)
    $size | ft -AutoSize

    Resize-Partition -DiskNumber 2 -PartitionNumber 2 -Size $size.sizemax    
}

foreach ($node in $nodes) {
    Write-Verbose $node -Verbose
    
    $disk = 
        Invoke-Command -ComputerName $node -ScriptBlock $CMD1 |
            ogv -PassThru -Title 'Select a disk'

    $part = 
        Invoke-Command -ComputerName $node -ScriptBlock $cmd2 -ArgumentList $disk |
            ogv -PassThru -Title 'Select the partition'

    Invoke-Command -ComputerName $node -ScriptBlock $cmd3 -ArgumentList $disk, $part
}