Last active
April 3, 2023 11:45
-
-
Save markgodiy/6b74e1cfdecc97b299735e39bfe44611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function ZebraTCPClient { | |
| <# | |
| Source/Crecit: Sven Sperner, https://github.com/sperner | |
| Original Code Permalink: | |
| https://github.com/sperner/PowerShell/blob/8c661ec4d0914b02095ffabb7c9dcbd8b1f0185a/TcpClient.ps1 | |
| .DESCRIPTION | |
| -To use, load function into a powershell session console. | |
| -Default port is 9100. Zebra printers also uses port 6101. | |
| -Port defaults to 9100 unless otherwise provided. | |
| -Modified from Sven's code for more traditional command prompt interface. | |
| -Added "quit" feature to exit the function. | |
| .EXAMPLE | |
| ZebraTCPClient -remotehost 100.100.100.100 -port 6101 | |
| .EXAMPLE | |
| ZebraTCPClient 100.100.100.100 | |
| #> | |
| param( [string] $remotehost, [int] $port = 9100 ) | |
| try | |
| { | |
| Write-Host "Connecting to $remoteHost on port $port ... " -NoNewLine | |
| try | |
| { | |
| $socket = New-Object System.Net.Sockets.TcpClient( $remoteHost, $port ) | |
| Write-Host -ForegroundColor Green "OK" | |
| } | |
| catch | |
| { | |
| Write-Host -ForegroundColor Red "failed" | |
| Break | |
| } | |
| $stream = $socket.GetStream( ) | |
| $writer = New-Object System.IO.StreamWriter( $stream ) | |
| $buffer = New-Object System.Byte[] 1024 | |
| $encoding = New-Object System.Text.AsciiEncoding | |
| while ($command -notmatch "^quit$") { | |
| start-sleep -m 500 | |
| while($stream.DataAvailable ) | |
| { | |
| $read = $stream.Read( $buffer, 0, 1024 ) | |
| Write-Host -n "Res> $(($encoding.GetString( $buffer, 0, $read )))`r`n" | |
| } | |
| Write-Host "Cmd> " -nonewline; $command = Read-Host | |
| $writer.WriteLine( $command ) | |
| $writer.Flush( ) | |
| } | |
| } | |
| finally | |
| { | |
| if( $writer ) { $writer.Close( ) } | |
| if( $stream ) { $stream.Close( ) } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment