Forum Discussion
Retrieving information from multiple Access Points via PowerShell
fstorer I wrote this, I think it covers what you are trying to accomplish.
Let me know how it goes.
<#
.SYNOPSIS
Create ssh session and run a command from a list of hosts
.DESCRIPTION
.NOTES
Author: Erick A. Moreno
Email: emoreno@request-script.com
Date: 12 Jan 2020
PowerShell: v3
.References
#https://www.powershellgallery.com/packages/Posh-SSH/2.1 #Install Posh-Ssh Module to use the corresponding cmdlets
#requested: https://techcommunity.microsoft.com/t5/forums/replypage/board-id/WindowsPowerShell/message-id/1011
#>
$WorkingDir = "C:\Users\erick\Desktop\LogsFolder"
$ScriptTime = Get-Date -UFormat "%m%d%Y-%H%M"
$Credentials = Get-Credential
$APList = Import-Csv "$WorkingDir\APIPs.csv"
$Output = @()
Foreach($AP in $APList)
{
Write-Host "Working on AP: $($AP.Name)" -ForegroundColor Cyan
$Command = $AP.Command
$Results = $null
$session = New-SSHSession -ComputerName $AP.IP -Credential $Credentials -AcceptKey
If(![string]::IsNullOrEmpty($($session)))
{
$Results = Invoke-SSHCommand -Index $session.SessionId -Command $Command
$Properties = [Ordered]@{
APName = $AP.Name
APIPAddress = $AP.IP
Command = $Command
Results = $($Results.Output) -join ";" #Adapt this depending on the type of output that you expect
}
$Output += New-Object PSObject -Property $Properties
Remove-SSHSession -SessionId $session.SessionId
}
Else
{
Write-Host "Unable to stablish ssh session with host: $($AP.Name) / $($AP.IP)" -ForegroundColor Yellow
}
}
$Output | Export-Csv "$WorkingDir\APsReport_$Scripttime.csv" -NoTypeInformation -Encoding UTF8
- fstorerFeb 25, 2020Brass Contributor
Thank you so much for your kind reply and apologies for replying so late, I was off for a few weeks... I have tried your script, I am able to start a SSH session with the AP (I can see the SessionID and that the connection is working), but the output of the commands is always empty. When I try to run the single commands, I got something like this:
Host : 192.168.x.x
Output : {}
ExitStatus : 255For example, I tried to run the following commands:
Invoke-SSHCommand -SessionId 0 -Command "show interface"
Invoke-SSHCommand -SessionId 0 -Command "show ssid"
In both cases, I got the results above.
I see that the ExitStatus is returning a 255 error code, but I can't understand why.
When running those commands on the AP, I get tables with multiple columns (Name/MAC addr/Mode/State/Chan/VLAN/Radio/Hive/SSID) and a list of MAC addresses/SSIDs.
Any suggestions?
- Erick A. Moreno R.Feb 25, 2020Iron Contributor
fstorer don't worry I understand, I'm glad to help.
About the error, have you confirmed that the network is ok? I mean the session was established correctly but there could be a timeout so please increase the timeout option to see how it behaves. Could you try with a single command like help just to see if it is taking the command string correctly?I don't think the format is a problem is should display something as I said in my comment in the script we may need to work on formatting that table.
Regards
- fstorerFeb 27, 2020Brass Contributor
I was able to solve my problem thanks to this article: https://github.com/darkoperator/Posh-SSH/issues/146
Using New-SSHShellStream instead of Invoke-SSHCommand returned all the results I wanted.
I need to tweak the final script, but these are the commands I used:
$Credentials = Get-Credential$session = New-SSHSession -ComputerName x.x.x.x -Credential $Credentials -AcceptKey$SSHStream = New-SSHShellStream -SessionId 0$SSHStream.WriteLine("show interface")$SSHStream.read()I hope this may be of some help!Thanks again for your help, I really appreciate that! And thanks to the creator of "Posh-SSH" module!