如何编写命令按钮以在powershell中使用字符串

时间:2016-11-04 12:31:43

标签: powershell

我正在编写一个Powershell脚本,以自动在我们的网络上安装打印机。我已经过度思考了,但我似乎无法通过命令按钮让用户从列表中选择打印机并将其设置为默认值。

我有一个字符串设置来定义打印机(其中4个),但无论我用什么方式编写$ OKButton.Add_Click,它都不会与用户选择一起使用。

这是我的代码。有人可以告诉我我的想法吗?

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Printer"
$objForm.Size = New-Object System.Drawing.Size(400,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

#Ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$strPrinter,$objForm.Close()})
$objForm.Controls.Add($OKButton)

#Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please select a printer:"
$objForm.Controls.Add($objLabel) 

#List box showing printer options
$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(360,20) 
$objListBox.Height = 80

[void] $objListBox.Items.Add("HP Color LaserJet CP2020")
[void] $objListBox.Items.Add("Brother DCP-8065DN")
[void] $objListBox.Items.Add("Canon iR-ADV C2220/2230")
[void] $objListBox.Items.Add("HP LJ300-400 color M351-M451")

$objForm.Controls.Add($objListBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

#String to call printers, each printer is assigned a value (1,2,3,4)
$strPrinter = 1, "HP Color LaserJet CP2020", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT01')) 
$strPrinter = 2, "Brother DCP-8065DN", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT02')) 
$strPrinter = 3, "Canon iR-ADV C2220/2230", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT03'))
$strPrinter = 4, "HP LJ300-400 color M351-M451", ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\PS\PT04'))

$x

1 个答案:

答案 0 :(得分:0)

您当前的脚本存在两个问题。

第一个,$x显示为空是由于范围问题。当在add_Click()事件处理程序的范围内时,$x是一个局部变量,并且它的值不会在事件处理程序之外访问。

可以通过指定父范围来解决这个问题,例如(注意global:范围限定符):

$global:x = $objListBox.SelectedItem

但是,没有任何事情会发生,导致我进入第二个问题:

我不确定你的意思是"字符串设置",但是你的脚本基本上最终会在运行时将最后一台打印机设置为默认值。

您需要在显示对话框之前预先定义打印机,并将((New-Object...语句包装在scriptblock中,如:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

# New array of "printer objects", rather than $strPrinter
$Printers = @(
    New-Object psobject -Property @{
        Name = "HP Color LaserJet CP2020"
        SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT01')) }
    },
    New-Object psobject -Property @{
        Name = "Brother DCP-8065DN"
        SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT02')) }
    },
    New-Object psobject -Property @{ 
        Name = "Canon iR-ADV C2220/2230"
        SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\\PS\PT03')) }
    },
    New-Object psobject -Property @{
        Name = "HP LJ300-400 color M351-M451"
        SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter('\PS\PT04')) }
    }
)

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Printer"
$objForm.Size = New-Object System.Drawing.Size(400,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

#Ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({
    # Grab the printer array index
    $index = $objListBox.SelectedIndex

    # Execute the appropriate command
    & $Printers[$index].SetCommand 

    # Exit
    $objForm.Close()
})
$objForm.Controls.Add($OKButton)

#Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please select a printer:"
$objForm.Controls.Add($objLabel) 

#List box showing printer options
$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(360,20) 
$objListBox.Height = 80

foreach($Printer in $Printers){
    [void] $objListBox.Items.Add($Printer.Name)
}

$objForm.Controls.Add($objListBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

由于打印机现在按正确顺序添加到列表框中,我们只需使用SelectedIndex属性查找原始打印机对象并调用将其设置为默认值的脚本块