c# - c# Core System.Management.Automation 远程连接错误
问题描述
我正在尝试编写一个 C# 核心程序来在远程 linux 系统上运行 powershell 脚本。在 .net 核心上运行是该项目的要求。我试图松散地遵循我在CodeProject上找到的指南上找到的指南。
这是我的代码:
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
SSHConnectionInfo connectionInfo = new SSHConnectionInfo(userName: "user", computerName: "server", keyFilePath: "id_rsa");
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open(); // The program errors out here
runspace.Close();
}
}
}
}
我的“id_rsa”文件与程序位于同一文件夹中。我已验证 windows、powershell core 6.0.2 和 .net core 2 SDK 的 openssh 已安装并正常工作。我正在使用Microsoft Powershell Core 存储库中的以下 nuget 包:Microsoft.PowerShell.SDK (6.0.2) 和 Sytem.Managment.Automation (6.0.2)
这是我收到的错误:
Unhandled Exception: System.Management.Automation.Remoting.PSRemotingDataStructureException: An error has occurred which PowerShell cannot handle. A remote session might have ended. ---> System.ArgumentException: The path is not of a legal form.
Parameter name: path
at System.IO.Path.GetDirectoryName(String path)
at System.Management.Automation.Runspaces.SSHConnectionInfo.StartSSHProcess(StreamWriter& stdInWriterVar, StreamReader& stdOutReaderVar, StreamReader& stdErrReaderVar)
at System.Management.Automation.Remoting.Client.SSHClientSessionTransportManager.CreateAsync()
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.SendNegotiationAsync(RemoteSessionState sessionState)
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.HandleStateChanged(Object sender, RemoteSessionStateEventArgs arg)
at System.Management.Automation.ExtensionMethods.SafeInvoke[T](EventHandler`1 eventHandler, Object sender, T eventArgs)
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerStateMachine.RaiseStateMachineEvents()
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerStateMachine.ProcessEvents()
--- End of inner exception stack trace ---
at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
at System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.EndOpen(IAsyncResult asyncResult)
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.Open()
at System.Management.Automation.RemoteRunspace.Open()
at ConsoleApp1.Program.Main(String[] args) in C:\ConsoleApp1\ConsoleApp1\Program.cs:line 18
Press any key to continue . . .
在这一点上,我不确定我错过了什么。
解决方案
我最终在 github 上为这个错误打开了一个问题。当前要解决此问题,您需要将以下代码添加到您的程序中,直到此问题在未来版本的 powershell 核心(>6.0.4 或>6.1.0-rc.1)中得到解决。这是有关该问题的具体帖子。
if (System.Management.Automation.Runspaces.Runspace.DefaultRunspace == null)
{
var defaultRunspace = RunspaceFactory.CreateRunspace();
defaultRunspace.Open();
System.Management.Automation.Runspaces.Runspace.DefaultRunspace = defaultRunspace;
}
推荐阅读
- javascript - 使用谷歌自动完成时是否需要实现会话令牌
- python - 运行可执行文件时,pandas 出现权限错误。PYTHON
- autohotkey - 如何使用 AutoHotKey 获取控件中的文本?
- c# - 从用户控件呈现 WPF LiveChart
- swift - @escaping () -> Void 在 Mojave 中无法正常工作
- .net - IIS 日志未捕获所有请求
- pandas - Python DataFrame:根据存在的行数为列分配值?
- python - Pandas 中的有序数据框值集
- azure-servicebus-queues - 在 Azure 服务总线队列中的 message.abandon() 之后,它是否附加到队列的末尾?
- javascript - 为什么回调最后执行