GetString - answer in an unreadable form

From some switchesI get the answer in this form:

??↑?? ??#??'

I’am using protocol 23 to connect to the switch because SSH still gives an error. Executing the command and geting a response is as follows:

function readtelnet {
    $response = ""
    While($Stream.DataAvailable) {
      $Read = $Stream.Read($Buffer, 0, 1024)
      $response += ($Encoding.GetString($Buffer, 0, $Read))
    return $response;
function waitfor {
    param([string]$search, [int]$timeout)
    if ($timeout -eq 0) {
      $timeout = 3
    $pause = 1000
    $pending = readtelnet
    $myResult = $pending
    $counter = 0
    while ($pending -notmatch $search -and $counter -le ($timeout * (1000/$pause))) {
      $counter ++
      Start-Sleep -m $pause
      $pending = readtelnet
      $myResult += $pending
    return $myResult

$Port = 23
$Result = ''
$Usuario = 'admin'
$Contrasena = '********'
$AdrecaIP = ""
$Socet = New-Object Net.Sockets.TcpClient
if ($Socet.Connected) {
        $Stream = $Socet.GetStream()
        $Writer = New-Object System.IO.StreamWriter($Stream)
        $Buffer = New-Object System.Byte[] 1024
        $Encoding = New-Object System.Text.ASCIIEncoding

         write-host "ENTER - Username: $Usuario"
         $Result = waitfor 'Username:'

         $Result += waitfor 'UNFAS1_A_\$ '

         Write-Output $Result
         Start-Sleep -s 1

         $Writer = $null
         $Stream = $null
         $Socet = $null

What cout be the reason I get my answer in an unreadable form and how do you fix an erron?
Many thanks!

I’m guessing there’s a typo in your question because port 22 is SSH. From your code, you’re connecting via port 23 (telnet).

If it’s working for some switches and not others, then only you can troubleshoot that. I would suggest you telnet to a working switch, and a non-working switch using a console or PuTTY so you can compare the output. Maybe there’s a motd or banner configured that’s affecting the output?

1 Like

Have you tried playing with different encoding methods?

Yes, that’s right, I’m using port 23. Connecting through port 22 I get an error:

SSH-2.0-OpenSSH_7.3p1.RL or SSH-2.0-OpenSSH_6.2

Protocol mismatch.

Using console or PuTTY connection and executing commands of the configuration is successful

I will try to playing with different encoding methods.

I just had a look through your previous post. It looks like you’re using the same code as above to make the SSH connection? That won’t work. SSH is more complicated than just opening a connection to the correct port.

SSH client is enabled in PowerShell in Windows 10 1809 and higher, you should use that. There’s a good getting started guide here:

1 Like