我的功能进行测试,如果是正确的调用,则函数
但是在这种情况下,我的变量$标签不是空的。
我需要重置它,不知道该怎么做
我在想$ label ="可以工作,但不能重置它。
在这里我的代码
Function name-label
{
# Incorporate Visual Basic into script
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
# show the Pop Up with the following text.
$label = [Microsoft.VisualBasic.Interaction]::InputBox("Pas plus de 11 caractères`r`n`
1) Que des lettres ou des chifres`
2) Pas de caractères bizarres comme $ * µ %"`
, "Nom de la clef USB", "")
# si plus de 11 caractères
if ($label.length -gt 12)
{
Write-Host "Vous avez mis plus de 11 caractères : $label" -ForegroundColor Red -BackgroundColor Black
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Vous avez tapé ce nom $label qui est trop long pour la clé USB`r `n Pas plus de 11 caractères`r `n Nouvel essai !",0,"ERREUR !",0)
name-label # Restart function
}
}
递归调用Name-Label
是完全不必要的,您可以使用简单的do{}until()
循环进行此操作:
Function Name-Label
{
# Import Visual Basic into script
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$firstRun = $true
# show the Pop Up with the following text.
do{
if(-not $firstRun){
# loop running again, must have failed input validation
$null = (New-Object -ComObject WScript.Shell).Popup("Vous avez tapé ce nom $label qui est trop long pour la clé USB`r `n Pas plus de 11 caractères`r `n Nouvel essai !",0,"ERREUR !",0)
}
$firstRun = $false
# prompt user for label
$label = [Microsoft.VisualBasic.Interaction]::InputBox("Pas plus de 11 caractères`r`n`
1) Que des lettres ou des chifres`
2) Pas de caractères bizarres comme $ * µ %",
"Nom de la clef USB", "")
} until ($label -match '^[dp{L}]{1,11}$')
return $label
}
用于输入验证的正则方式如下:
^[dp{L}]{1,11}$
^ # start of string
[
d # digits
p{L} # Letters
]
{1,11} # between 1 and 11 of the previous character class
$ # end of string