-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckPrinters.ps1
210 lines (181 loc) · 6.8 KB
/
CheckPrinters.ps1
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Printer Connection Timeout
$TIMEOUT = 2000;
$READ_BUFFER_SIZE = 1024;
$READ_ENCODING = New-Object System.Text.AsciiEncoding;
$BYTE_START_OF_TEXT = [System.Byte] 2;
$BYTE_END_OF_TEXT = [System.Byte] 3;
<#
.SYNOPSIS
Gets the targeted printer's settings in xml format.
.DESCRIPTION
The Get-PrinterSettingsXML function retrieves the printer's xml configuration and returns it as a configuration object. An example of this configuration can be seen in ExamplePrinterSettings.xml.
.PARAMETER printerIp
The ip of the printer.
.PARAMETER port
The port to connect to (defaults to 9100).
.EXAMPLE
Get the name of a printer.
(Get-PrinterSettingsXML 0.0.0.0)["ZEBRA-ELTRON-PERSONALITY"]["SAVED-SETTINGS"]["NAME"]
.NOTES
Some printers return invalid xml and this function fails. We can't really do anything about that.
#>
Function Get-PrinterSettingsXML([string] $printerIp, [int] $port = 9100) {
$configString = Send-PrinterCommand "^XA^HZS^XZ" $printerIp $port -read "</ZEBRA-ELTRON-PERSONALITY>";
try {
[xml]$configString;
}
catch {
throw "Could not parse config XML from ${printerIp}:$port";
}
}
<#
.SYNOPSIS
Sends a command to a printer and reads it's response (if any).
.DESCRIPTION
The Send-PrinterCommand function sends a command to the printer and (if specified) attempts to read it's response.
.PARAMETER command
The command to send.
.PARAMETER printerIp
The ip of the printer.
.PARAMETER port
The port to connect to (defaults to 9100).
.PARAMETER read
If present, attempt to read a response from the printer.
.PARAMETER endText
An optional string to listen for to indicate the end of a response.
.EXAMPLE
Get the diagnostics printout of a printer.
Send-PrinterCommand "~HD" 0.0.0.0
.NOTES
Since some commands give different responses, or none at all, there's no guarentee that a command has (or hasn't) run successfuly.
#>
Function Send-PrinterCommand([string] $command, [string] $printerIp, [int] $port = 9100, [switch] $read, [string] $endText = "") {
# Create a new tcp client and connect the printer
$client = New-Object System.Net.Sockets.TcpClient;
if ( -Not $client.ConnectAsync($printerIp, $port).Wait($TIMEOUT))
{
Throw "Failed to connect to ${printerIp}:$port";
}
# Get a data stream for the socket
$stream = $client.GetStream();
# Create a new Writer and write our command to the stream
$writer = New-Object System.IO.StreamWriter($stream);
$writer.WriteLine($command);
$writer.Flush();
# Response string
$response = "";
if($read) {
# Timeout counter
$timeoutCounter = 0;
# While the timeout's not expired
while ($timeoutCounter -lt $TIMEOUT) {
# Check if there's data available
if($stream.DataAvailable) {
# We got some data so reset the timeout
$timeoutCounter = 0;
# Create the Read Buffer
$buffer = New-Object System.Byte[] $READ_BUFFER_SIZE;
# Fill the buffer
$readSize = $stream.Read($buffer, 0, $READ_BUFFER_SIZE);
# Calculate beginning/ending of string
$beginIndex = 0;
$endIndex = $readSize;
$foundEnd = $false;
for ($i = 0; $i -lt $readSize; $i++) {
$byte = $buffer[$i];
if($byte -eq $BYTE_START_OF_TEXT) {
$beginIndex = $i + 1;
}
if($byte -eq $BYTE_END_OF_TEXT) {
$foundEnd = $true;
$endIndex = $i;
}
}
# Decode Bytes and add to response
if($endIndex -gt $beginIndex) {
$response += $READ_ENCODING.GetString($buffer, $beginIndex, $endIndex - $beginIndex);
}
else {
$response += $READ_ENCODING.GetString($buffer, 0, $readSize);
}
# Check for custom end text since some commands don't use an END-OF-TEXT character
if($endText -ne "" -and $response.Contains($endText)) { $foundEnd = $true; }
# If we got the end, stop reading
if($foundEnd) { break; }
}
else {
# Increment the timeout and wait a bit for more data
$timeoutCounter += 50;
start-sleep -Milliseconds 50;
}
}
# If we timed out, throw an error
if( -Not $timeoutCounter -lt $TIMEOUT) { Throw "Read timed out for ${printerIp}:$port"; }
# Return (But not actually because PS)
$response
}
# Close everything
$writer.Close()
$stream.Close()
}
# All of our check-in printers
$printers = @(
# Dover
@{ Ip="10.1.27.1"; },
@{ Ip="10.1.27.2"; },
@{ Ip="10.1.27.3"; },
@{ Ip="10.1.27.4"; },
@{ Ip="10.1.27.5"; },
@{ Ip="10.1.27.6"; },
@{ Ip="10.1.27.7"; },
@{ Ip="10.1.27.8"; },
@{ Ip="10.1.27.9"; },
@{ Ip="10.1.27.10"; },
@{ Ip="10.1.27.11"; },
@{ Ip="10.1.27.12"; },
@{ Ip="10.1.27.13"; },
@{ Ip="10.1.27.36"; },
@{ Ip="10.1.27.37"; },
@{ Ip="10.1.27.39"; },
@{ Ip="10.1.27.127"; },
# Millersburg
@{ Ip="10.2.27.11"; },
@{ Ip="10.2.27.12"; },
@{ Ip="10.2.27.13"; },
@{ Ip="10.2.27.14"; },
@{ Ip="10.2.27.106"; },
# Canton
@{ Ip="10.3.27.11"; },
@{ Ip="10.3.27.12"; },
@{ Ip="10.3.27.13"; },
@{ Ip="10.3.27.14"; },
@{ Ip="10.3.27.15"; },
@{ Ip="10.3.27.27"; }
# Coshocton
# -
# Wooster
# -
)
# Set all printers to cut-off mode
$i = 0
foreach ($printer in $printers) {
Write-Progress -Activity "Setting cut mode" -Status "Setting $($printer.Ip)" -PercentComplete ($i++ / $printers.count * 100)
try { Send-PrinterCommand "^XA^MMC^JUS^XZ" $printer.Ip } catch { Write-Warning "$_" }
}
# Get the current config for each one
$i = 0
foreach ($printer in $printers) {
Write-Progress -Activity "Checking Printers" -Status "Checking $($printer.Ip)" -PercentComplete ($i++ / $printers.count * 100)
try {
$settings = (Get-PrinterSettingsXML $printer.Ip)["ZEBRA-ELTRON-PERSONALITY"]["SAVED-SETTINGS"]
$printer.Name = $settings["NAME"].InnerText
$printer.CurrentMode = $settings["PRINT-MODE"]["MODE"]["CURRENT"].InnerText
$printer.SavedMode = $settings["PRINT-MODE"]["MODE"]["STORED"].InnerText
}
catch {
Write-Warning "$_"
$printer.Name = "ERROR"
$printer.CurrentMode = $printer.SavedMode = "-"
}
}
$printers.ForEach({[PSCustomObject]$_}) | Format-Table Ip, Name, CurrentMode, SavedMode -AutoSize