Difference between revisions of "SSH Client (PowerShell)"

Jump to navigation Jump to search
→‎Renci SSH.NET: Some rewording, and added .NET not installed error
(Initial creation - content from PowerShell Examples page)
 
(→‎Renci SSH.NET: Some rewording, and added .NET not installed error)
Line 13: Line 13:


== Renci SSH.NET ==
== Renci SSH.NET ==
[http://sshnet.codeplex.com/ Renci SSH.NET] is an ongoing project, still in beta, but already provides a workable solution. Providing both .NET 4.0 and 3.5 libraries (you need 3.5 for PowerShell v2).
[http://sshnet.codeplex.com/ Renci SSH.NET] is an ongoing project, its still in beta, but already provides a workable solution with all the core features you're likely to want. Plus as its in active development there's high chance that any probs you might have would be promptly worked on. It provides both .NET 4.0 and 3.5 libraries, '''you need to use the .NET 3.5 library for PowerShell v2'''.


To get running you need to download the binary DLL file from http://sshnet.codeplex.com/releases/view/72214, and put it in a place where you can load the assembly into your PowerShell session, eg  
To get running you need to download the binary DLL file from http://sshnet.codeplex.com/releases/view/72214, and put it in a place where you can load the assembly into your PowerShell session, eg  


<source lang="powershell">
<source lang="powershell">
[void][reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\RenciSSH\Renci.SshNet.dll") )      # DLL file is in \lib\RenciSSH\ below calling script
[void][reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\RenciSSH\Renci.SshNet.dll") )      # DLL file is in \lib\RenciSSH\ folder below the calling script
</source>
</source>


Create a SSH Client object, and connect...
If you don't have .NET 3.5 installed, you'll need to do so from https://www.microsoft.com/download/en/details.aspx?displaylang=en&id=21 (the main installer is just a downloader/bootstrapper, there's a full fat install package link at the bottom of the page).
 
'''Create a SSH Client object, and connect...'''
<source lang="powershell">
<source lang="powershell">
$SshClient = New-Object Renci.SshNet.SshClient("ssh-server", 22, "user", "password")
$SshClient = New-Object Renci.SshNet.SshClient("ssh-server", 22, "user", "password")
Line 27: Line 29:
</source>
</source>


Run a command (eg a Unix <code>ifconfig</code> against the server...
If the connect fails with the following error - you need to install .NET 3.5 (the SSH library is trying to use components that don't exist).
* <code>Could not load file or assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.</code>
 
'''Run a command (eg a Unix <code>ifconfig</code> against the server...'''
<source lang="powershell">
<source lang="powershell">
if ($SshClient.IsConnected) {
if ($SshClient.IsConnected) {
Line 35: Line 40:
</source>
</source>


Clear down...
'''Clear down...'''
<source lang="powershell">
<source lang="powershell">
$SshCommand.Dispose()
$SshCommand.Dispose()

Navigation menu