PowerShell, read/write to SSH.NET streams

Question:

I would like to connect to a server via SSH, with PowerShell, and then change to a different user.

In order to accomplish connecting to a server with PowerShell via SSH, I used SSH from PowerShell using the SSH.NET library and How to connect via SSH from powershell. That was great and gave me simple commandlets such as new-sshsession and invoke-sshcommand to work with.

Next, I wanted to su to a different user. When I tried to do that via the commandlets, I got the error: standard in must be a tty.

I understand that I can edit the security settings on the server to let me su to a user without being in interactive mode. However, I can’t change that setting on all the servers I want to connect to.

Working off of a C# example I found at https://sshnet.codeplex.com/discussions/285853, I put together:

Running, that I get output such as:

However, /users/user1/tempfile.txt never gets written and I do not get any further output.

Do you see what I’m doing wrong?

Answer:

Using an example from https://sshnet.codeplex.com/discussions/439210, I was able to solve my problem with the below code. The main issue was that I was creating two different streams for input/output and I needed to just use one stream.

Source:

PowerShell, read/write to SSH.NET streams by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply