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?
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#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)
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 255956
Url: https://administrator.de/forum/drucker-powershell-gui-probleme-255956.html
Ausgedruckt am: 10.04.2025 um 17:04 Uhr
8 Kommentare
Neuester Kommentar

Moin,
Dat muss so
Btw. CSV Dateien manuell zu erstellen macht man eigentlich nicht, wo es doch die schönen Custom Objects gibt
Gruß jodel32
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
$csv = New-Object PSObject -Property @{IPAddress=$ipaddress;Location=$location
}$csv | Export-CSV c:\drucker.csv -delimiter ',' -NoType -Encoding UTF8
Gruß jodel32

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 ...
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 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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
@ 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.
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.