问题大于问题

时间:2016-08-30 15:39:03

标签: function powershell if-statement

我正在尝试创建一个函数,要求用户输入驱动器的大小,如果驱动器大小大于100,则要求用户再次输入。即使用户输入50,我的脚本仍然会说“不能要求超过100gb”。

这是我的剧本

param (
        [string]$MaxC = "100"
)

function cDrive {

    $C_DiskGB = Read-Host "C: Drive - Disk Size in GB"
        if ($C_DiskGB -gt $MaxC) {

                Write-Host "Cannot ask for more then 100gb of disk space.  Please re-enter how much disk space you need for the c: drive" 
                cDrive 

             }
        else {

            Write-Host "C: Drive is $C_DiskGB GB" 

             } 

 }   

当我减少任何东西时的输出是: C:驱动器 - 磁盘大小(GB):50 不能要求超过100GB的磁盘空间。请重新输入c:驱动器所需的磁盘空间 C:驱动器 - 以GB为单位的磁盘大小:

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

好的,当你想要比较数字时,让我们开始比较字符串。将变量定义为数字([int]),问题应自行解决。

其次,你要打破范围。并不可怕,但是当它可以轻易避免时不推荐。这就是我的建议:

function cDrive {
Param([int]$MaxC = 100)
    [int]$C_DiskGB = Read-Host "C: Drive - Disk Size in GB"
    While($C_DiskGB -gt $MaxC){
        [int]$C_DiskGB = Read-Host "Cannot ask for more then $MaxC`gb of disk space. Please re-enter how much disk space you need for the c: drive" 
    } 
    Write-Host "C: Drive is $C_DiskGB GB"
}
相关问题