我推送操作系统分区的脚本调整大小,例如
Resize-Partition -DiskNumber 2 -PartitionNumber 1 -Size (60GB)
如果我收到错误,因为Windows无法缩小到所需的大小(“没有足够的空间来执行操作”),请尝试
Resize-Partition -DiskNumber 2 -PartitionNumber 1 -Size (70GB)
并且循环继续运行,直到调整分区大小。
问题是如何使用pwshell设置条件?
答案 0 :(得分:0)
以下是您需要做的事情的概述:
这是我的代码(如果您不熟悉PowerShell函数,可能需要将其保存到.ps1
文件并导入它。)
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 456GB