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

Function PowerShell不再从文本框读取输入,function,powershell,input,Function,Powershell,Input,我上次运行这个脚本是在两年前,我想可能我没有将$userInput传输回主程序,但即使是函数也不读取$userInput 如何排除故障 function getValues($formTitle, $textTitle){ [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartial

我上次运行这个脚本是在两年前,我想可能我没有将$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
使现代化 当我尝试从中编写代码并添加写入主机$x时,没有输出


PowerShell 4.0版

我以前从未对表单做过任何操作,但在ShowDialog生效后将$userInput分配给$objTextBox.Text:

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

我以前从未对表单做过任何操作,但在ShowDialog之后将$userInput分配给$objTextBox.Text对我有效:

[void] $objForm.ShowDialog()
$userInput = $objTextBox.Text
write-host user Input is $userInput
我上次运行这个脚本是两年前

自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()})
这与导致在PowerShell 4.0中无法正确运行的行为相同-除非您将所有内联提到的$x替换为$Script:x或$Global:x

您可以阅读有关PowerShell中动态作用域模型的更多信息,以及在子作用域中初始化的变量如何在帮助文件中隐藏与父作用域同名的变量:

Get-Help about_Scopes -Full
我上次运行这个脚本是两年前

自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()})
这与导致在PowerShell 4.0中无法正确运行的行为相同-除非您将所有内联提到的$x替换为$Script:x或$Global:x

您可以阅读有关PowerShell中动态作用域模型的更多信息,以及在子作用域中初始化的变量如何在帮助文件中隐藏与父作用域同名的变量:

Get-Help about_Scopes -Full

尝试写入主机用户输入$userInput@Erti-克丽塞尔玛运气不好。更糟糕的是,我正在尝试从中编写代码,当我添加write host$x时,什么都不是输出,write host用户输入就是输出$userInput@Erti-克丽塞尔玛运气不好。更糟糕的是,我正在尝试从中编写代码,当我添加write host$x时,没有任何输出是有效的,但我想知道,为什么以前的代码不起作用。甚至微软官方网站上的脚本也不起作用???我有根据地猜测,自从Powershell 1.0打破了他们的示例脚本以来,有些事情发生了变化。@TonyHinkle是的,他已经被确定了范围@谢谢你花时间给我们关于示波器的独家报道。我怀疑是这样的,但我对Powershell了解不够,不知道如何使用$Script:variable技巧。我以前使用过globals,但这是我今天学到的新方法。我很高兴:我认为最正确的方法是从事件委托访问直接父作用域,比如:Set Variable-Name userInput-scope 1-Value$objTextBox.Text,但它很快就会变得不可读。我认为脚本范围是OP描述它工作的合理边界,但我想知道,为什么以前的代码不工作。甚至微软官方网站上的脚本也不起作用???我有根据地猜测,自从Powershell 1.0打破了他们的示例脚本以来,有些事情发生了变化。@TonyHinkle是的,他已经被确定了范围@谢谢你花时间给我们关于示波器的独家报道。我怀疑是这样的,但我对Powershell了解不够,不知道如何使用$Script:variable技巧。我以前使用过globals,但这是我今天学到的新方法。我很高兴:我认为最正确的方法是从事件委托访问直接父作用域,比如:Set Variable-Name userInput-scope 1-Value$objTextBox.Text,但它很快就会变得不可读。我认为脚本范围是OP描述的一个合理边界是的。我相信这种行为的改变实际上是一种修复,TechNet杂志上原始WinForm代码片段的作者可能对他们的酷工作示例过于兴奋,以至于无法检查他们是否在PowerShell 1.0中滥用了某些东西是的。我相信这种行为上的改变实际上是一种修复,TechNet杂志上原始WinForm代码片段的作者可能对他们的酷工作示例过于兴奋,以至于无法检查他们是否滥用了PowerShell 1.0中的某些内容