2016-11-04 60 views
0

我正在编写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 
+0

这是(主要)作用域问题,用'$ global替换'$ x = $ objListBox.SelectedItem' :x = $ objListBox.SelectedItem' –

回答

0

当前脚本有两个问题。

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

可以工作围绕这通过指定父范围,像(注意global:范围限定):

$global:x = $objListBox.SelectedItem 

但尽管如此,什么都不会发生,导致我的第二个问题:

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

你要定义的打印机前面,显示对话框之前,并包裹在脚本块的((New-Object...语句,是这样的:

[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属性来查找原始打印机对象并调用将其设置为默认值的scriptblock

+0

所以当我测试更新的代码。我得到了屏幕上的列表框,它是空白的,然后我得到了以下错误 新对象:无法将'System.Object []'转换为参数'Property'所需的'System.Collections.IDictionary'类型。指定的方法不受支持。 在C:\ Users \ Documents \ PowerShell_Scripts \ Production_Scripts \ Install PS3_V2.ps1上的打印机:6个字符:35 + New-Object psobject -Property { +〜 + CategoryInfo:InvalidArgument:(:) [New-Object] ,ParameterBindingException + FullyQualifiedErrorId: –

+0

不是'-Property {...'但是 - 属性@ {...'(注意'@') –

+0

好吧,我有这个: New-Object psobject -Property @ {at第6行,是否报告错误。 $打印机= @( 新物体psobject,物业@ { NAME = “于HP Color LaserJet CP2020” set命令= {((新物体-ComObject WScript.Network).SetDefaultPrinter('\\ PS \ PS3 '))} }, –

相关问题