0

我有一个脚本需要用户在文本框中输入。一旦用户按回车键或单击确定,文本框中的数据必须传输到命令。这是代码的文本框部分。我刚学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

换句话说,用户在上面的粗体项目中键入并按回车键,然后根据用户输入的版本执行命令。我在这里想要的主要是如何将用户输入到我想要的命令中。

4

2 回答 2

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
于 2013-06-17T20:49:57.090 回答
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()
于 2015-01-02T20:34:42.983 回答