【发布时间】:2019-06-27 15:40:25
【问题描述】:
我有一个 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 上输入的值
显然,由于 GUI 存在于 powershell 之外,因此一旦运行,它就不会出现错误。
【问题讨论】:
标签: powershell