powershell如何设置字符限制不超过15个字符,超过则提示重新输入名字
powershell how to set a character limit of no more than 15 characters, if more then prompt to re-enter the name
如何为以下代码设置字符限制?
我需要在这里检查长度的 IF 语句吗?
do { $VM = Read-Host "Enter Server Name" }
until ($VM)
我试过了
do { [string][validatelength(0,15)] $VM = Read-Host "Enter Server Name" }
until ($VM)
如果输入文本不超过 15,这很好...如果超过它会出错
我需要它循环回来所以它要求输入正确的名称
我该如何调整它?
谢谢
一直循环直到输入字符串满足你的约束:
do {
$VM = Read-Host "Enter Server Name"
} while($VM.Length -gt 15)
您可以执行以下操作:
$VM = "anything over 15"
while ($VM -match ".{15}" ){
$VM = Read-Host "Enter Server Name"
}
我在这个 post post
中找到了有关检查长度的信息
以及 here's 微软关于 while 循环的文档。
即使是 if 条件也可以。
$VM = "qwertyuiop"
if ($VM.length -gt 15) {
Write-Output "Please enter more than 15 characters."
$VM = Read-Host "Re-enter VM Name"
}
$VM
如何为以下代码设置字符限制? 我需要在这里检查长度的 IF 语句吗?
do { $VM = Read-Host "Enter Server Name" }
until ($VM)
我试过了
do { [string][validatelength(0,15)] $VM = Read-Host "Enter Server Name" }
until ($VM)
如果输入文本不超过 15,这很好...如果超过它会出错 我需要它循环回来所以它要求输入正确的名称
我该如何调整它?
谢谢
一直循环直到输入字符串满足你的约束:
do {
$VM = Read-Host "Enter Server Name"
} while($VM.Length -gt 15)
您可以执行以下操作:
$VM = "anything over 15"
while ($VM -match ".{15}" ){
$VM = Read-Host "Enter Server Name"
}
我在这个 post post
中找到了有关检查长度的信息以及 here's 微软关于 while 循环的文档。
即使是 if 条件也可以。
$VM = "qwertyuiop"
if ($VM.length -gt 15) {
Write-Output "Please enter more than 15 characters."
$VM = Read-Host "Re-enter VM Name"
}
$VM