如何在关闭连接后存储Invoke-SSHCommand
的结果以供使用?
我尝试了两种不同的方式:
#method 1
$result = Invoke-SSHCommand -Index 0 -Command "ls /somepath"
#method 2
Invoke-SSHCommand -Index 0 -Command "ls /somepath" -OutVariable $result
两个方法
后,变量$result
为空
答案 0 :(得分: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")
答案 1 :(得分:2)
这对我有用:
#method 1
$CurrentSession=New-SSHSession -ComputerName x.x.x.x -Credential (Get-Credential)
$result = Invoke-SSHCommand -SSHSession $CurrentSession -Command "ls /somepath"
$result.Output
答案 2 :(得分:0)
从$
中删除-OutVariable $result
,它应该有效。
Invoke-SSHCommand -Index 0 -Command "ls /somepath" -OutVariable result
正在处理命令中的$result
,返回null,然后尝试将Invoke-SSHCommand的结果输出为null,据我所知