title: How to Configure cisco router using POWERSHELL SSH.net?
body: <p>Good, this is the code I'm using:</p>
  <pre><code>function escribe_log($dispositivo, $log, $comando){
  $Fichero_Log = &quot;C:\Modificacion_routers.log&quot;
  $texto = $dispositivo
      $texto &gt;&gt; $Fichero_Log
      $texto = $log
      $texto &gt;&gt; $Fichero_Log
      $texto = $comando
      $texto &gt;&gt; $Fichero_Log
  }
  
  ##PROGRAMA PRINCIPAL##
  Import-Module SSH-Sessions
  
  $ficheroIPS = &quot;C:\ipsdispositivos.txt&quot;
  $ficheroComandos = &quot;c:\comandos.txt&quot;
  
  $dispositivos = Get-Content $ficheroIPS
  $comandos = Get-Content $ficheroComandos
  
  foreach ($dispositivo in $dispositivos) {
      New-SshSession -ComputerName $dispositivo -Username USUARIO -password PASSWORD
      foreach ($comando in $comandos) {
          $SshResults = Invoke-SshCommand -ComputerName $dispositivo -Command $comando
          escribe_log $dispositivo $SshResults $comando
          Start-Sleep -s 1
      }
      $comandos = Get-Content $ficheroComandos
  }
  
  Remove-sshSession -RemoveAll
  </code></pre>
  <p>Works perfectly to launch &quot;sh&quot; show commands, my problem is that when I launch a command automatically disconnects the session, then command is not possible to launch a &quot;conf t&quot; to enter configuration mode and then launch configuration commands.</p>
  <p>Do you know if this library is possible to configure a router?
  if that is not possible,
  would they know me any alternative to configure the router using script?</p>
  <hr />
  <p>SOLVED:</p>
  <h1>FUNCTION FOR CHANGES TO SSH ROUTER/SWITCH WITH POWERSHELL</h1>
  <pre><code>Function set-SSH($devices, $ssh){
      function ReadStream($reader)
      {
          $line = $reader.ReadLine();
          while ($line -ne $null)
          {
              $line
              $line = $reader.ReadLine()
          }
      }
  
      function WriteStream($cmd, $writer, $stream)
      {
          $writer.WriteLine($cmd)
          while ($stream.Length -eq 0)
          {
              start-sleep -milliseconds 1000
          }
      }    
  
      $stream = $ssh.CreateShellStream(&quot;dumb&quot;, 80, 24, 800, 600, 1024)
  
              $reader = new-object System.IO.StreamReader($stream)
              $writer = new-object System.IO.StreamWriter($stream)
              $writer.AutoFlush = $true
  
              while ($stream.Length -eq 0)
              {
                  start-sleep -milliseconds 500
              }
              ReadStream $reader
  
              foreach ($command in $commands) {
  
                  WriteStream $command $writer $stream
                  ReadStream $reader
                  Write-Host $reader
                  start-sleep -milliseconds 500
              }
  
              $stream.Dispose()
              $ssh.Disconnect()
              $ssh.Dispose()
  </code></pre>
  <p>}</p>
  <hr />
  <p>There are three variables to initialize $commands, $ssh, $devices</p>
  <pre><code>    #Example of file commands.txt...
      #sh conf
      #conf t
      #int f0/1
      #description Modify via powershell
      #exit
      #exit
      #wr
      #...
      $fileCommands= &quot;.\commands.txt&quot;
      $commands= Get-Content $fileCommands
      #Example of file devices.csv...
      #10.10.10.10;SWITCH01
      #10.10.10.11;SWITCH02
      #10.10.10.12;SWITCH03
      #...
      $devices = Import-Csv -path &quot;.\devices.csv&quot; -header 'ip','hostname' -delimiter ';'
      $port = 22
      $ssh = new-object Renci.SshNet.SshClient($devices[0].ip, $port, $user, $password)
      Try{
              $ssh.Connect()
              Start-Sleep -s 1
              set-SSH $commands $ssh
      }catch{
      }
  </code></pre>