2014-06-06 22 views
0

我正在尋找一種更好的方法來驗證用戶對我的腳本的「響應」。我知道你可以使用一個函數來驗證這一點,但我希望它更具互動性。更好的方法來驗證不使用函數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) 

回答

0

這不是在提示用戶什麼情況下輸入清楚,但我贊成傳遞的有效參數列表在命令行上。我會讓他們互斥使用Parameter Sets

但是,如果這是一個較大的腳本的一部分,它會提示輸入部分路徑,那麼使用windows API提示輸入以顯示輸入框可能是適當的。以下鏈接更詳細地描述了這種方法Creating a Custom Input Box

儘管從powershell顯示UI感覺不對,但我知道有些時候這是可取的,使用上面的鏈接來實現一個ListBox,只需將它傳遞給一個字符串數組並返回選定的值:

<# 
.SYNOPSIS 
    Displays a Windows List Control and returns the selected item 
.EXAMPLE 
    Get-ListBoxChoice -Title "Select Environment" -Prompt "Choose an environment" -Options @("Option 1","Option 2") 
    This command displays a list box containing two options. 
.NOTES 
    There are two command buttons OK and Cancel, selecting OK will return the selected option, whilst 
    Cancel will return nothing. 
.RELATED LINKS 
    http://technet.microsoft.com/en-us/library/ff730941.aspx 
#> 
Function Get-ListBoxChoice 
{ 
    [cmdletbinding()] 
    param 
    (
     [Parameter(Mandatory=$true)] 
     [string] $Title, 

     [Parameter(Mandatory=$true)] 
     [string] $Prompt, 

     [Parameter(Mandatory=$true)] 
     [string[]] $Options 
    ) 

    Write-Verbose "Get-ListBoxChoice" 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

    $uiForm = New-Object System.Windows.Forms.Form 
    $uiForm.Text = $Title 
    $uiForm.FormBorderStyle = 'Fixed3D' 
    $uiForm.MaximizeBox = $false 
    $uiForm.Size = New-Object System.Drawing.Size(300,240) 
    $uiForm.StartPosition = "CenterScreen" 

    $uiForm.KeyPreview = $True 
    $uiForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
     {$chosenValue=$objListBox.SelectedItem;$uiForm.Close()}}) 
    $uiForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
     {$uiForm.Close()}}) 

    $OKButton = New-Object System.Windows.Forms.Button 
    $OKButton.Location = New-Object System.Drawing.Size(75,160) 
    $OKButton.Size = New-Object System.Drawing.Size(75,23) 
    $OKButton.Text = "OK" 
    $OKButton.Add_Click({$chosenValue=$objListBox.SelectedItem;$uiForm.Close()}) 
    $uiForm.Controls.Add($OKButton) 

    $CancelButton = New-Object System.Windows.Forms.Button 
    $CancelButton.Location = New-Object System.Drawing.Size(150,160) 
    $CancelButton.Size = New-Object System.Drawing.Size(75,23) 
    $CancelButton.Text = "Cancel" 
    $CancelButton.Add_Click({$uiForm.Close()}) 
    $uiForm.Controls.Add($CancelButton) 

    $uiLabel = New-Object System.Windows.Forms.Label 
    $uiLabel.Location = New-Object System.Drawing.Size(10,20) 
    $uiLabel.Size = New-Object System.Drawing.Size(280,20) 
    $uiLabel.Text = $Prompt 
    $uiForm.Controls.Add($uiLabel) 

    $objListBox = New-Object System.Windows.Forms.ListBox 
    $objListBox.Location = New-Object System.Drawing.Size(10,40) 
    $objListBox.Size = New-Object System.Drawing.Size(260,20) 
    $objListBox.Height = 120 

    $Options | % { 
     [void] $objListBox.Items.Add($_) 
    } 

    $uiForm.Controls.Add($objListBox) 
    $uiForm.Topmost = $True 

    $uiForm.Add_Shown({$uiForm.Activate()}) 
    [void] $uiForm.ShowDialog() 

    $chosenValue 
} 
+0

大衛你好,感謝您的答覆,我使用這個在一個更大的腳本來捕捉信息和寧願保持在cli。有了參數集,它可以提供更好的方法來驗證,但當命令無效時會跳出命令,我需要一些能夠立即反饋並給予用戶機會來糾正錯誤的方法,這是合理的。 –

+0

在這種情況下,我會使用第二種方法,這使得他們的下拉列表不會出錯。 –

+0

儘管該選項非常好,但是您認爲只有在CLI中有更好的方式才能完成此操作,並且沒有輸入框? –

0

如果他們至少運行V3,你可以使用出的GridView:

$Departments = @(
[PSCustomObject]@{Name = 'Accounts';Description = 'Accounting Department'} 
[PSCustomObject]@{Name = 'Claims';Description = 'Claims Department'} 
[PSCustomObject]@{Name = 'Broker';Description = 'Broker Services'} 
) 


$GridParams = @{ 
Title = "Select a department, and press 'OK', or 'Cancel' to quit." 
OutPutMode = 'Single' 
} 

$Department = $Departments | Out-Gridview @GridParams 

If ($Department) 
{ #Do stuff } 
相關問題