首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >收缩自动化

收缩自动化
EN

Stack Overflow用户
提问于 2017-02-02 13:54:54
回答 1查看 114关注 0票数 1

我的脚本来推动OS分区的大小调整。

代码语言:javascript
复制
Resize-Partition -DiskNumber 2 -PartitionNumber 1 -Size (60GB)

如果由于windows无法收缩到所需的大小(“没有足够的空间执行操作”),因此出现错误,请尝试

代码语言:javascript
复制
Resize-Partition -DiskNumber 2 -PartitionNumber 1 -Size (70GB)

循环继续进行,直到分区被调整大小。

问题是如何使用pwshell设置条件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-02 17:34:03

以下是您需要做的工作的概述:

  1. 将变量设置为所需分区的大小。
  2. 循环,直到成功为止,或者所需的大小是分区的当前大小。
  3. 在循环中,尝试将分区调整为变量中的大小。
    • 如果尝试失败,请使用一些算法向分区的实际大小移动。

下面是我的代码(您可能希望将它保存到一个.ps1文件中,如果您对PowerShell函数还不熟悉,就导入它)。

代码语言:javascript
复制
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
    }
}

下面是一个使用该函数的示例:

代码语言:javascript
复制
Resize-PartitionDynamic -diskNumber 2 -PartitionNumber 3 -Size 456GB
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42004026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档