更好的方法来验证何时不使用函数ValidateSet

时间:2014-06-06 10:43:51

标签: powershell

我正在寻找一种更好的方法来验证用户对我的脚本的“响应”。我知道你可以使用一个函数来验证这个,但我希望它更具互动性。

$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)

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

如果他们至少运行V3,您可以使用Out-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 }