locked
Powershell Increase index for connection ssh RRS feed

  • Question

  • Hi,

    I'm checking some ports on linux servers via New-SshSession.
    This works fine but I'd like to close the sessions after all my sessions are made. Now I'm doing this after each session and that seems to give problems.

    In other words the -Index should increase with 1.
    So each next server would get -Index 0 +1, in my example (below) the next server would get

    $Grep80 = Invoke-SSHCommand -Index 1 -Command 'netstat -tlpn | grep 80'

    next one

    $Grep80 = Invoke-SSHCommand -Index 2 -Command 'netstat -tlpn | grep 80'

    etc

    Hope this is clear

    Please advise.

     foreach ($Server in $Servers)
      {

        try { New-SSHSession -ComputerName $Server -Credential $credentials -AcceptKey -ErrorAction Stop
          $Grep80 = Invoke-SSHCommand -Index 0 -Command 'netstat -tlpn | grep 80'
          $Grep443 = Invoke-SSHCommand -Index 0 -Command 'netstat -tlpn | grep 4443' 
          $connectionstatus = 'OK'
          Get-SSHSession | Remove-SSHSession
        }
        catch {$connectionstatus = 'Error occured';$Grep80 = '';$Grep443 = ''}

    Goal =   getting the "Get-SSHSession | Remove-SSHSession" out of the foreach


    Jan Hoedt

    • Moved by Bill_Stewart Monday, September 25, 2017 5:01 PM This is not a support forum for third-party software
    Monday, May 29, 2017 1:23 PM

Answers

  • Found a way around it.

    $Sessionid = (Get-SSHSession | Where-Object Host -eq $server).Sessionid

    then

     $Grep443 = Invoke-SSHCommand -Index $SessionId -Command 'netstat -tlpn | grep 4443' 


    Jan Hoedt

    • Marked as answer by janhoedt Tuesday, May 30, 2017 10:49 AM
    Tuesday, May 30, 2017 10:49 AM

All replies

  • You need to post issues to the author of the scripts.


    \_(ツ)_/

    Monday, May 29, 2017 1:31 PM
  • Don't understand why you would think I'm not the author.

    Jan Hoedt

    Tuesday, May 30, 2017 7:40 AM
  • Found a way around it.

    $Sessionid = (Get-SSHSession | Where-Object Host -eq $server).Sessionid

    then

     $Grep443 = Invoke-SSHCommand -Index $SessionId -Command 'netstat -tlpn | grep 4443' 


    Jan Hoedt

    • Marked as answer by janhoedt Tuesday, May 30, 2017 10:49 AM
    Tuesday, May 30, 2017 10:49 AM