Powershell posh-ssh:如何存储Invoke-SSHCommand的结果?

Ada*_*dam 2 ssh powershell

Invoke-SSHCommand关闭连接后,如何存储使用结果?

我尝试了两种不同的方式:

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

#method 2
Invoke-SSHCommand -Index 0 -Command "ls /somepath" -OutVariable $result
Run Code Online (Sandbox Code Playgroud)

$result两种方法后变量都是空的

Ran*_*tta 6

这对我很有用:

# 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")
Run Code Online (Sandbox Code Playgroud)