Remotely extend a partition using WMI

一曲冷凌霜 提交于 2019-12-06 07:00:55

Pre-requisites:

  • PsExec from SysInternals Suite
  • PowerShell 2.0 or greater for PowerShell modules feature on the remote computer(s)

First, enable PSRemoting via PsExec:

psexec \\[computer name] -u [admin account name] -p [admin account password] -h -d powershell.exe "enable-psremoting -force"

The following PowerShell script will do the trick, without WMI, via PowerShell Sessions instead, and will do it for as many computers as you want:

Here is the driver script:

$computerNames = @("computer1", "computer2");
$computerNames | foreach {
  $session = New-PSSession -ComputerName $_;
  Invoke-Command -Session $session -FilePath c:\path\to\Expand-AllPartitionsOnAllDisks.ps1
  Remove-PSSession $session
}

And here is Expand-AllPartitionsOnAllDisks.ps1:

Import-Module Storage;

$disks = Get-Disk | Where FriendlyName -ne "Msft Virtual Disk";

foreach ($disk in $disks)
{
    $DiskNumber = $disk.DiskNumber;
    $Partition = Get-Partition -DiskNumber $disk.DiskNumber;

    $PartitionActualSize = $Partition.Size;
    $DriveLetter = $Partition.DriveLetter;
    $PartitionNumber = $Partition.PartitionNumber
    $PartitionSupportedSize = Get-PartitionSupportedSize -DiskNumber $DiskNumber -PartitionNumber $PartitionNumber;

    if ($disk.IsReadOnly)
    {
        Write-Host -ForegroundColor DarkYellow "Skipping drive letter [$DriveLetter] partition number [$PartitionNumber] on disk number [$DiskNumber] because the disk is read-only!";
        continue;
    }

    if ($PartitionActualSize -lt $PartitionSupportedSize.SizeMax) {
        # Actual Size will be greater than the partition supported size if the underlying Disk is "maxed out".
        # For example, on a 50GB Volume, if all the Disk is partitioned, the SizeMax on the partition will be 53684994048.
        # However, the full Size of the Disk, inclusive of unpartition space, will be 53687091200.
        # In other words, it will still be more than partition and unlikely to ever equal the partition's MaxSize.
        Write-Host -ForegroundColor Yellow "Resizing drive letter [$DriveLetter] partition number [$PartitionNumber] on disk number [$DiskNumber] because `$PartitionActualSize [$PartitionActualSize] is less than `$PartitionSupportedSize.SizeMax [$($PartitionSupportedSize.SizeMax)]"

        Resize-Partition -DiskNumber $DiskNumber -PartitionNumber $PartitionNumber -Size $PartitionSupportedSize.SizeMax -Confirm:$false -ErrorAction SilentlyContinue -ErrorVariable resizeError
        Write-Host -ForegroundColor Green $resizeError
    }
    else {
        Write-Host -ForegroundColor White "The partition is already the requested size, skipping...";
    }
}

See also my related research into doing this:

  1. https://serverfault.com/questions/946676/how-do-i-use-get-physicalextent-on-get-physicaldisk
  2. https://stackoverflow.com/a/4814168/1040437 - Solution using diskpart, requires knowing the volume number
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!