PowerShell不再从文本框读取输入



上次我运行这个脚本是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 version 4.0

最后一次运行这个脚本是在2年前

在PowerShell 3.0版本中,由事件处理程序(如

)调用的委托的作用域
$OKButton.Add_Click({$userInput=$objTextBox.Text;$objForm.Close()})

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

修复方法是显式地声明一个父作用域,如下所示:
$OKButton.Add_Click({$Script:userInput=$objTextBox.Text;$objForm.Close()})

同样的行为导致TechNet的例子不能在PowerShell 4.0中正常运行-除非你用$Script:x$Global:x替换所有内联提到的$x

你可以在about_Scopes帮助文件中阅读更多关于PowerShell中的动态作用域模型(以及在子作用域中初始化的变量如何最终隐藏在父作用域中共享其名称的变量):

Get-Help about_Scopes -Full

我从来没有做过任何形式之前,但使$userInput分配到$objTextBox。ShowDialog()之后的文本为我工作:

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

相关内容

  • 没有找到相关文章

最新更新