【发布时间】:2017-02-25 05:37:18
【问题描述】:
这是我的第一个脚本,所以不要打我!
我正在编写一个基于用户输入创建网络目录和 AD 组的脚本。以下是我到目前为止所得到的。它可以工作,但我想进行一些改进。
我想验证用户输入的长度。我找到了一篇文章 (PowerShell ValidateLength with Read-Host),它解释了如何使用 ValidateLength 字符串来检查用户的输入。这很好用,但是,我想将它包含在一个循环中。例如,如果用户没有准确输入 X 个字符 - 然后重试。现在它只是出错了。
任何帮助将不胜感激!
[ValidateLength(2,2)]$Division = [string](Read-Host -Prompt 'Please enter the TWO digit division number ')
[ValidateLength(4,4)]$Matter = [string](Read-Host -Prompt 'Please enter the FOUR digit matter number ')
[ValidateLength(4,4)]$Client = [string](Read-Host -Prompt 'Please enter the FOUR digit client number ')
【问题讨论】:
-
如果您希望用户输入数字,为什么要验证转换为字符串的值的长度而不是转换为 int 的值的 validating the range?
标签: powershell