【问题标题】:Check if the Read-Host input value is a String or an Integer or Float检查 Read-Host 输入值是字符串还是整数或浮点数
【发布时间】:2019-10-05 23:30:11
【问题描述】:

我正在创建一个脚本来获取用户输入的共享路径或路径,它将提供所提供路径的权限列表。使用 Read-Host command-let 我要求用户提供他需要的路径数量的输入,因此他/她需要一一放置路径,这将提供 ACL 列表。 在执行此操作时,我遇到了用户输入错误输入的问题,例如,如果用户提供的输入不是整数、双精度值或字符串,那么它应该转到我的 if 条件,然后他/她再次重新输入错误的数字会结束脚本并要求重新运行脚本。

我已经尝试了下面的脚本,但是在第 4 行我完全卡住了。这是我编写的完整代码。在第 3 行,当我输入一个值说 1.2 或任何字符串值说 Hello 或任何负整数或 0 值时,它应该转到第 4 行,否则它应该转到第 36 行的 elseif。有人可以帮我解决这个问题.我知道我可以用更短的方式做到这一点,但这里有些违规。

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"

【问题讨论】:

标签: powershell


【解决方案1】:

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

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
    }
}

你可以这样使用它:

# 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

}

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多