使用 WMI 远程扩展分区
Remotely extend a partition using WMI
我正在尝试使用 PowerShell 和 WMI 在 VMware 上的 Windows 虚拟机 运行 上远程扩展 C 驱动器分区。
这些 VM 没有启用 WinRM,这不是一个选项。
我想要做的是相当于在 AD 控制台中远程管理 Active Directory 计算机对象以扩展分区,但在 PowerShell 中。
我已经设法通过 Win32 WMI 对象提取分区信息,但还没有扩展部分。
有谁知道如何在这样的驱动器上最大化 C 分区?
Pre-requisites:
- 来自 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...";
}
}
另请参阅我对此的相关研究:
- https://serverfault.com/questions/946676/how-do-i-use-get-physicalextent-on-get-physicaldisk
- - 使用 diskpart 的解决方案,需要知道卷号
我正在尝试使用 PowerShell 和 WMI 在 VMware 上的 Windows 虚拟机 运行 上远程扩展 C 驱动器分区。
这些 VM 没有启用 WinRM,这不是一个选项。 我想要做的是相当于在 AD 控制台中远程管理 Active Directory 计算机对象以扩展分区,但在 PowerShell 中。
我已经设法通过 Win32 WMI 对象提取分区信息,但还没有扩展部分。
有谁知道如何在这样的驱动器上最大化 C 分区?
Pre-requisites:
- 来自 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...";
}
}
另请参阅我对此的相关研究:
- https://serverfault.com/questions/946676/how-do-i-use-get-physicalextent-on-get-physicaldisk
- - 使用 diskpart 的解决方案,需要知道卷号