Execute multiple commands via SSH and PowerShell

nixda picture nixda · Jun 26, 2014 · Viewed 8.5k times · Source

I successfully managed to connect to a Cisco IE-2000-L switch via SSH. I used the Renci SSH.NET library.

Starting guide: http://vwiki.co.uk/SSH_Client_(PowerShell)

My working code is

# Load SSH library (for .NET 4.0 and PowerShell 3)
$DllPath = "D:\temp\Renci.SshNet.dll"
[void][reflection.assembly]::LoadFrom( (Resolve-Path $DllPath) )

# Connect to switch (Cisco IE2000-L) with IP, port, username, password
$SshClient = New-Object Renci.SshNet.SshClient('172.20.91.30', 22, 'admin', 'mypassword')
$SshClient.Connect()

# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')

# show result
$SshCommand.Result 

# close SSH connection
$SshCommand.Dispose()
$SshClient.Disconnect()
$SshClient.Dispose()

My problem is

The above code sends just one command. But I want to execute several commands consecutively without closing and reopening a session.

If I add a second command right after the first one

# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')
$SshCommand = $SshClient.RunCommand('show start')

...the script hangs and never finishes. What am I doing wrong?


Minor relevant information

Answer