【问题标题】:Attempting to create a powershell GUI to install a printer尝试创建一个 powershell GUI 来安装打印机
【发布时间】: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

}

)

我预计问题来自两件事:

  1. 为允许文本输入而创建的对象无法与文本字段中输入的值相关联。

  2. 因此该函数无法解释在 GUI 上输入的值

显然,由于 GUI 存在于 powershell 之外,因此一旦运行,它就不会出现错误。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    没关系,我在函数方法下处理了这个问题,然后将这些字段的变量通过管道输出,它就可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多