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

标签 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

最佳答案

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

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

关于function - PowerShell不再从文本框中读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30379946/

相关文章:

javascript - 如何解决php中图片不显示的问题?

python - 计算机生成一个数字,用户猜测数字,不管怎么猜总是不对

r - 将数据帧的名称传递到 r 中的循环中

c++ - undefined reference ;类公共(public)函数在链接时不可访问

powershell - 从Powershell POST时如何解析 “Content”输出?

Powershell - 验证 AD 中是否存在对象

Javascript:获取标签中具有特定 "for"的单选按钮标签

wcf - Powershell:使用 MTOM 消息编码使用 WCF 服务时出错

c++ - 一个函数可以有一个像 (x | y) 这样的参数吗?它是如何工作的?

JQuery onclick 函数聚焦输入