title: PowerShell, read/write to SSH.NET streams
body: <p>I would like to connect to a server via SSH, with PowerShell, <strong>and then change to a different user</strong>.</p>
  
  <p>In order to accomplish connecting to a server with PowerShell via SSH, I used <a href="http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library" rel="nofollow">SSH from PowerShell using the SSH.NET library</a> and <a href="http://241931348f64b1d1.wordpress.com/2012/07/11/how-to-connect-via-ssh-from-powershell/" rel="nofollow">How to connect via SSH from powershell</a>. That was great and gave me simple commandlets such as <em>new-sshsession</em> and <em>invoke-sshcommand</em> to work with. </p>
  
  <p>Next, I wanted to <em>su</em> to a different user. When I tried to do that via the commandlets, I got the error: <strong>standard in must be a tty</strong>.</p>
  
  <p>I understand that I can edit the security settings on the server to let me <em>su</em> to a user without being in interactive mode. However, I can't change that setting on all the servers I want to connect to. </p>
  
  <p>Working off of a C# example I found at <a href="https://sshnet.codeplex.com/discussions/285853" rel="nofollow">https://sshnet.codeplex.com/discussions/285853</a>, I put together:</p>
  
  <pre><code>$server = "server1"
  $port = 22
  $username = "user1"
  $password = "password1"
  
  $ssh = new-object Renci.SshNet.SshClient($server, $port, $username, $password)
  $ssh.Connect()
  
  $inputstream = new-object System.IO.MemoryStream
  $streamwriter = new-object System.IO.StreamWriter($inputstream)
  $outputstream = new-object System.IO.MemoryStream
  $streamreader = new-object System.IO.StreamReader($outputstream)
  
  $shell = $ssh.CreateShell($inputstream, $outputstream, $outputstream)
  $shell.Start()
  $streamwriter.WriteLine("echo hello &gt; tempfile.txt")
  $streamwriter.Flush()
  $streamreader.ReadToEnd()
  
  $shell.Stop()
  </code></pre>
  
  <p>Running, that I get output such as:</p>
  
  <pre><code>Last login: Fri Nov  8 11:37:45 2013 from 10.XXX.XXX.XXX
  [user1@server1 /users/user1]
  </code></pre>
  
  <p>However, <code>/users/user1/tempfile.txt</code> never gets written and I do not get any further output.</p>
  
  <p>Do you see what I'm doing wrong?</p>