我有一个脚本需要用户在文本框中输入。用户点击进入或单击“确定”后,必须将文本框中的数据传输到命令。这是代码的文本框部分。我只是几个月的PS。随意批评:)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form;
$objForm.Text = "Enter Patch Number";
$objForm.Size = New-Object System.Drawing.Size(100,100);
$objForm.StartPosition = "CenterScreen";
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(20,50)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$Form.Controls.Add($InputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,30)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Send to Patch Command"
$Button.Add_Click({$version})
$Form.Controls.Add($Button)
$wks=$InputBox.text
.\anp deploypatch -patch=$version
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
这是必须将用户输入传送到最后的命令。正确执行此cmd的一个示例是...... 。\ anp deploypatch -patch = 437.1237
换句话说,用户在上面以粗体键入项目并点击输入,并根据用户输入的版本执行命令。我想要的主要是如何将用户输入到我想要的命令中。
答案 0 :(得分:1)
您可以尝试以下代码。在原始代码中$form
不存在,控件位置是随机的,并且回车键未链接到按钮。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form;
$objForm.Text = "Enter Patch Number";
$objForm.Size = New-Object System.Drawing.Size(200,200);
$objForm.StartPosition = "CenterScreen";
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Point(20,20)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$objForm.Controls.Add($InputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Point(50,50)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Send to Patch Command"
$Button.Add_Click({$objForm.Close()})
$objForm.AcceptButton = $Button # the button is linked to the enter key
$objForm.Controls.Add($Button)
$wks=$InputBox.text
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$version = $InputBox.Text
#$version
.\anp deploypatch -patch=$version
答案 1 :(得分:0)
以下似乎可以解决这个问题让我知道你是怎么过的。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form;
$Form.Text = "Enter Patch Number";
$Form.Size = New-Object System.Drawing.Size(500,200);
$Form.StartPosition = "CenterScreen";
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(20,50)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$InputBox.text = ""
$Form.Controls.Add($InputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,30)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Send to Patch Command"
$Button.Add_Click({patch})
$Form.Controls.Add($Button)
Function patch {
$wks=$InputBox.text
Invoke-Expression "\anp deploypatch -patch='$wks'"
}
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()