Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查用户是否在powershell脚本中没有输入的情况下按enter键?_Powershell - Fatal编程技术网

如何检查用户是否在powershell脚本中没有输入的情况下按enter键?

如何检查用户是否在powershell脚本中没有输入的情况下按enter键?,powershell,Powershell,我有一个powershell脚本,希望用户输入一个值,该脚本返回一个随机字符串作为密码。如果他们只是在提示输入密码长度时按enter键,我希望它默认为9个字符 如何处理无输入 我试过这样的方法,但我觉得不太对: 写入主机输入密码长度要求: $length = Read-Host IF ( $length -eq $NULL) { Do continue on with the value of 9 for the length} ELSE {Use $length for th

我有一个powershell脚本,希望用户输入一个值,该脚本返回一个随机字符串作为密码。如果他们只是在提示输入密码长度时按enter键,我希望它默认为9个字符

如何处理无输入

我试过这样的方法,但我觉得不太对:

写入主机输入密码长度要求:

$length = Read-Host
IF ( $length -eq $NULL) 
    { Do continue on with the value of 9 for the length}
ELSE
    {Use $length for the rest of the script}
其他部分工作正常;然而,当生成密码时,我发现自己一遍又一遍地输入9。我宁愿按回车键


非常感谢您的帮助

要做到这一点,最好的方法是为脚本添加一个参数,并使用V2验证属性。这也将防止它们放入坏数据,如字符串

param( [参数(必需=$true)] [ValidateRange(1,9)] [内部] $Length )

这将为您提供更好的验证以及一致的和本地化的错误

希望这对($length-eq“”)有所帮助

而不是 如果($length-eq$NULL)

应该这样做。

我会说“按设计工作”。因为$lenght不是空的。正确的测试是:

if ($length -eq [string]::empty)
因此,也许这两个测试是结合在一起的


JP

PowerShell非常棒,因为您可以经常缩短代码,而且可以正常工作:)

为什么?


在if块中,将$Length设置为9,然后您就可以开始了。@jason我只想显示条件,我想John能够完成其余的:)无论如何,thx用于审阅。John可能没有意识到,我有时喜欢明确表示。:)
if (!$length) { 
  Do continue on with the value of 9 for the length
} ...
[bool]$null         # False
[bool]''            # False
[bool]'something'   # True