首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查Read-Host输入值是String、Integer还是Float

检查Read-Host输入值是String、Integer还是Float
EN

Stack Overflow用户
提问于 2019-05-19 17:47:51
回答 1查看 1.4K关注 0票数 0

我正在创建一个脚本来获取用户输入的共享路径或路径,它将提供所提供路径的权限列表。使用Read-Host命令-让我要求用户提供他所需的路径数量的输入,因此他/她需要一个接一个地放入将提供ACL列表的路径。当这样做的时候,我得到了用户输入错误输入的问题,例如如果用户提供的输入不是整数,或者是双精度值,或者是字符串,那么它应该转到我的if条件,并且他/她再次键入错误的数字,它将结束脚本,并要求重新运行脚本。

我试过下面的脚本,但是在第四行我完全卡住了。这是我写的完整代码。在第3行,当我输入一个值,比如1.2,或者任何字符串值,比如Hello,或者任何负整数或者0值,它应该转到第4行,否则它应该转到else is,也就是第36行。有人能帮我解决这个问题吗。我知道我可以用一种更短的方式来做,但是这里有些东西是违规的。

代码语言:javascript
复制
Clear-Host
Get-Date
$num_path= Read-Host "`n `n Enter number of paths"
if (($num_path -as [int]) -le 0 -or ($num_path -as [double]) -is [double])
{
    Write-Host "Error: Value you have Entered is either less than or equal to 0 or not an interger or it is a String value.`nKindly enter correct value." -ForegroundColor Black -BackgroundColor Cyan
    $New_num_path= Read-Host "`n `n Re-Enter number of paths"
    if (($New_num_path -as [int]) -gt 0 -and !($New_num_path -as [double]) -is [double])
    {

        Write-Host "`n `n \\ServerName\FilePath `n `n Enter File Path in above format"
        For($i=1; $i -le $New_num_path; $i++)
        {
            $paths= Read-Host "`n `nEnter File path no. $i " #Enter path with $ sign after drive name e.g E$\Applications
            Write-Host "`n `n"
            if (Test-Path -Path $paths)
            {
                foreach($path in $paths)
                {
                    Get-Acl -Path $path | fl
                }
            }

            Else
            {
                Write-Host "Error: Path '$paths' does not exist" -ForegroundColor Black -BackgroundColor Cyan
            }
        }
    }
    Else
    {
        Write-Host "Error: You have input wrong value. Kindly Re-run the script again." -ForegroundColor Black -BackgroundColor Cyan
    }

}
Elseif(($num_path -as [int]) -gt 0)
{

    Write-Host "`n `n \\ServerName\FilePath `n `n Enter File Path in above format"
    For($i=1; $i -le $num_path; $i++)
    {
        $paths= Read-Host "`n `nEnter File path no. $i " #Enter path with $ sign after drive name e.g E$\Applications
        Write-Host "`n `n"
        if (Test-Path -Path $paths)
        {
            foreach($path in $paths)
            {
                Get-Acl -Path $path | fl
            }
        }

        Else
        {
            Write-Host "Error: Path '$paths' does not exist" -ForegroundColor Black -BackgroundColor Cyan
        }
    }
}
Else
{
    Write-Host "Error: You have input wrong value. Kindly Re-run the script again." -ForegroundColor Black -BackgroundColor Cyan
}
Write-Host "`n `n `n `t `t `t------------------------------------------------------THE END------------------------------------------------------`n"
EN

回答 1

Stack Overflow用户

发布于 2019-05-19 19:48:59

我认为一个小的辅助函数可能会派上用场。可能是这样的:

代码语言:javascript
复制
function Ask-Integer {
    [CmdletBinding()]
    param(
        [string]$Prompt = 'Please enter a value.',
        [string]$CancelOption = $null,
        [int]$MinValue = [int]::MinValue,
        [int]$MaxValue = [int]::MaxValue
    )

    # enter an endless loop
    while ($true) {
        Clear-Host

        [int]$value = 0
        if ($CancelOption) { 
            Write-Host "Type $CancelOption to cancel." -ForegroundColor Yellow
        }
        $result = Read-Host $Prompt

        # user cancelled, exit function and return nothing
        if ($result -eq $CancelOption) { return $null }

        if ([int]::TryParse($result, [ref]$value)) {
            if ($value -ge $MinValue -and $value -le $MaxValue) {
                return $value
            }
        }

        # if we got here, the user entered something other than the wanted integer, so try again
        Write-Warning "Invalid choice.. Please enter a whole number between $MinValue and $MaxValue."
        Sleep -Seconds 3
    }
}

您可以这样使用它:

代码语言:javascript
复制
# use the function to return an integer value or $null if the user cancelled
$num_path = Ask-Integer -Prompt 'Enter the number of paths' -CancelOption 'Q' -MinValue 1 -MaxValue 10
# test if the user did not cancel
if ($null -ne $num_path) {

    # rest of your code

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56206628

复制
相关文章

相似问题

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