Forum Discussion
Retrieving information from multiple Access Points via PowerShell
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:
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
- MadhurimaSep 06, 2021Copper Contributor
Dear @Erick A. Moreno R
Thank you for the above script. It was insightful and helped me achieved a goal.
I'm at present working on another project where I have automated the script using kshell. But now I'm trying run the script from Windows PowerShell.
Below is the script:
#! /bin/ksh
Variable= install_dir/xyz.cfg
Export Variable
$install_dir/pmgr -i $install_dir/abc/init.cfg
echo "application started"
Sleep 2
Ps - ef|grep pmgr
Echo $!
In PowerShell, I'm able to check the status of the application using invoke-sshcommand -sessionid 0 -command "ps -ef|grep pmgr
It gives the status of the application, but need to start the service using PowerShell. Could you please help? - fstorerMar 03, 2020Brass Contributor
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 utf8Thanks again Erick A. Moreno R. for all your help!