2016-11-04 1 views
0

ネットワーク上のプリンタのインストールを自動化するPowershellスクリプトを作成しています。しかし、私はあなたのリストからプリンタを選択し、デフォルトとしてそれを設定するユーザーを許可する私のコマンドボタンを得ることができない場所でoverthinkingしている。コマンドボタンをプログラムして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

これは主にスコープの問題です。 '$ x = $ objListBox.SelectedItem'を' $ global :x = $ objListBox.SelectedItem' –

答えて

0

現在のスクリプトには2つの問題があります。

最初のものは、$xが空に見えるのは、スコープの問題によるものです。 イベントハンドラのスコープ内にある場合、$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プロパティを使用して元のプリンタオブジェクトを見つけてデフォルトとして設定するスクリプトブロックを呼び出すことができます

+0

私は更新されたコードをテストしていました。私は画面上にリストボックスを持っていましたが、空白だったので、以下のエラーが表示されました 新しいオブジェクト: 'System.Object []'をパラメータ 'Property'で必要な 'System.Collections.IDictionary'に変換できません。指定されたメソッドはサポートされていません。 C:\ Users \ Documents \ PowerShell_Scripts \ Production_Scripts \ PS3_V2.ps1のプリンタをインストールします。6文字:35 +新規オブジェクトpsobject - プロパティ{ +〜 +カテゴリ情報:InvalidArgument :(:) [New-Object] 、ParameterBindingException + FullyQualifiedErrorId: –

+0

'-Property {...'ではなく '-Property @ {(' @に気付く) –

+0

ありがとう、私はこれを持っています: New-Object psobject -Property @ {at 6行目に、エラーを報告しました。 $プリンター= @( 新オブジェクトpsobject -Property @ { 名= "HPの色のLaserJet CP2020" SetCommand = {((新オブジェクト-ComObject WScript.Network).SetDefaultPrinter( '\\ PS \ PS3 '))}} } –

関連する問題