如何在 PowerShell 中测试变量是否超过八个字符? [英] How can I test that a variable is more than eight characters in PowerShell?
本文介绍了如何在 PowerShell 中测试变量是否超过八个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试$dbUserName中的字符数是否超过8个字符?
How do test if the number of characters in $dbUserName is more than eight characters?
我一直无法找到可以让我执行此操作的命令或一系列命令.我只能找到变量是否为空:
I have been unable to locate a command or series of commands that will let me do this. I have only been able to find if the variable is null:
if ($dbUserName) {
Write-Output " You left Username blank"
$dbUserName = read-host
}
但我想像这样进行下一个测试:
But I would like to next test like this:
if ($dbUserName [String] > 8 ) }
Write-Output " Please enter more than 8 characters "
$dbUserName=read-host " Re-enter database user name"
}
推荐答案
使用[String]
类型的length
属性:
if ($dbUserName.length -gt 8) {
Write-Output "Please enter more than 8 characters."
$dbUserName = Read-Host "Re-enter database username"
}
请注意,您必须在 if
条件中使用 -gt
而不是 >
.PowerShell 使用以下比较运算符来比较值和测试条件:
Please note that you have to use -gt
instead of >
in your if
condition. PowerShell uses the following comparison operators to compare values and test conditions:
- -eq = 等于
- -ne = 不等于
- -lt = 小于
- -gt = 大于
- -le = 小于或等于
- -ge = 大于或等于
这篇关于如何在 PowerShell 中测试变量是否超过八个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文