使用 WMI 远程扩展分区 [英] Remotely extend a partition using WMI

查看:57
本文介绍了使用 WMI 远程扩展分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PowerShell 和 WMI 远程扩展在 VMware 上运行的 Windows 虚拟机上的 C 驱动器分区.

I'm trying to use PowerShell and WMI to remotely extend a C drive partition on Windows VMs running on VMware.

这些虚拟机没有启用 WinRM,这不是一个选项.我正在尝试做的相当于在 AD 控制台中远程管理 Active Directory 计算机对象以扩展分区,但在 PowerShell 中.

These VM do not have WinRM enabled and that's not an option. What I'm trying to do is an equivalent of remotely managing an Active Directory computer object in an AD console to extend a partition, but in PowerShell.

我已经设法通过 Win32 WMI 对象提取分区信息,但还不是扩展部分.

I'v already managed to pull partition informations through Win32 WMI objects but not yet the extension part.

有谁知道如何在这样的驱动器上最大化 C 分区?

Does anyone know how to max out a C partition on a drive like that?

推荐答案

先决条件:

  • 来自 SysInternals Suite 的 PsExec
  • PowerShell 2.0 或更高版本,用于远程计算机上的 PowerShell 模块功能

首先,通过 PsExec 启用 PSRemoting:

First, enable PSRemoting via PsExec:

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

以下 PowerShell 脚本将在没有 WMI 的情况下通过 PowerShell 会话来实现这一点,并且可以为任意数量的计算机执行此操作:

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:

这里是驱动脚本:

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

这里是 Expand-AllPartitionsOnAllDisks.ps1:

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 - 使用 diskpart 的解决方案,需要知道卷号
  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

这篇关于使用 WMI 远程扩展分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆