【问题标题】:User input validation in PowerShellPowerShell 中的用户输入验证
【发布时间】:2021-06-20 14:42:46
【问题描述】:

我在 PowerShell 中有这段代码。我需要对整数进行用户输入验证,并写入输出说明输入有什么问题并期待正确的输入。

$FromObj = "Please input object number"
$FromInput = Read-Host $FromObj

while(($FromInput -isnot [int])) {
    #I also want to put in the while that the input has to be an integer, -gt 0, and -lt 800
    Write-Output "Your input has to be a number."
    $FromInput = Read-Host $FromObj
    }
if ($FromInput -le 0) {
    Write-Output "Your input has to be a number greater than 0!"
    $FromInput = Read-Host $FromObj
    }
elseif ($FromInput -ge 800) {
    Write-Output "Your input has to be a number less than 800!"
    $FromInput = Read-Host $FromObj
    }
else {
    $FromInput = $FromInput -as [int]
    }

但是,这对我不起作用。你能帮我用上面显示的输出来验证它吗?

【问题讨论】:

  • 我认为 IsNumeric 对我不起作用,我只需要整数验证。我不知道我是否正确,但有没有办法将其验证为整数?
  • 第一个答案 ($FromInput -match "^\d+$") 将为您执行此操作,在您确认字符串包含整数后,您只需将其转换为:[int]$FromInput。将您的 ($FromInput -isnot [int]) 替换为 !($FromInput -match "^\d+$") 并在您的第一个 if 之前放置一个 $FromInput = [int]$FromInput
  • 你完全正确!像往常一样,我错了。你的回答很有魅力。我真诚地道歉并感谢您的帮助!

标签: powershell validation


【解决方案1】:

我认为Nicks's link in comment 已经为您的问题提供了不同的正确答案,即:使用regex 匹配\d+(数字)或使用[int]::TryParse(...),但要解释为什么您的代码不起作用:

  • Read-Host 会将用户输入存储为 string,除非您在最后一个条件下(else {...} 语句)操作它。

这就是为什么您永远无法通过 $FromInput -isnot [int] 条件的原因。

要修复代码,您可以简单地尝试从一开始就存储用户输入 -as [int],并且由于您的代码中有 3 个 Read-Host,您可以将用户输入尝试 -as [int] 保存在 ScriptBlock 中,然后可以可以根据需要轻松执行多次:

$FromObj = "Please input object number"
$giveMeNumber = { (Read-Host $FromObj) -as [int] }
$FromInput = & $giveMeNumber

while($FromInput -isnot [int]) {
    Write-Output "Your input has to be a number."
    $FromInput = & $giveMeNumber
}
if ($FromInput -le 0) {
    Write-Output "Your input has to be a number greater than 0!"
    $FromInput = & $giveMeNumber
}
elseif ($FromInput -ge 800) {
    Write-Output "Your input has to be a number less than 800!"
    $FromInput = & $giveMeNumber
}

请注意,即使这样有效,该语句也不完全正确,因为您在输入 is [int] 后打破了 while loop,用户可能会强制输入不正确的输入。

试试这个,它将无限期地执行直到正确的输入:

Clear-Host
$ErrorActionPreference = 'Stop'
$FromObj = "Please input object number"

$scriptBlock = {
    try
    {
        $FromInput = [int](Read-Host $FromObj)

        # Note I'm using Write-Host instead of Write-Ouput, this is because
        # we don't want to store the invalid inputs messages in the
        # $userInput variable.
        if ($FromInput -le 0) {
            Write-Host "Your input has to be a number greater than 0!"
            & $scriptBlock
        }
        elseif ($FromInput -ge 800) {
            Write-Host "Your input has to be a number less than 800!"
            & $scriptBlock
        }
        else {
            $FromInput
        }
    }
    catch
    {
        Write-Host "Your input has to be a number."
        & $scriptBlock
    }
}

$userInput = & $scriptBlock

【讨论】:

  • 像往常一样,我错了。你和尼克的答案都完全符合我的想法。由于我的问题中缺乏描述,你们仍然把它钉牢了。 Srsly,不知道你是怎么做到的。有时候想想你在读心术,你就是那么好。尊重!
  • @Sv3n 如果这个答案对您有帮助,请考虑accepting it,因为它可能会在将来对其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2017-09-11
相关资源
最近更新 更多