You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求助:如何用PowerShell远程批量扩展虚拟机C盘分区?

Hey there! As someone who’s fumbled through remote disk expansion with PowerShell as a newbie, I totally get where you’re coming from. Since you didn’t share your current script, I’ll walk through the most common pitfalls people hit with this task, plus a working sample script you can compare against your own code.

Common Mistakes to Diagnose

Let’s start with the usual suspects that trip up beginners:

  • WinRM Permissions & Setup: Remote PowerShell relies on WinRM being enabled and configured correctly on target machines. If you haven’t already, you need to run Enable-PSRemoting -Force (as admin) on each VM first. Also, make sure your local machine trusts the remote hosts (use Set-Item WSMan:\localhost\Client\TrustedHosts -Value "VM1,VM2" -Concatenate -Force to add them).
  • Skipping Disk Expansion: When you increase VDI capacity, the VM’s disk might not automatically recognize the new space. You have to first resize the disk itself before you can expand the partition (C: drive). A lot of beginners forget this step.
  • Batch Processing Errors: If your script isn’t properly reading the text file (e.g., not using Get-Content, or handling blank lines/invalid machine names), it’ll fail silently for some entries. Always add error handling to catch connection issues.
  • Wrong Disk/Partition Target: If your script targets Disk 0 blindly, it might hit the wrong disk (especially if the VM has other drives). You need to filter to find the disk that hosts the C: partition specifically.
Sample Working Script for Batch Remote Expansion

Here’s a tested script that handles the full workflow—you can tweak it to match your setup:

# Read the list of VMs from your text file
$vmList = Get-Content -Path "C:\Path\To\Your\VMs.txt"

# Loop through each VM in the list
foreach ($vm in $vmList) {
    Write-Host "`nProcessing $vm..." -ForegroundColor Cyan

    try {
        # Create a remote PowerShell session (requires admin rights on the VM)
        $remoteSession = New-PSSession -ComputerName $vm -ErrorAction Stop

        # Run the disk expansion logic remotely
        Invoke-Command -Session $remoteSession -ScriptBlock {
            # Find the disk that contains the C: partition
            $systemDisk = Get-Disk | Where-Object {
                $_.PartitionStyle -ne "RAW" -and
                ($_.GetPartitions() | Where-Object { $_.DriveLetter -eq "C" })
            }

            if (-not $systemDisk) {
                Write-Error "Could not locate the disk hosting the C: drive on $using:vm"
                return
            }

            # Step 1: Expand the disk to use all available space from VDI
            $systemDisk | Set-Disk -IsOffline $false -ErrorAction SilentlyContinue
            $systemDisk | Resize-Disk -Size $systemDisk.Size

            # Step 2: Expand the C: partition to fill the disk
            $cPartition = Get-Partition -DriveLetter "C"
            if ($cPartition) {
                $maxSize = ($cPartition | Get-PartitionSupportedSize).SizeMax
                $cPartition | Resize-Partition -Size $maxSize
                Write-Host "Successfully expanded C: drive on $using:vm to $maxSize bytes" -ForegroundColor Green
            } else {
                Write-Error "C: partition not found on $using:vm"
            }
        }

        # Clean up the remote session
        Remove-PSSession -Session $remoteSession
    } catch {
        Write-Host "Failed to process $vm : $_" -ForegroundColor Red
    }
}
How to Compare This to Your Script

Check these points against your own code:

  1. Are you using New-PSSession or Invoke-Command with proper admin credentials?
  2. Does your script first resize the disk (not just the partition)?
  3. Are you filtering the disk correctly to target the one with the C: drive?
  4. Do you have error handling (try/catch blocks) to see why specific VMs are failing?
  5. Are you running your local PowerShell session as an administrator?

If you share your actual script, I can help pinpoint exact issues—but this should cover most of the common mistakes. Good luck!

内容的提问来源于stack exchange,提问作者Storm2508

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:28:20