【问题标题】:Remotely extend a partition using WMI使用 WMI 远程扩展分区
【发布时间】:2017-02-01 09:41:16
【问题描述】:

我正在尝试使用 PowerShell 和 WMI 在 VMware 上运行的 Windows VM 上远程扩展 C 盘分区。

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

我已经设法通过 Win32 WMI 对象提取分区信息,但还没有扩展部分。

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

【问题讨论】:

  • 我认为单独使用 WMI 是不可能的。 Related。在 Windows 8/Server 2012 及更高版本上,您也可以尝试Resize-Partition
  • 您是否尝试过使用 powershell 远程处理或 PSexec.exe?
  • @SandeepKs 来自问题:“这些虚拟机没有启用 WinRM,这不是一个选项。”
  • @AnsgarWiechers 好!那么是否可以远程复制 psexec.exe 并运行它然后扩展音量?
  • @AnsgarWiechers 谢谢。然而,似乎 Resize-Partition 需要 CimSession 远程执行。当我尝试使用计算机名称和管理员 PSCredentials 的“New-CimSession”时,它抱怨 IIS 和 WinRM 都没有运行。我设法提取了这样的信息:Get-WMIOjbect Win32_LogicalDisk -ComputerName $vm_name -Credentials $credentials 有人知道像 Set-WMIObject 这样允许更改的东西吗?还有什么?

标签: powershell wmi partitioning


【解决方案1】:

先决条件:

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

首先,通过 PsExec 启用 PSRemoting:

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

以下 PowerShell 脚本将在不使用 WMI 的情况下通过 PowerShell 会话来解决问题,并且可以为任意数量的计算机执行此操作:

这里是驱动脚本:

$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:

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...";
    }
}

另请参阅我的相关研究:

  1. https://serverfault.com/questions/946676/how-do-i-use-get-physicalextent-on-get-physicaldisk
  2. https://stackoverflow.com/a/4814168/1040437 - 使用 diskpart 的解决方案,需要知道卷号

【讨论】:

  • 这似乎在某些机器上失败,原因我不明白。特别是,在某些附加了非 C 盘 EBS 卷的 AWS EC2 实例上,EBS 卷不会显示在 Get-Disk 的输出中
  • 请参阅 AWS 文档以将磁盘映射到卷:docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/…
  • 如果您无权执行此操作,AWS 提供的脚本将会失败,但会显示一条略显神秘的错误消息,并不能说明问题出在哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2011-12-29
  • 2011-01-29
  • 1970-01-01
相关资源
最近更新 更多