Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell posh-ssh: How to store results of Invoke-SSHCommand?

Tags:

powershell

ssh

How can I store the result of Invoke-SSHCommand for use after I close the connection?

I've tried 2 different ways:

#method 1
$result = Invoke-SSHCommand -Index 0 -Command "ls /somepath"

#method 2
Invoke-SSHCommand -Index 0 -Command "ls /somepath" -OutVariable $result

The variable $result is empty after both methods

like image 862
Adam Avatar asked Dec 10 '22 13:12

Adam


1 Answers

This worked well for me :

# Create SSH Session
$ssh = New-SSHSession -ComputerName $PC01 -Credential $credential -AcceptKey 1

# Invoke SSH command and capture output as string (you can return the full object if you like, I just needed the string Out)
$ret = $(Invoke-SSHCommand -SSHSession $ssh -Command "racadm getconfig -g cfgUserAdmin -i 2").Output

# return object is a String - if you want it as an array of strings for each row returned
$ret = $ret.split("`n")
like image 169
Ranadip Dutta Avatar answered Dec 21 '22 17:12

Ranadip Dutta