在powershell中调用函数然后如果函数再次调用自身如何重置函数的变量
calling a function in powershell then how to reset a function's variable if function calls again itself
我的函数进行测试,如果为真,则再次调用函数
但在这种情况下,我的变量 $label 不为空。
我需要重置它,但不知道该怎么做
我在想 $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 '^[\d\p{L}]{1,11}$')
return $label
}
用于输入验证的正则表达式模式如下:
^[\d\p{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
我的函数进行测试,如果为真,则再次调用函数
但在这种情况下,我的变量 $label 不为空。
我需要重置它,但不知道该怎么做
我在想 $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 '^[\d\p{L}]{1,11}$')
return $label
}
用于输入验证的正则表达式模式如下:
^[\d\p{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