繁体   English   中英

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

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

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

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

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

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

两种方法后,变量$result为空

这对我很有用:

# 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")

这对我有用:

#method 1
$CurrentSession=New-SSHSession -ComputerName x.x.x.x -Credential (Get-Credential) 
$result = Invoke-SSHCommand -SSHSession $CurrentSession -Command "ls /somepath"
$result.Output

-OutVariable $result删除$ ,它应该可以工作。

Invoke-SSHCommand -Index 0 -Command "ls /somepath" -OutVariable result

正在处理命令中的$result ,返回null,然后尝试将Invoke-SSHCommand的结果输出为null,据我所知

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM