2016-08-30 44 views
0

我试图创建一个函数,要求用户输入驱动器的大小,如果驱动器大小大于100,则要求用户再次输入它。我的脚本只是口口声声说“无法要求更多,然后100GB”,即使用户输入50大于问题

这里是我的脚本

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):

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

+3

我认为问题是,你要比较一个字符串一个整数,尝试:如果([INT] $ C_DiskGB -gt $ MAXC) –

回答

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" 
} 
+0

奏效谢谢! –