Bulk expanding disks after increasing their physical size

About

This script deals with expanding the logical volumes in windows after they have been expanded in the hypervisor. It will work on any windows server which you have remote PowerShell access to, in fact it tests for that before attempting to expand the disks.

It does however make an assumption about disks and volumes. They are hard coded rather than dynamically determined. You’ll need to update it for your server drive lettering standards.

Features

  • Works remotely
  • Works on multiple servers at once

Script

$AllServers = ""

Function DiskPartExpand 
{
    $Server = $env:computername
    $DiskPartDeploymentPath = "C:\SysAdmin\Scripts\Config"
    $DiskPathScriptFile = $DiskPartDeploymentPath + "\DiskPartExpand.txt"
    $DataVolume         = "d:"
    $LogsVolume         = "e:"
    $TempVolume         = "f:"
    $PageFileVolume     = "w:"
    $DataVolExists      = Test-Path -Path $DataVolume
    $LogsVolExists      = Test-Path -Path $LogsVolume
    $TempVolExists      = Test-Path -Path $TempVolume
    $PageFileVolExists  = Test-Path -Path $PageFileVolume
    $DeployPathExists   = Test-Path -Path $DiskPartDeploymentPath -PathType Container
    
$ExpandPageVol = @"

sel vol 3
extend

"@

$ExpandDataVol = @"

sel vol 4
extend

"@    

$ExpandLogsVol = @"

sel vol 5
extend

"@ 

$ExpandTempVol = @"

sel vol 6
extend

"@ 

If ($DeployPathExists -eq $false) 
    {
        New-Item -Path $DiskPartDeploymentPath -ItemType Directory | Out-Null
        Write-Output "$Server Config folder created"
    }
    else {Write-Output "$Server Config folder already exists"}

If ($DataVolExists -eq $true) 
{
    Write-Output "$Server $DataVolume Volume Found"
    $DiskPartCommand += $ExpandDataVol
}
    else {Write-Output "$Server $DataVolume Volume not found"}

If ($LogsVolExists -eq $true) 
{
    Write-Output "$Server $LogsVolume Volume Found"
    $DiskPartCommand += $ExpandLogsVol
}
    else {Write-Output "$Server $LogsVolume Volume not found"}

If ($TempVolExists -eq $true) 
{
    Write-Output "$Server $TempVolume Volume Found"
    $DiskPartCommand += $ExpandTempVol
}
    else {Write-Output "$Server $TempVolume Volume not found"}

If ($PageFileVolExists -eq $true) 
{
    Write-Output "$Server $PageFileVolume Volume Found"
    $DiskPartCommand += $ExpandPageVol
}
    else 
    {
        Write-Output "$Server $PageFileVolume Volume not found"
    }

Set-Content -Path $DiskPathScriptFile -Value $DiskPartCommand -force -ErrorAction stop
$cmd = "cmd /c diskpart /s $DiskPathScriptFile"
$processID = (Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ErrorAction Stop).processid
}

Foreach ($Server in $AllServers) 
{
    winrm id -r:$Server 2>$null | Out-Null
    if ($LastExitCode -eq 0) 
    {
        Write-Host "$Server Online...............................[OK]" -ForegroundColor green
        Write-Host "$Server Attempting Disk Expansion.............[?]" -ForegroundColor Yellow
        Invoke-Command -ComputerName $Server -ScriptBlock ${function:DiskPartExpand}
        Write-Host "$Server Disks Expanded.......................[OK]" -ForegroundColor Green
    }
    else {Write-Host "$Server unavailable...........................[!]" -ForegroundColor Red}
}