VMware Cloud Community
piercj2
Enthusiast
Enthusiast
Jump to solution

Posh-ssh output to array

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,

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

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"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

piercj2
Enthusiast
Enthusiast
Jump to solution

That was it, thanks Luc.

 

Reply
0 Kudos