Powershell running multiple commands

Oct 11, 2012 at 5:59 PM

 I am trying to write a script which allows me to send multiple commands to a Cisco switch.  Everything was working great until I tried to add a second Runcommand. 

Here is the code:

# Load the SSH library
try {
    [void][reflection.assembly]::LoadFrom( (Resolve-path $SshDll) )
} catch {
    Write-Error $_
    throw "Unable to find/load the Ssh.Net DLL"
}

# Connect to router and run command
    Write-Host "Connecting to $IPAddress..."
    $SshClient = New-Object Renci.SshNet.SshClient("10.11.1.5", 22, "root", "xxxxxxxxxx")
    $SshClient.Connect()   
       $SshCommand = $SshClient.RunCommand("show ip inter brief")
    Write-Host $SshCommand.Result
    $SshCommand = $SshClient.RunCommand("show boot")
    Write-Host $SshCommand.Result

# Close session to router and dump objects
    $SshCommand.Dispose()
    $SshClient.Disconnect()
    $SshClient.Dispose()

I now get this error on the second RunCommand line:

Exception calling "RunCommand" with "1" argument(s): "An established connection was aborted by the software in your host machine"
At line:21 char:5
+     $SshCommand = $SshClient.RunCommand("show boot")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SocketException

Any suggestion about what I am doing wrong?

Michael K

 

 

Oct 15, 2012 at 9:39 PM

could the show boot be paging? it has been a long time since I have played with cisco devices. If it does you may need to first run terminal length 0 so as to disable paging and it will show all info at once.

May 5, 2014 at 2:59 PM
I'm getting the same errors when trying to push a firmware upgrade to a bunch of HP iLo's. It always seems to be the second runcommand.

Exception calling "RunCommand" with "1" argument(s): "An established connection was aborted by the software in your host machine."
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\SSH-Sessions\SSH-Sessions.psm1:line:272 char:66
  • $CommandObject = $global:SshSessions.$Computer.RunCommand( <<<< $Command)

    New-sshsession -computername $Line.RIB_IP_Address -username $UserName -password $Password
    Invoke-SshCommand -InvokeOnAll -command 'cd /map1/firmware1'
    Invoke-SshCommand -InvokeOnAll -command 'load -source http://xxxx.yyy.zzzz/ilo/ilo3_170.bin'
    Remove-SshSession $Line.RIB_IP_Address
I'm spinning my wheels here, I've tried trimming down my command to bare bones and it still doesn't work, suggestions?
May 10, 2014 at 11:08 AM
Because Invoke-SshCommand is not executing in a shell but using SSH Exec to execute the command so the "cd" does nothing to change a current working directory because you are not in a shell. You would need to create a shell stream and execute your commands inside of it.