Switch语句产生错误的结果

时间:2015-07-13 12:26:54

标签: winforms function powershell return

我为我们的支持部门和实习生创建了一个脚本。我有一个工作脚本,但想让我们的实习生使用GUI更容易一些。此刻我现在把头发拉了出来,我已经复制了下面的功能,并希望得到$Result的价值。但是,当我尝试将返回$Task1放入变量时,$Task1_exec变量不会打印$FormTitle = "Formtitle" $ScriptPath = $(get-location).Path; $task1 = "Task 1" $task1_exec = "Perform task 1" $task2 = "Task 2" $task2_exec = "Perform task 2" $task3 = "Task 3" $task3_exec = "Perform task 3" $task4 = "Task 4" $task4_exec = "Perform task 4" function Prompt-SelectionList { Param( [Parameter(Position=0,Mandatory=$true)][string]$Description, [Parameter(Position=1,Mandatory=$true)][string[]]$List ) [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $Form = New-Object System.Windows.Forms.Form $Form.Text = $FormTitle $Form.Size = New-Object System.Drawing.Size(500,450) #(300,200) $Form.StartPosition = "CenterScreen" $btnOK = New-Object System.Windows.Forms.Button $btnOK.Location = New-Object System.Drawing.Size(125,375) $btnOK.Size = New-Object System.Drawing.Size(125,25) $btnOK.Text = "Run..." $Form.Controls.Add($btnOK) $btnCancel = New-Object System.Windows.Forms.Button $btnCancel.Location = New-Object System.Drawing.Size(250,375) $btnCancel.Size = New-Object System.Drawing.Size(125,25) $btnCancel.Text = "Exit" $btnCancel.Add_Click({ $script:Result = $null; $Form.Close() }) $Form.Controls.Add($btnCancel) $Label = New-Object System.Windows.Forms.Label $Label.Location = New-Object System.Drawing.Size(10,20) $Label.Size = New-Object System.Drawing.Size(480,20) $Label.Text = $Description $Form.Controls.Add($Label) $ListBox = New-Object System.Windows.Forms.ListBox $ListBox.Location = New-Object System.Drawing.Size(10,40) $ListBox.Size = New-Object System.Drawing.Size(460,330) $List | ForEach-Object { [void] $ListBox.Items.Add($_) } $Form.Controls.Add($ListBox) $Form.KeyPreview = $True $Form.Add_KeyDown({ if ($_.KeyCode -eq "Enter") { $script:Result = $ListBox.SelectedItem; $Form.Close() }}) $Form.Add_KeyDown({ if ($_.KeyCode -eq "Escape") { $script:Result = $null; $Form.Close() }}) $btnOK.Add_Click({ $script:Result = $ListBox.SelectedItem; $Form.Close() }) $Form.Topmost = $True $Form.Add_Shown({ $Form.Activate() }) [void] $Form.ShowDialog() return $Result } function Do-PromptTask { switch -wildcard (Prompt-SelectionList "Chose task to run:" @($task1,$task2,$task3,$task4)) { $task1{$task1_exec} $task2{$task2_exec} $Task3{$task3_exec} $Task4{$task4_exec} ""{Exit} #End Selection (cancel selected) # "*"{Do-PromptTask} #Keep prompting until "cancel" selected } } Do-PromptTask 变量。

$task1_exec

我的愿望是我可以将Do-PromptTask返回给变量,这样当. ".\Bin\$Result.ps1" 完成时,返回可用于调用另一个脚本

flock

1 个答案:

答案 0 :(得分:0)

看起来你正走在正确的轨道上。

让我们假装我运行Do-PromptTask,选择任务1,然后点击Run ...

来自Prompt-SelectionList的$ result变量被传递回Switch语句,此时超出范围。该开关正在执行其工作并匹配条件,然后打印$ task1_exec,但此时$ result无用。如果要将$ task1_exec存储为变量,则需要设置与其相等的值。但是,我会说你可以在switch语句中执行它而不用将它存储在变量中,如果这是有意义的话。

少代码方法......

    function Do-PromptTask
{   
    switch -wildcard (Prompt-SelectionList "Chose task to run:" @($task1,$task2,$task3,$task4))
    {
        $task1{Invoke-Expression $task1_exec}
        $task2{Invoke-Expression  $task2_exec}
        $Task3{Invoke-Expression  $task3_exec}
        $Task4{Invoke-Expression  $task4_exec}
        ""{Exit} #End Selection (cancel selected)
        # "*"{Do-PromptTask} #Keep prompting until "cancel" selected
    }
}

如果你设置存储变量,你可以这样做......

    function Do-PromptTask
{   
    switch -wildcard (Prompt-SelectionList "Chose task to run:" @($task1,$task2,$task3,$task4))
    {
        $task1{$task_result = $task1_exec}
        $task2{$task_result = $task2_exec}
        $Task3{$task_result = $task3_exec}
        $Task4{$task_result = $task4_exec}
        ""{Exit} #End Selection (cancel selected)
        # "*"{Do-PromptTask} #Keep prompting until "cancel" selected
    }
    Invoke-Expression $task_result
}