事件处理程序 Powershell GUI 内的 Posh-SSH 模块的 Invoke-SSHCommand 没有输出

问题描述 投票:0回答:1

我有一个问题是我没有从 .Add_SelectedIndexChanged 内的 Invoke-SSHCommand 获得输出。我需要这个输出来检查 tmux 会话是否正在我的 Linux 机器上运行。

#executes commands on server via SSH-posh
$Creds = Get-Credential
$global:Session = New-SSHSession -ComputerName myip -Credential $Creds -AcceptKey
$ServerAmount = Invoke-SSHCommand -SSHSession $global:Session -Command "ls /mnt/server"


#This is giving me a output
$ActiveServer = Invoke-SSHCommand -SSHSession $global:Session -Command "tmux ls"
$ActiveServer.Output 



#Adds all the server directories to combobox
foreach ($server in $ServerAmount.Output){
    if ($server -ne "lost+found"){
        $ddlServer.Items.Add($server)
    }     
}

#On Selected Item changed do this: 
$ddlServer.Add_SelectedIndexChanged({
    

    $txbOutput.Text = ""
    $SelectedServer = $ddlServer.SelectedItem
    $txbOutput.AppendText($SelectedServer)

    #Execute tmux ls to see what servers are running
    

    #this is not giving me any output
    $ActiveServer = Invoke-SSHCommand -SSHSession $global:Session -Command "tmux ls"
    $ActiveServer.Output
    

    if ($SelectedServer -match $ActiveServer.Output) {
        $txbOutput.AppendText("Server Status: Up")
    }

    else {
        $txbOutput.AppendText("Server Status: Down")
    }
})

我尝试将

$ddlServer.Add_SelectedIndexChanged
内的所有内容都变成一个函数,然后在
$ddlServer.Add_SelectedIndexChanged
内运行。这没有什么区别。我不认为这可能是任何权限/连接问题,因为它可以正常工作,并且我已确保“tmux ls”有实际输出。我尝试将事件处理程序切换到不同的事件处理程序,即“.Add_Click”,但这似乎也不起作用。

powershell user-interface output posh-ssh
1个回答
0
投票

我不知道是什么让它起作用,但这起作用了:

# Execute commands or perform operations over SSH
$output = Invoke-SSHCommand -SessionId $global:Session.SessionId -Command "tmux ls"



# Extract only the server name
$ActiveServer = ""
$output.Output | ForEach-Object {
    $serverName = $_ -split ':', 2   # Split the line at the colon and take the first part
    $ActiveServer += $serverName[0] + "`r`n"  # Append the extracted server name along with a newline character
}
© www.soinside.com 2019 - 2024. All rights reserved.