我整理了一个Powershell脚本,以提供用于安装打印机的GUI。我似乎在将变量从屏幕上的文本框传递到添加打印机功能并将它们绑定到按钮时遇到问题。
我尝试了Set函数和=函数的几种组合来尝试连接不同的变量,但没有成功。我也看不到错误,因为在GUI加载时它不显示任何错误消息。
Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='Printer Installation'
$main_form.Width = 600
$main_form.Height = 400
$main_form.AutoSize = $true
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "What is the Printer IP Address?"
$Label.Location = New-Object System.Drawing.Point(0,10)
$Label.AutoSize = $true
$PrinterIPGUI = New-Object System.Windows.Forms.TextBox
$PrinterIPGUI.Location = New-Object System.Drawing.Point(200,10)
$PrinterIPGUI.AutoSize = $true
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "What do you want to name the printer?"
$Label2.Location = New-Object System.Drawing.Point(0,40)
$Label2.AutoSize = $true
$PrinterNameGUI = New-Object System.Windows.Forms.TextBox
$PrinterNameGUI.Location = New-Object System.Drawing.Point(200,40)
$PrinterNameGUI.AutoSize = $true
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(0,60)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Run"
$main_form.Controls.Add($Label2)
$main_form.Controls.Add($PrinterNameGUI)
$main_form.Controls.Add($PrinterIPGUI)
$main_form.Controls.Add($Button)
$main_form.Controls.Add($Label)
$main_form.ShowDialog()
function Set-ActionOnClick{
add-printerport -name $PrinterIPGUI -printerhostaddress $PrinterIPGUI
add-printer -name $PrinterNameGUI -drivername "HP Universal Printing PCL
6 (v6.7.0)" -port $PrinterIPGUI
}
$Button.Add_Click(
{
Set-ActionOnClick
}
)
我希望问题出自两件事:
为允许输入文本而创建的对象无法与在文本字段上输入的值相关。
因为该函数无法解释在GUI上输入的值
很显然,由于powershell运行后GUI已存在,因此不会出现任何错误。
答案 0 :(得分:0)
没关系,我在函数方法下进行了处理,然后通过管道将这些字段的变量通过管道传输出来了。