我正在尋找一種更好的方法來驗證用戶對我的腳本的「響應」。我知道你可以使用一個函數來驗證這一點,但我希望它更具互動性。更好的方法來驗證不使用函數ValidateSet
$DPString = @"
Enter users department.
Valid choices are;
"Accounts","Claims","Broker Services","Underwriting","Compliance","HR","IT","Developmet","Legal" and "Legal Underwriting"
"@
$Department = Read-Host "$DPString"
do
{
Switch ($Department)
{
Accounts { $DepBool = $true }
Claims { $DepBool = $true }
"Broker Services" { $DepBool = $true }
Underwriting { $DepBool = $true }
Compliance { $DepBool = $true }
"Legal Underwriting" { $DepBool = $true }
Legal { $DepBool = $true }
HR { $DepBool = $true }
IT { $DepBool = $true }
Development { $DepBool = $true }
Default { $DepBool = $false }
}
if ($DepBool -eq $true)
{
$DepLoop = $false
}
else {
$Department = Read-Host "Please enter a valid Department"
$DepLoop = $true
}
}
while ($DepLoop)
大衛你好,感謝您的答覆,我使用這個在一個更大的腳本來捕捉信息和寧願保持在cli。有了參數集,它可以提供更好的方法來驗證,但當命令無效時會跳出命令,我需要一些能夠立即反饋並給予用戶機會來糾正錯誤的方法,這是合理的。 –
在這種情況下,我會使用第二種方法,這使得他們的下拉列表不會出錯。 –
儘管該選項非常好,但是您認爲只有在CLI中有更好的方式才能完成此操作,並且沒有輸入框? –