posh-ssh

Posh-SSH - Missing the first character while using the WriteLine method in the loop


I am trying to create a simple script to update something on a Cisdo device. Below, you can see the part of the code that has a strange error:

$Stream = New-SSHShellStream -SessionId $Session.SessionId
$Stream.WriteLine("sh object-group name $ObjGroup")
Start-Sleep -Seconds 1
$Result = $Stream.Read()
Write-Warning "$Result"
if ($Result.Contains('Network object group')) {
  foreach ($Item in $IPs) {
    $Stream.WriteLine("sh object-group name $ObjGroup | i $Item")
}
Start-Sleep -Seconds 1
$Result = $Stream.Read()
Write-Warning "$Result"

The output of the warning message looks like this:

WARNING:
Session established with AUTHENTICATION Servers

Connected to hostname01 line 2
Use of this system constitutes your consent to monitoring


hostname01#sh object-group name OBJECT_GROUP
Network object group OBJECT_GROUP
 Description -- ACL: Host and Subnets Drop Xway Service --
 host 2.57.168.230
 host 2.57.168.233
 host 2.57.169.46
 host 43.153.134.127
 host 45.8.17.67
 host 45.91.23.11
 host 45.130.81.76
 host 45.131.193.74
 host 45.131.195.174
 host 45.132.224.63
 host 45.146.54.5
 host 45.146.55.147
 host 66.133.123.247
 host 68.67.59.21
 host 68.67.59.22
 host 74.208.83.155
 host 84.247.59.217
 host 85.203.15.17
 host 85.203.36.128
 host 85.237.194.68
 host 86.188.222.131
 --More--

hostname01#h object-group name OBJECT_GROUP | i 193.107.216.241
help object-group name OBJECT_GROUP | i 193.107.216.241
^
% Invalid input detected at '^' marker.

hostname01#sh object-group name OBJECT_GROUP | i 74.208.83.155
 host 74.208.83.155
hostname01#sh object-group name OBJECT_GROUP | i 84.247.59.217
 host 84.247.59.217
hostname01#sh object-group name OBJECT_GROUP | i 85.203.15.17
 host 85.203.15.17
hostname01#sh object-group name OBJECT_GROUP | i 85.203.36.128
 host 85.203.36.128
hostname01#sh object-group name OBJECT_GROUP | i 85.237.194.68
 host 85.237.194.68
hostname01#sh object-group name OBJECT_GROUP | i 86.188.222.131
 host 86.188.222.131
hostname01#

As you can see, the first command in the loop unexpectedly lost the first character. But the other iterations looks as I expected.

So, what can I do with this behavior? Is it a bug, or am I doing something wrong?


Solution

  • I resolved this issue. Added a new line before the loop.

    if ($Result.Contains('Network object group')) {
      $Stream.WriteLine("`n")
      foreach ($Item in $IPs) {
        $Stream.WriteLine("sh object-group name $ObjGroup | i $Item")
    }