我的脚本来推动OS分区的大小调整。
Resize-Partition -DiskNumber 2 -PartitionNumber 1 -Size (60GB)如果由于windows无法收缩到所需的大小(“没有足够的空间执行操作”),因此出现错误,请尝试
Resize-Partition -DiskNumber 2 -PartitionNumber 1 -Size (70GB)循环继续进行,直到分区被调整大小。
问题是如何使用pwshell设置条件?
发布于 2017-02-02 17:34:03
以下是您需要做的工作的概述:
下面是我的代码(您可能希望将它保存到一个.ps1文件中,如果您对PowerShell函数还不熟悉,就导入它)。
Function Resize-PartitionDynamic
{
param(
[int] $diskNumber,
[int] $PartitionNumber,
[long] $Size
)
$currentSize = $size
$successful = $false
$maxSize = (get-partition -DiskNumber 0 -PartitionNumber 1).size
# Try until success or the current size is
# the size of the existing partition
while(!$successful -and $currentSize -lt $maxSize)
{
try
{
Resize-Partition -DiskNumber $diskNumber -PartitionNumber $PartitionNumber -Size $currentSize -ErrorAction Stop
$successful = $true
}
catch
{
# Record the failure and move the size
# half way to the size of the current partition
# feel free to change the algorithm move closer to the
# current size to something else...
# there probably should be a minimum move size too
$lastError = $_
$currentSize = $Size + (($maxSize - $successful)/2)
}
}
# If we weren't successful, throw the last error
if(!$successful)
{
throw $lastError
}
}下面是一个使用该函数的示例:
Resize-PartitionDynamic -diskNumber 2 -PartitionNumber 3 -Size 456GBhttps://stackoverflow.com/questions/42004026
复制相似问题