Automation

 View Only
  • 1.  Posh-ssh output to array

    Posted Aug 20, 2021 01:09 PM

    Hi, i'm using Posh SSH to check some config settings 

    The specific command i'm interested in is Invoke-SSHCommand -sessionID $sshSession.SessionId -Command "get dataplane | find Tx_ring_size"

    Output from Invoke-SSHCommand -sessionID $sshSession.SessionId -Command "get dataplane | find Tx_ring_size" is

    Host : myServerNameHere.mycomp.com
    Output : {Tx_ring_size : 4096}
    ExitStatus : 0

    Creating a Posh-SSH Session and running the command works as expected.

    $sshUsername = 'admin'
    $password = 'notThePassword!'
    $sshPassword = $password | ConvertTo-SecureString -AsPlainText -Force
    $sshPassword.MakeReadOnly()
    $sshCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sshUsername,$sshPassword
    $sshCredential = Get-Credential -Credential $sshCred

    $sshSession = New-SSHSession -ComputerName 'myServerNameHere.mycomp.com' -Credential $sshCredential -AcceptKey:$true
    if ($sshSession.Connected -like "True") {


    $Tx_ring_size = Invoke-SSHCommand -sessionID $sshSession.SessionId -Command "get dataplane | find Tx_ring_size"
    Write-Host "Tx_ring_size is: " -NoNewline
    (($Tx_ring_size.Output -split ':')[1]).Trim("{","}")

    $Rx_ring_size = Invoke-SSHCommand -sessionID $sshSession.SessionId -Command "get dataplane | find Rx_ring_size"
    Write-Host "Rx_ring_size is: " -NoNewline
    (($Rx_ring_size.Output) -split ':')[1]

    Write-Host "Disconnecting ssh session to myServerNameHere.mycomp.com "
    Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null
    Write-Host -ForegroundColor DarkGray "`n------------------------------------------------------------------`n"
    }

    Output to the console is as expected

    Tx_ring_size is: 4096
    Rx_ring_size is: 4096
    Disconnecting ssh session to myServerNameHere.mycomp.com

     

    I need to create a report for a large number of Servers by entering the details into an array and outputting the contents of the array

    $ServerNameArray = @()

    $resultsArray = @()

    $downloadsDir = "$HOME\Downloads"

    foreach ($item in $ServerNameArray){

    $resultsProperty = [ordered] @{
    'Server_FQDN'=$item.FQDN
    'Server_IP' = $item.IP

    $sshUsername = 'admin'
    $password = 'notThePassword!'
    $sshPassword = $password | ConvertTo-SecureString -AsPlainText -Force
    $sshPassword.MakeReadOnly()
    $sshCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sshUsername,$sshPassword
    $sshCredential = Get-Credential -Credential $sshCred

    $sshSession = New-SSHSession -ComputerName 'myServerNameHere.mycomp.com' -Credential $sshCredential -AcceptKey:$true
    if ($sshSession.Connected -like "True") {


    $Tx_ring_size = Invoke-SSHCommand -sessionID $sshSession.SessionId -Command "get dataplane | find Tx_ring_size"
    Write-Host "Tx_ring_size is: " -NoNewline
    (($Tx_ring_size.Output -split ':')[1]).Trim("{","}")

    $resultsProperty.Add("Tx_ring_size",(($Tx_ring_size.Output -split ':')[1]).Trim("{","}"))

    $Rx_ring_size = Invoke-SSHCommand -sessionID $sshSession.SessionId -Command "get dataplane | find Rx_ring_size"
    Write-Host "Rx_ring_size is: " -NoNewline
    (($Rx_ring_size.Output) -split ':')[1]

    $resultsProperty.Add("Rx_ring_size",(($Rx_ring_size.Output -split ':')[1]).Trim("{","}"))

    Write-Host "Disconnecting ssh session to myServerNameHere.mycomp.com "
    Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null
    Write-Host -ForegroundColor DarkGray "`n------------------------------------------------------------------`n"

    $resultsArray += New-Object -TypeName psobject -Property $resultsProperty
    $i++
    }

    $resultsArray | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending | Out-GridView

    $resultsArray | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending | Export-Excel -Path "$downloadsDir\Config_Check.xlsx"

     

    My issue is, the Tx_ring_size and Rx_ring_size fields are not being populated in the results array.

     

    Thanks,

     



  • 2.  RE: Posh-ssh output to array
    Best Answer

    Posted Aug 20, 2021 01:28 PM

    Not sure if that is the reason here, but some curly braces are missing and others are not alligned.
    I think it should be something like this

    $ServerNameArray = @()
    
    $resultsArray = @()
    
    $downloadsDir = "$HOME\Downloads"
    
    foreach ($item in $ServerNameArray) {
    
      $resultsProperty = [ordered] @{
        'Server_FQDN' = $item.FQDN
        'Server_IP' = $item.IP
      }
      $sshUsername = 'admin'
      $password = 'notThePassword!'
      $sshPassword = $password | ConvertTo-SecureString -AsPlainText -Force
      $sshPassword.MakeReadOnly()
      $sshCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sshUsername, $sshPassword
      $sshCredential = Get-Credential -Credential $sshCred
    
      $sshSession = New-SSHSession -ComputerName 'myServerNameHere.mycomp.com' -Credential $sshCredential -AcceptKey:$true
      if ($sshSession.Connected -like "True") {
    
    
        $Tx_ring_size = Invoke-SSHCommand -SessionId $sshSession.SessionId -Command "get dataplane | find Tx_ring_size"
        Write-Host "Tx_ring_size is: " -NoNewline
        (($Tx_ring_size.Output -split ':')[1]).Trim("{", "}")
    
        $resultsProperty.Add("Tx_ring_size", (($Tx_ring_size.Output -split ':')[1]).Trim("{", "}"))
    
        $Rx_ring_size = Invoke-SSHCommand -SessionId $sshSession.SessionId -Command "get dataplane | find Rx_ring_size"
        Write-Host "Rx_ring_size is: " -NoNewline
        (($Rx_ring_size.Output) -split ':')[1]
    
        $resultsProperty.Add("Rx_ring_size", (($Rx_ring_size.Output -split ':')[1]).Trim("{", "}"))
    
        Write-Host "Disconnecting ssh session to myServerNameHere.mycomp.com "
        Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null
        Write-Host -ForegroundColor DarkGray "`n------------------------------------------------------------------`n"
    
        $resultsArray += New-Object -TypeName psobject -Property $resultsProperty
        $i++
      }
    }
    
    $resultsArray | Sort-Object -Property { ($_ | Get-Member -MemberType NoteProperty).Count } -Descending | Out-GridView
    
    $resultsArray | Sort-Object -Property { ($_ | Get-Member -MemberType NoteProperty).Count } -Descending | Export-Excel -Path "$downloadsDir\Config_Check.xlsx"
    


  • 3.  RE: Posh-ssh output to array

    Posted Aug 20, 2021 01:49 PM

    That was it, thanks Luc.