pixel0815
Goto Top

Drucker Powershell GUI - Probleme

Hallo zusammen,

unten stehendes Skript habe ich etwas angepasst, so dass in der GUI noch der Kommentar gefüllt werden kann.
Optisch sieht auch alles soweit ok aus, allerdings funktioniert es nicht so richtig.

In der ports.txt steht unter Printserver zwar das was ich eingetragen habe, allerdings Portname und IPAdress ist nicht gefüllt. Im Debugger stehen dort aber Werte die ich in die Textfelder eingegeben habe.
Hat da jemand ne Idee? face-sad

#printer add script 
#Created from Scripting guys example scripts and my own. 
#Stephen Small 
 
#Clears all variables if you've run the scripts before   
$portname = ""   
$IPaddress = ""   
$driver = ""   
$location = ""   
$comment = ""   
 
#publishes the form 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")    
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")    
 
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Printer Add"   
$objForm.Size = New-Object System.Drawing.Size(500,500)  
$objForm.StartPosition = "CenterScreen"   
 
$objForm.KeyPreview = $True 
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$driver=$objListBox.SelectedItem;$portname=$objTextBox1.Text;$IPaddress=$objTextBox2.Text;$Location=$objTextBox3.Text;$objForm.Close()}})   
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$Cancel=$True;$objForm.Close()}})   
 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(75,400) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK"   
$OKButton.Add_Click({$driver=$objListBox.SelectedItem;$portname=$objTextBox1.Text;$IPaddress=$objTextBox2.Text;$Location=$objTextBox3.Text;$comment=$objTextBox5.Text;$objForm.Close()}) 
$objForm.Controls.Add($OKButton) 
 
$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Size(150,400) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel"   
$CancelButton.Add_Click({$Cancel=$True;$objForm.Close()}) 
$objForm.Controls.Add($CancelButton) 
 
$objLabel1 = New-Object System.Windows.Forms.Label 
$objLabel1.Location = New-Object System.Drawing.Size(10,19)  
$objLabel1.Size = New-Object System.Drawing.Size(280,20)  
$objLabel1.Text = "What is the Printer name?"   
$objForm.Controls.Add($objLabel1)  
 
$objTextBox1 = New-Object System.Windows.Forms.TextBox  
$objTextBox1.Location = New-Object System.Drawing.Size(10,38)  
$objTextBox1.Size = New-Object System.Drawing.Size(260,20)  
$objForm.Controls.Add($objTextBox1) 
 
 
$objLabel2 = New-Object System.Windows.Forms.Label 
$objLabel2.Location = New-Object System.Drawing.Size(10,61)  
$objLabel2.Size = New-Object System.Drawing.Size(280,20)  
$objLabel2.Text = "What is the IP Address?"   
$objForm.Controls.Add($objLabel2)  
 
$objTextBox2 = New-Object System.Windows.Forms.TextBox  
$objTextBox2.Location = New-Object System.Drawing.Size(10,80)  
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)  
$objForm.Controls.Add($objTextBox2) 
 
 
$objLabel3 = New-Object System.Windows.Forms.Label 
$objLabel3.Location = New-Object System.Drawing.Size(10,280)  
$objLabel3.Size = New-Object System.Drawing.Size(280,20)  
$objLabel3.Text = "What is the Location?"   
$objForm.Controls.Add($objLabel3)  

$objListBox = New-Object System.Windows.Forms.ListBox  
$objListBox.Location = New-Object System.Drawing.Size(10,120)  
$objListBox.Size = New-Object System.Drawing.Size(260,20)  
$objListBox.Height = 160 
$objListbox.SelectionMode = "one"   
 
[void] $objListBox.Items.Add("LP 3335_LP 4335")   
[void] $objListBox.Items.Add("Kyocera TASKalfa 5550ci KX")   
[void] $objListBox.Items.Add("Kyocera TASKalfa 3050ci KX")   
[void] $objListBox.Items.Add("Kyocera FS-1128MFP KX")   
[void] $objListBox.Items.Add("Kyocera FS-1135MFP KX")   
[void] $objListBox.Items.Add("Kyocera FS-1370DN KX")   
[void] $objListBox.Items.Add("Kyocera FS-3040MFP+ KX")   
[void] $objListBox.Items.Add("Kyocera FS-C2126MFP+ KX")   
[void] $objListBox.Items.Add("Kyocera FS-C2526MFP KX")   
[void] $objListBox.Items.Add("Kyocera FS-C5250DN KX")   
 
$objListBox.SelectedItem 
 
$objForm.Controls.Add($objListBox) 
 
$objTextBox3 = New-Object System.Windows.Forms.TextBox  
$objTextBox3.Location = New-Object System.Drawing.Size(10,300)  
$objTextBox3.Size = New-Object System.Drawing.Size(260,20)  
$objForm.Controls.Add($objTextBox3) 

$objLabel5 = New-Object System.Windows.Forms.Label 
$objLabel5.Location = New-Object System.Drawing.Size(10,330)  
$objLabel5.Size = New-Object System.Drawing.Size(330,20)  
$objLabel5.Text = "Comment"   
$objForm.Controls.Add($objLabel5)  
 
$objTextBox5 = New-Object System.Windows.Forms.TextBox  
$objTextBox5.Location = New-Object System.Drawing.Size(10,350)  
$objTextBox5.Size = New-Object System.Drawing.Size(280,20)  
$objForm.Controls.Add($objTextBox5) 
 
 
$objForm.Topmost = $True 
 
$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog() 
 
if ($Cancel -eq $True) 
{Exit} 
 
#reset to match the name of your printserver 
$printservername = "prrz001"   
 
#adds the variable content to the text files for readin 
 
Clear-Content c:\printeraddscript\ports.txt 
add-content c:\printeraddscript\ports.txt "Printserver,Portname,IPAddress"   
add-content c:\printeraddscript\ports.txt $printservername","$portname","$IPaddress   
 
Clear-Content c:\printeraddscript\printers.txt 
add-content c:\printeraddscript\printers.txt "Printserver,Driver,PortName,ShareName,Location,Comment,DeviceID"   
add-content c:\printeraddscript\printers.txt $printservername","$driver","$portname","$portname","$location","$IPaddress" - "$driver","$portname   
 
#adds the printer from the text file data 
 
function CreatePrinterPort { 
$server = $args  
$port = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()   
$port.Name = $args[1] 
$port.SNMPEnabled = $true 
$port.SNMPCommunity = "CHDMread"   
$port.Protocol = 1 
$port.Portnumber = "9100"   
$port.HostAddress = $args[2] 
$port.Put()  
} 
 
function CreatePrinter{ 
$server = $args 
$print = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_Printer").createInstance()   
$print.Drivername = $args[1] 
$print.PortName = $args[2] 
$print.Shared = $true 
$print.Published = $true 
$print.Sharename = $args[3] 
$print.Location = $args[4] 
$print.Comment = $args[5] 
$print.DeviceID = $args[6] 
$print.Put() 
} 
 
#Log File 
$printers = Import-Csv c:\printeraddscript\printers.txt 
$ports = Import-Csv c:\printeraddscript\ports.txt 
$filename = "printeradd-{0:d2}-{1:d2}-{2:d2}.log" -f $date.month,$date.day,$date.year   
$filepath = "c:\printeraddscript\"   
 
foreach ($port in $ports){ 
CreatePrinterPort $port.Printserver $port.Portname $port.IPAddress 
} 
foreach ($printer in $printers){ 
CreatePrinter $printer.Printserver $printer.Driver $printer.Portname $printer.Sharename $printer.Location $printer.Comment $printer.DeviceID 
$date = Get-Date; Add-Content -Path $filepath\$filename ("Printer $($portname) was added $($date)")   
} 
 
#Balloon tip on Cmplettion 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")   
 
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon  
 
$objNotifyIcon.Icon = "C:\ProgramData\Microsoft\Device Stage\Task\{e35be42d-f742-4d96-a50a-1775fb1a7a42}\print_queue.ico"   
$objNotifyIcon.BalloonTipIcon = "Info"    
$objNotifyIcon.BalloonTipText = ("Your Printer" , $portname , " has been added to the server.")    
$objNotifyIcon.BalloonTipTitle = "Printer add Complete"   
  
$objNotifyIcon.Visible = $True  
$objNotifyIcon.ShowBalloonTip(20000)    

Content-Key: 255956

Url: https://administrator.de/contentid/255956

Printed on: April 20, 2024 at 01:04 o'clock

Member: pixel0815
pixel0815 Nov 27, 2014 at 08:51:57 (UTC)
Goto Top
Die GUI funktioniert soweit. Jedoch werden die Eingaben der GUI nicht in die Textdateien geschrieben und somit funktioniert das erstellen der Drucker nicht face-sad
Mitglied: 114757
114757 Nov 27, 2014 updated at 10:24:13 (UTC)
Goto Top
Moin,
122. add-content c:\printeraddscript\ports.txt $printservername","$portname","$IPaddress
da sind wohl überall die Anführungszeichen falsch gesetzt !!!!!!!

126. add-content c:\printeraddscript\printers.txt $printservername","$driver","$portname","$portname","$location","$IPaddress" - "$driver","$portname
Hier auch ....

Dat muss so
add-content c:\printeraddscript\printers.txt """$printservername"",""$driver",""$portname"",""$portname"",""$location"",""$IPaddress"",""$driver"",""$portname"""

Btw. CSV Dateien manuell zu erstellen macht man eigentlich nicht, wo es doch die schönen Custom Objects gibt face-smile
$csv = New-Object PSObject -Property @{IPAddress=$ipaddress;Location=$location}
$csv | Export-CSV c:\drucker.csv -delimiter ',' -NoType -Encoding UTF8

Gruß jodel32
Member: pixel0815
pixel0815 Nov 27, 2014 at 10:22:49 (UTC)
Goto Top
Klappt nicht oder ich bin zu blöde face-sad
Member: Pago159
Pago159 Nov 27, 2014 at 11:26:13 (UTC)
Goto Top
Hallo Heiko Wellendorf,

ich persönlich würde das ganze bei mir auch gerne mal Testen, allerdings mit anderen Treibern.
Wo hast du die Pfade zu den Treibern eigentlich hinterlegt?
Ich nutze Treiber aus einer Freigabe, wie kann ich diese einbinden?
Member: pixel0815
pixel0815 Nov 27, 2014 at 12:35:34 (UTC)
Goto Top
Hallo Pago,

das Skript wird auf einen Printserver laufen bzw. nimmt die Treiber die auf dem Printserver installiert sind.
Daher habe ich das Problem gar nicht.

Das Skript kommt von hier:

https://gallery.technet.microsoft.com/scriptcenter/8fb23450-6306-4492-a6 ...

und wäre für meine Zwecke ideal. Aber es funktioniert nicht face-sad
Mitglied: 114757
Solution 114757 Nov 27, 2014, updated at Nov 30, 2014 at 09:01:39 (UTC)
Goto Top
kann ja auch alles nicht laufen ... Du hast den falschen Variable-Scope für deine Variablen : http://technet.microsoft.com/de-de/library/hh847849.aspx

hier der Abschnitt für das Erzeugen der CSV-Dateien (Zeile 1 bis 126 deines Codes).
Aber wieso du extra vorher CSV-Dateien mit den Daten erzeugst verstehe ich nicht, wieso in Dateien schreiben obwohl man die Daten schon im Script hat ??? Kopfschüttel ...
#Clears all variables if you've run the scripts before   
$global:portname = ""   
$global:IPaddress = ""   
$global:driver = ""   
$global:location = ""   
$global:comment = ""   
 
#publishes the form 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")    
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")    


$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Printer Add"   
$objForm.Size = New-Object System.Drawing.Size(500,500)  
$objForm.StartPosition = "CenterScreen"   
 
$ok_click_event = {
        $global:driver=$objListBox.SelectedItem
        $global:portname=$objTextBox1.Text
        $global:IPaddress=$objTextBox2.Text
        $global:Location=$objTextBox3.Text
        $global:comment=$objTextBox5.Text
        $objForm.Close()
}

$key_down_event = {
    if ($_.KeyCode -eq "Enter") {  
        $global:driver=$objListBox.SelectedItem
        $global:portname=$objTextBox1.Text
        $global:IPaddress=$objTextBox2.Text
        $global:Location=$objTextBox3.Text
        $global:comment=$objTextBox5.Text
        $objForm.Close()
    }
}

$objForm.KeyPreview = $True 
$objForm.Add_KeyDown($key_down_event) 
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$Cancel=$True;$objForm.Close()}})   
 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(75,400) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK"   
$OKButton.Add_Click($ok_click_event) 
$objForm.Controls.Add($OKButton) 
 
$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Size(150,400) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel"   
$CancelButton.Add_Click({$Cancel=$True;$objForm.Close()}) 
$objForm.Controls.Add($CancelButton) 
 
$objLabel1 = New-Object System.Windows.Forms.Label 
$objLabel1.Location = New-Object System.Drawing.Size(10,19)  
$objLabel1.Size = New-Object System.Drawing.Size(280,20)  
$objLabel1.Text = "What is the Printer name?"   
$objForm.Controls.Add($objLabel1)  
 
$objTextBox1 = New-Object System.Windows.Forms.TextBox  
$objTextBox1.Location = New-Object System.Drawing.Size(10,38)  
$objTextBox1.Size = New-Object System.Drawing.Size(260,20)  
$objForm.Controls.Add($objTextBox1) 
 
 
$objLabel2 = New-Object System.Windows.Forms.Label 
$objLabel2.Location = New-Object System.Drawing.Size(10,61)  
$objLabel2.Size = New-Object System.Drawing.Size(280,20)  
$objLabel2.Text = "What is the IP Address?"   
$objForm.Controls.Add($objLabel2)  
 
$objTextBox2 = New-Object System.Windows.Forms.TextBox  
$objTextBox2.Location = New-Object System.Drawing.Size(10,80)  
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)  
$objForm.Controls.Add($objTextBox2) 
 
 
$objLabel3 = New-Object System.Windows.Forms.Label 
$objLabel3.Location = New-Object System.Drawing.Size(10,280)  
$objLabel3.Size = New-Object System.Drawing.Size(280,20)  
$objLabel3.Text = "What is the Location?"   
$objForm.Controls.Add($objLabel3)  

$objListBox = New-Object System.Windows.Forms.ListBox  
$objListBox.Location = New-Object System.Drawing.Size(10,120)  
$objListBox.Size = New-Object System.Drawing.Size(260,20)  
$objListBox.Height = 160 
$objListbox.SelectionMode = "one"   
 
$printers = @(
"LP 3335_LP 4335",  
"Kyocera TASKalfa 5550ci KX",  
"Kyocera TASKalfa 3050ci KX",  
"Kyocera FS-1128MFP KX",  
"Kyocera FS-1128MFP KX",  
"Kyocera FS-1135MFP KX",  
"Kyocera FS-1370DN KX",  
"Kyocera FS-3040MFP+ KX",  
"Kyocera FS-C2126MFP+ KX",  
"Kyocera FS-C2526MFP KX",  
"Kyocera FS-C5250DN KX"  
)
$printers | %{[void] $objListBox.Items.Add($_)}
 
$objForm.Controls.Add($objListBox) 
 
$objTextBox3 = New-Object System.Windows.Forms.TextBox  
$objTextBox3.Location = New-Object System.Drawing.Size(10,300)  
$objTextBox3.Size = New-Object System.Drawing.Size(260,20)  
$objForm.Controls.Add($objTextBox3) 

$objLabel5 = New-Object System.Windows.Forms.Label 
$objLabel5.Location = New-Object System.Drawing.Size(10,330)  
$objLabel5.Size = New-Object System.Drawing.Size(330,20)  
$objLabel5.Text = "Comment"   
$objForm.Controls.Add($objLabel5)  
 
$objTextBox5 = New-Object System.Windows.Forms.TextBox  
$objTextBox5.Location = New-Object System.Drawing.Size(10,350)  
$objTextBox5.Size = New-Object System.Drawing.Size(280,20)  
$objForm.Controls.Add($objTextBox5) 

$objForm.Topmost = $True 
 
$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog() 
 
if ($Cancel -eq $True) 
{Exit} 
 
#reset to match the name of your printserver 
$global:printservername = "prrz001"  
 
#adds the variable content to the text files for readin 
New-Object PSObject -Property @{"Printserver"=$global:printservername;"Portname"=$global:portname;"IPAddress"=$global:IPaddress} | select Printserver,Portname,IPAddress | export-csv 'c:\printeraddscript\ports.txt' -Delimiter "," -NoTypeInformation -Encoding UTF8  
New-Object PSObject -Property @{"Printserver"=$global:printservername;"Driver"=$global:driver;"Portname"=$global:portname;"ShareName"=$global:portname;"Location"=$global:location;"Comment"="$($global:IPaddress) - $($global:driver)";"DeviceID"=$global:portname} | select Printserver,Driver,Portname,ShareName,Location,Comment,DeviceID | export-csv 'c:\printeraddscript\printers.txt' -Delimiter "," -NoTypeInformation -Encoding UTF8  
Member: pixel0815
pixel0815 Nov 27, 2014 at 14:59:53 (UTC)
Goto Top
Danke !! :o) Das ist ja genial. Es läuft. Habe es getetstet auf meinem Printserver.
Member: Pago159
Pago159 Nov 28, 2014 at 08:34:57 (UTC)
Goto Top
@ jodel23: Super arbeit, wenn ich das Script richtig verstehe, sollte ich doch auch Remote Drucker installieren können. Oder lese ich das Script da falsch?
Könntest du evtl noch eine Möglichkeit einbauen, zu den Ausgewählten Druckern einen bestimmten *.inf Treiber zu installieren?
z.B. Drucker "Lexmark MS510" nimm Treiber "\\Share\Ordner1\ms510.inf

Oben wurde ja auch mal geschrieben, dass man das ganze ohne die CSV-Dateien machen kann, an dieser Variante wäre ich auch sehr interessiert,
so dass nur eine "install.log" Datei erstellt wird.