gpt4 book ai didi

powershell - 使用 WMI 远程扩展分区

转载 作者:行者123 更新时间:2023-12-02 23:41:13 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

先决条件:

  • 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 Sessions 来解决问题,并且可以为任意数量的计算机执行此操作:

    这是驱动程序脚本:
    $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...";
    }
    }

    另请参阅我的相关研究:
  • https://serverfault.com/questions/946676/how-do-i-use-get-physicalextent-on-get-physicaldisk
  • https://stackoverflow.com/a/4814168/1040437 - 使用diskpart的解决方案,需要知道卷号
  • 关于powershell - 使用 WMI 远程扩展分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41976411/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com