PowerShell不再从文本框中读取输入

时间:2015-05-21 16:56:37

标签: function powershell input

上次我运行这个脚本是在2年前,我想也许我没有将$userInput转回主程序,但即使是函数也没有读$userInput

如何解决问题?

function getValues($formTitle, $textTitle){


    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 


    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = $formTitle
    $objForm.Size = New-Object System.Drawing.Size(300,200)
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$userInput=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $CANCELButton = New-Object System.Windows.Forms.Button
    $CANCELButton.Location = New-Object System.Drawing.Size(150,120)
    $CANCELButton.Size = New-Object System.Drawing.Size(75,23)
    $CANCELButton.Text = "CANCEL"
    $CANCELButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CANCELButton)

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20)
    $objLabel.Size = New-Object System.Drawing.Size(280,30)
    $objLabel.Text = $textTitle
    $objForm.Controls.Add($objLabel)

    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,50)
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objForm.Controls.Add($objTextBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})

    [void] $objForm.ShowDialog()

    write-host user Input is $userInput
    return $userInput


}


$schema = getValues "Database Schema" "Enter database schema"
$db_IP = getValues "Database Server" "Enter IP address where database is located"
$init_cat = getValues "Database Name" "Enter database name "
$userID = getValues "Database Administrator Username" "Enter username of database administrator"

输出

user Input is
user Input is
user Input is
user Input is

更新

当我尝试https://technet.microsoft.com/en-us/library/ff730941.aspx中的代码并添加write-host $x时,没有输出

PowerShell 4.0版

2 个答案:

答案 0 :(得分:3)

  

上次我运行此脚本的时间是2年前

从PowerShell 3.0版开始,

等事件处理程序调用的委托的范围
$OKButton.Add_Click({$userInput=$objTextBox.Text;$objForm.Close()})

与包含脚本不同,这意味着当您在$userInput脚本块中为Add_Click分配内容时,您实际上会将某些内容分配给该脚本块本地的变量。< / p>

修复方法是明确说明父范围,如下所示:

$OKButton.Add_Click({$Script:userInput=$objTextBox.Text;$objForm.Close()})

导致TechNet example无法在PowerShell 4.0中正常运行的行为相同 - 除非您使用$x或{{1}替换$Script:x的所有内联提及}

您可以在about_Scopes帮助文件中阅读有关PowerShell中动态范围模型的更多信息(以及在子范围中初始化的变量如何最终隐藏在父范围内共享其名称的变量):

$Global:x

答案 1 :(得分:1)

我以前从未对表单做任何事情,但在ShowDialog()为我工作后将$ userInput赋值给$ objTextBox.Text:

[void] $objForm.ShowDialog()
$userInput = $objTextBox.Text
write-host user Input is $userInput