用户输入使用PowerShell进行验证

时间:2017-07-31 17:55:16

标签: validation powershell user-input

问题

我正在开发自定义用户输入验证方法,但我很难让它正常工作。

以下是该脚本的基本概念:

$answer = Read-Host "Are you sure you wish to continue? [Y]es or [N]o"
while (!$answer -AND $answer -ine "y" -AND $answer -ine "yes" -AND $answer -ine "n" -AND $answer -ine "no") {
   $answer = Read-Host "You hand entered an invalid response.`nPlease enter [Y]es or [N]o"
}
if ($answer -ieq "yes" -OR $answer -ieq "y") {
   do action
}
else {
   don't do action
}

我面临的问题是,通过上述验证,它只确认字符串是否为空/ null。无论我在答案中输入什么,其他4个标准似乎都不重要,脚本将退出WHILE循环并继续执行IF-ELSE语句。

示例:

如果我输入"是","是","是"," y"," Y"或者任何其他类似的变体,应用程序将跳过/退出WHILE语句并继续进行IF语句,按预期执行所需的操作。如果我没有输入任何内容(保留字符串null / empty),它将按预期保留在While循环内。但是,任何其他输入都将导致应用程序跳过/退出WHILE语句并继续执行ELSE语句,这不是我想要的。

我尝试过使用-eq,-ieq,-ceq,-like,-ilike,-ne,-ine,-notlike和-inotlike 我甚至一直在玩-AND和-OR运算符,希望我可能搞砸了那个设置。 不幸的是,所有人都给了我相同的结果。

提前感谢您提供任何建议。

顺便说一句:我还没能在这里找到任何与我试图接近的东西有关的东西,而且我已经研究了大部分官方网站,以了解运营商的运作方式。到目前为止,所有逻辑都表明它应该可以工作,但是当我运行它时它会失败。

3 个答案:

答案 0 :(得分:2)

这只有当$ an​​swer为空时才会起作用,因为条件语句的第一部分!$answer AND ...

您需要将其更改为“OR”,如下所示:while (!$answer -OR ($answer -ine "y" -AND $answer -ine "yes" -AND $answer -ine "n" -AND $answer -ine "no"))

答案 1 :(得分:2)

使用内置的PowerShell选择工具。例如:

$choices = [Management.Automation.Host.ChoiceDescription[]] @(
  New-Object Management.Automation.Host.ChoiceDescription("&Yes","Do whatever.")
  New-Object Management.Automation.Host.ChoiceDescription("&No","Do not do whatever.")
)
$choice = $Host.UI.PromptForChoice("Are you sure?","This will do whatever.",$choices,1)

您可以轻松添加到选项列表中。如果您选择第一个选项$choice,则0变量将设置为1,等等。PromptForChoice变量将设置为Enterpublic class ObjectA { public int data; public ObjectA() { this.data = 1; } public changeData(int x) { this.data = x; } } public class ObjectB { public int data; public ObjectB() { this.data = 1; } public changeData(int x) { this.data = x; } } 方法的最后一个参数确定默认选项(即,如果您只按<html> <head> </head> <script type="text/javascript"> function toggle(source) { checkboxes = document.getElementsByName('chk1[]'); for(var i=0, n=checkboxes.length;i<n;i++) { checkboxes[i].checked = source.checked; } } </script> <body> <h2><font color="white">Welcome to Cars and Coffee</h2> <p>Sign up to receive if you are interested in receiving information from Cars and Coffee.</p> <p>Please select at least one option</p> <form name="newEntry" action="page.php" method="POST"> name<input type=text length=60 size=30 name="name"><br> email<input type=text length=60 size=30 name="email"><br> car<input type=text length=60 size=30 name="car"><br> phone<input type=text length=60 size=30 name="phone"><br> <input type="submit" name="submit" email="add" car="add" phone="add" > </form> <form action="page.php" method="POST"> <input type="checkbox" name="chk[]" value="cars_coffee">Cars & Coffee<br> <input type="checkbox" name="chk[]" value="kentucky">Kentucky Derby Party<br> <input type="checkbox" name="chk[]" value="july">July 4th<br> <input type="checkbox" name="chk[]" value="labor">Labor Day<br> <input type="checkbox" name="chk[]" value="new_year">New Years Day<br> <input type="checkbox" name="chk[]" value="all" onClick="toggle(this)">Select All<br> </form> <?php mysql_connect("localhost", "db", "pw"); mysql_select_db("db"); $qh = mysql_query("select * from all_emails order by id desc"); if (@mysql_num_rows($qh)) { /* the if(@...) syntax makes PHP supress any warnings that might come out of that function. */ /* mysql_fetch_object(query handle); * returns an object whose contents are that of one rotw in the database. */ while ($obj = mysql_fetch_object($qh)) { echo " <p> $obj->email<br> $obj->name<br> $obj->car<br> $obj->phone<br> <p> <hr> "; } } ?> </body> </html> )。

答案 2 :(得分:-1)

$answer = Read-Host "Are you sure you wish to continue? [Y]es or [N]o"
While (!($answer) -and 
       (($answer.Substring(0,1).ToUpper() -ne 'Y') -or
        ($answer.Substring(0,1).ToUpper() -ne 'N')))
{
   $answer = Read-Host "You entered an invalid response.`r`nPlease enter [Y]es or [N]o"
}

( ... )