Retrieving information from multiple Access Points via PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-1163088%22%20slang%3D%22en-US%22%3ERetrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1163088%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20doing%20a%20WiFi%20site%20survey%20and%20I%20would%20need%20to%20retrieve%20some%20information%20from%20all%20the%20Access%20Points%20installed%20in%20our%20school%20(around%20136).%20I%20wonder%20if%20it%20would%20be%20feasible%20to%20write%20a%20Powershell%20script%20which%20should%20be%20able%20to%3A%3C%2FP%3E%3CP%3E-%20Connect%20to%20all%20the%20Aerohive%20Access%20Points%20via%20SSH%20(from%20a%20list%20of%20IP%20addresses)%3CBR%20%2F%3E-%20Run%20a%20specific%20command%20for%20each%20AP%20(e.g.%20%22show%20interface%22)%3CBR%20%2F%3E-%20Export%20the%20results%20of%20that%20command%20to%20a%20txt%2Fhtml%20file%3C%2FP%3E%3CP%3EMy%20purpose%20is%20getting%20all%20the%20different%20MAC%20addresses%20used%20by%20each%20AP%20and%20listing%20them%20on%20a%20txt%2Fhtml%20file.%3C%2FP%3E%3CP%3EI%20have%20been%20told%20that%20I%20could%20use%20a%20Python%20module%20(Aeromiko)%20to%20extract%20data%20from%20the%20Access%20Points%20but%20I%20am%20not%20familiar%20with%20the%20Python%20language.%26nbsp%3B%3C%2FP%3E%3CP%3ECould%20you%20please%20advise%20on%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1163088%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1170972%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1170972%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F429537%22%20target%3D%22_blank%22%3E%40fstorer%3C%2FA%3E%26nbsp%3BI%20wrote%20this%2C%20I%20think%20it%20covers%20what%20you%20are%20trying%20to%20accomplish.%26nbsp%3B%3C%2FP%3E%3CP%3ELet%20me%20know%20how%20it%20goes.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%26lt%3B%23%0A.SYNOPSIS%0A%20%20%20%20Create%20ssh%20session%20and%20run%20a%20command%20from%20a%20list%20of%20hosts%0A.DESCRIPTION%0A%20%20%20%20%0A.NOTES%0A%20%20%20%20Author%3A%20%20%20%20%20Erick%20A.%20Moreno%0A%20%20%20%20Email%3A%20%20%20%20%20%20emoreno%40request-script.com%0A%20%20%20%20Date%3A%20%20%20%20%20%20%2012%20Jan%202020%0A%20%20%20%20PowerShell%3A%20v3%0A.References%0A%23https%3A%2F%2Fwww.powershellgallery.com%2Fpackages%2FPosh-SSH%2F2.1%20%20%23Install%20Posh-Ssh%20Module%20to%20use%20the%20corresponding%20cmdlets%0A%23requested%3A%20https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Freplypage%2Fboard-id%2FWindowsPowerShell%2Fmessage-id%2F1011%0A%0A%23%26gt%3B%0A%0A%24WorkingDir%20%20%3D%20%22C%3A%5CUsers%5Cerick%5CDesktop%5CLogsFolder%22%0A%24ScriptTime%20%20%3D%20Get-Date%20-UFormat%20%22%25m%25d%25Y-%25H%25M%22%0A%24Credentials%20%3D%20Get-Credential%0A%24APList%20%20%20%20%20%20%3D%20Import-Csv%20%22%24WorkingDir%5CAPIPs.csv%22%0A%24Output%20%20%20%20%20%20%3D%20%40()%0A%20%0AForeach(%24AP%20in%20%24APList)%0A%20%7B%0A%20%20Write-Host%20%22Working%20on%20AP%3A%20%24(%24AP.Name)%22%20-ForegroundColor%20Cyan%0A%20%20%24Command%20%3D%20%24AP.Command%20%0A%20%20%24Results%20%3D%20%24null%0A%20%20%24session%20%3D%20New-SSHSession%20-ComputerName%20%24AP.IP%20-Credential%20%24Credentials%20-AcceptKey%0A%0A%20%20If(!%5Bstring%5D%3A%3AIsNullOrEmpty(%24(%24session)))%0A%20%20%20%7B%0A%20%20%20%20%24Results%20%20%3D%20Invoke-SSHCommand%20-Index%20%24session.SessionId%20-Command%20%24Command%20%0A%20%20%20%20%0A%20%20%20%20%24Properties%20%20%3D%20%5BOrdered%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20APName%20%20%20%20%20%20%3D%20%24AP.Name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20APIPAddress%20%3D%20%24AP.IP%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Command%20%20%20%20%20%3D%20%24Command%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Results%20%20%20%20%20%3D%20%24(%24Results.Output)%20-join%20%22%3B%22%20%23Adapt%20this%20depending%20on%20the%20type%20of%20output%20that%20you%20expect%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%24Output%20%20%2B%3D%20New-Object%20PSObject%20-Property%20%24Properties%0A%20%20%20%20%0A%20%20%20%20Remove-SSHSession%20-SessionId%20%24session.SessionId%0A%20%20%20%7D%0A%20%20Else%0A%20%20%20%7B%0A%20%20%20%20Write-Host%20%22Unable%20to%20stablish%20ssh%20session%20with%20host%3A%20%24(%24AP.Name)%20%2F%20%24(%24AP.IP)%22%20-ForegroundColor%20Yellow%0A%20%20%20%7D%0A%20%7D%0A%0A%20%24Output%20%7C%20Export-Csv%20%22%24WorkingDir%5CAPsReport_%24Scripttime.csv%22%20-NoTypeInformation%20-Encoding%20UTF8%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1192863%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1192863%22%20slang%3D%22en-US%22%3E%3CP%3EDear%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20so%20much%20for%20your%20kind%20reply%20and%20apologies%20for%20replying%20so%20late%2C%20I%20was%20off%20for%20a%20few%20weeks...%20I%20have%20tried%20your%20script%2C%20I%20am%20able%20to%20start%20a%20SSH%20session%20with%20the%20AP%20(I%20can%20see%20the%20SessionID%20and%20that%20the%20connection%20is%20working)%2C%20but%20the%20output%20of%20the%20commands%20is%20always%20empty.%20When%20I%20try%20to%20run%20the%20single%20commands%2C%20I%20got%20something%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHost%20%3A%20192.168.x.x%3CBR%20%2F%3EOutput%20%3A%20%7B%7D%3CBR%20%2F%3EExitStatus%20%3A%20255%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20example%2C%20I%20tried%20to%20run%20the%20following%20commands%3A%3C%2FP%3E%3CP%3E%3CEM%3EInvoke-SSHCommand%20-SessionId%200%20-Command%20%22show%20interface%22%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3EInvoke-SSHCommand%20-SessionId%200%20-Command%20%22show%20ssid%22%3C%2FEM%3E%3C%2FP%3E%3CP%3EIn%20both%20cases%2C%20I%20got%20the%20results%20above.%3C%2FP%3E%3CP%3EI%20see%20that%20the%20ExitStatus%20is%20returning%20a%20255%20error%20code%2C%20but%20I%20can't%20understand%20why.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20running%20those%20commands%20on%20the%20AP%2C%20I%20get%20tables%20with%20multiple%20columns%20(Name%2FMAC%20addr%2FMode%2FState%2FChan%2FVLAN%2FRadio%2FHive%2FSSID)%20and%20a%20list%20of%20MAC%20addresses%2FSSIDs.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20suggestions%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1194275%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1194275%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F429537%22%20target%3D%22_blank%22%3E%40fstorer%3C%2FA%3E%26nbsp%3Bdon't%20worry%20I%20understand%2C%20I'm%20glad%20to%20help.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EAbout%20the%20error%2C%20have%20you%20confirmed%20that%20the%20network%20is%20ok%3F%20I%20mean%20the%20session%20was%20established%20correctly%20but%20there%20could%20be%20a%20timeout%20so%20please%20increase%20the%20timeout%20option%20to%20see%20how%20it%20behaves.%26nbsp%3B%20Could%20you%20try%20with%20a%20single%20command%20like%20help%20just%20to%20see%20if%20it%20is%20taking%20the%20command%20string%20correctly%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20think%20the%20format%20is%20a%20problem%20is%20should%20display%20something%20as%20I%20said%20in%20my%20comment%20in%20the%20script%20we%20may%20need%20to%20work%20on%20formatting%20that%20table.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1198170%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1198170%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20able%20to%20solve%20my%20problem%20thanks%20to%20this%20article%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fdarkoperator%2FPosh-SSH%2Fissues%2F146%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fdarkoperator%2FPosh-SSH%2Fissues%2F146%3C%2FA%3E%3C%2FP%3E%3CP%3EUsing%20%3CEM%3ENew-SSHShellStream%3C%2FEM%3E%20instead%20of%20%3CEM%3EInvoke-SSHCommand%3C%2FEM%3E%26nbsp%3B%20returned%20all%20the%20results%20I%20wanted.%3C%2FP%3E%3CP%3EI%20need%20to%20tweak%20the%20final%20script%2C%20but%20these%20are%20the%20commands%20I%20used%3A%3C%2FP%3E%3CDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24Credentials%26nbsp%3B%3D%26nbsp%3BGet-Credential%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24session%26nbsp%3B%3D%26nbsp%3BNew-SSHSession%26nbsp%3B-ComputerName%20x%3C%2FSPAN%3E%3CSPAN%3E.x%3C%2FSPAN%3E%3CSPAN%3E.x%3C%2FSPAN%3E%3CSPAN%3E.x%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Credential%26nbsp%3B%24Credentials%26nbsp%3B-AcceptKey%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24SSHStream%26nbsp%3B%3D%26nbsp%3BNew-SSHShellStream%26nbsp%3B-SessionId%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E0%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24SSHStream.WriteLine(%3C%2FSPAN%3E%3CSPAN%3E%22show%26nbsp%3Binterface%22%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24SSHStream.read()%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EI%20hope%20this%20may%20be%20of%20some%20help!%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EThanks%20again%20for%20your%20help%2C%20I%20really%20appreciate%20that!%20And%20thanks%20to%20the%20creator%20of%20%22Posh-SSH%22%20module!%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1198862%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1198862%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F429537%22%20target%3D%22_blank%22%3E%40fstorer%3C%2FA%3E%26nbsp%3BAwesome%2C%20thanks%20for%20the%20update.%20So%20we%20need%20to%20push%20commands%20that%20way%20whenever%20the%20command%20has%20params%2C%20good%20to%20know.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1204607%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1204607%22%20slang%3D%22en-US%22%3E%3CP%3EDear%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnfortunately%2C%20I%20am%20still%20having%20issues%20with%20the%20output...%3C%2FP%3E%3CP%3EI%20want%20to%20send%20the%20output%20of%20the%20%22%24SSHStream.read()%22%20to%20a%20txt%20file%2C%20but%20when%20I%20run%20the%20command%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3EOut-File%26nbsp%3B-FilePath%26nbsp%3B%22.%5CAPInterfaces.txt%22%26nbsp%3B-InputObject%26nbsp%3B%24SSHStream.read()%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EI%20got%20a%20blank%20txt%20file.%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EAny%20suggestions%3F%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1205925%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1205925%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20reply!%3C%2FP%3E%3CP%3EI%20managed%20to%20make%20it%20work%2C%20but%20I%20had%20to%20add%20the%20%22Start-Sleep%22%20command%20between%20the%20WriteLine%20and%20the%20SSHStream.read%2C%20otherwise%20it%20didn't%20work.%20So%20here%20is%20the%20script%20that%20worked%20for%20me%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3E%24Credentials%26nbsp%3B%3D%26nbsp%3BGet-Credential%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24session%26nbsp%3B%3D%26nbsp%3BNew-SSHSession%26nbsp%3B-ComputerName%20x%3C%2FSPAN%3E%3CSPAN%3E.x%3C%2FSPAN%3E%3CSPAN%3E.x%3C%2FSPAN%3E%3CSPAN%3E.x%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Credential%26nbsp%3B%24Credentials%26nbsp%3B-AcceptKey%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24SSHStream%26nbsp%3B%3D%26nbsp%3BNew-SSHShellStream%26nbsp%3B-SessionId%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E0%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24SSHStream.WriteLine(%3C%2FSPAN%3E%3CSPAN%3E%22show%26nbsp%3Binterface%22%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EStart-Sleep%204%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24OutputVar%26nbsp%3B%3D%26nbsp%3B%24(%24SSHStream.read())%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%24OutputVar%26nbsp%3B%7C%26nbsp%3BOut-File%26nbsp%3B-FilePath%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%22.%5CAPInterfaces.txt%22%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Encoding%26nbsp%3Butf8%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EThanks%20again%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%26nbsp%3B%20for%20all%20your%20help!%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1204742%22%20slang%3D%22en-US%22%3ERe%3A%20Retrieving%20information%20from%20multiple%20Access%20Points%20via%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1204742%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F429537%22%20target%3D%22_blank%22%3E%40fstorer%3C%2FA%3E%26nbsp%3BHello%2C%20you%20got%20that%20as%20once%20you%20run%20the%20read%20from%20the%20stream%20you%20reset%20the%20variable%20to%20no%20data.%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20tested%20this%20saving%20the%20read%20output%20to%20a%20new%20Variable%20and%20it%20is%20working%20for%20me%2C%20please%20test%20it%20and%20let%20me%20know%20the%20outcome.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%24session%20%3D%20New-SSHSession%20-ComputerName%20%24AP.IP%20-Credential%20%24Credentials%20-AcceptKey%0A%24SSHStream%20%3D%20New-SSHShellStream%20-SessionId%20%24session.SessionId%0A%24SSHStream.WriteLine(%22show%20interface%22)%0A%0A%24OutputVar%20%3D%20%20%24null%0A%24OutputVar%20%3D%20%24(%24SSHStream.read())%0A%24OutputVar%20%0A%0A%24OutputVar%20%7C%20Out-File%20-FilePath%20%22%24WorkingDir%5CAPInterfaces.txt%22%20-Encoding%20utf8%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%20Moreno%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello everyone!

 

I am doing a WiFi site survey and I would need to retrieve some information from all the Access Points installed in our school (around 136). I wonder if it would be feasible to write a Powershell script which should be able to:

- Connect to all the Aerohive Access Points via SSH (from a list of IP addresses)
- Run a specific command for each AP (e.g. "show interface")
- Export the results of that command to a txt/html file

My purpose is getting all the different MAC addresses used by each AP and listing them on a txt/html file.

I have been told that I could use a Python module (Aeromiko) to extract data from the Access Points but I am not familiar with the Python language. 

Could you please advise on this?

 

Thank you in advance!

8 Replies

@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

 

 

Dear@Erick A. Moreno R.

 

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 : 255

 

For 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?

 

 

 

 

 

 

 

 

 

@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 

Hi @Erick A. Moreno R.

 

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!

@fstorer Awesome, thanks for the update. So we need to push commands that way whenever the command has params, good to know. 

 

Regards

Erick

Dear @Erick A. Moreno R. 

 

Unfortunately, I am still having issues with the output...

I want to send the output of the "$SSHStream.read()" to a txt file, but when I run the command:

 

Out-File -FilePath ".\APInterfaces.txt" -InputObject $SSHStream.read()
 
I got a blank txt file. 
Any suggestions?

@fstorer Hello, you got that as once you run the read from the stream you reset the variable to no data. 

I've tested this saving the read output to a new Variable and it is working for me, please test it and let me know the outcome.

 

$session = New-SSHSession -ComputerName $AP.IP -Credential $Credentials -AcceptKey
$SSHStream = New-SSHShellStream -SessionId $session.SessionId
$SSHStream.WriteLine("show interface")

$OutputVar =  $null
$OutputVar = $($SSHStream.read())
$OutputVar 

$OutputVar | Out-File -FilePath "$WorkingDir\APInterfaces.txt" -Encoding utf8 

 

Regards

Erick Moreno

@Erick A. Moreno R.

 

Thank you for your reply!

I managed to make it work, but I had to add the "Start-Sleep" command between the WriteLine and the SSHStream.read, otherwise it didn't work. So here is the script that worked for me:

 

$Credentials = Get-Credential
$session = New-SSHSession -ComputerName x.x.x.x -Credential $Credentials -AcceptKey
$SSHStream = New-SSHShellStream -SessionId 0
$SSHStream.WriteLine("show interface")
Start-Sleep 4
$OutputVar = $($SSHStream.read())
$OutputVar | Out-File -FilePath ".\APInterfaces.txt" -Encoding utf8
 
Thanks again @Erick A. Moreno R.  for all your help!