【问题标题】:Powershell runspace instantiate error: System.Management.Automation not foundPowershell 运行空间实例化错误:找不到 System.Management.Automation
【发布时间】:2019-10-31 03:21:21
【问题描述】:

我正在尝试从 .NET 核心 api 向 Exchange 发出一些远程请求,但我遇到了一些问题,我无法找到解决方案。

我创建了一个单例模式,因此每个用户只能有一个 powershell 会话,并且每当我必须拨打交换电话时,我都会检索该会话。问题是当我尝试创建运行空间时收到 System.IO.FileNotFoundException' en System.Management.Automation.dll 错误并且我无法运行任何命令。

我在 Windows 10 的本地 IIS 中托管我的 api,并且我使用 .NET Core 2.2。如果我直接向 IIS Express 发出请求,则在 Visual Studio 中运行我的代码时,我没有收到任何错误,但当我向 IIS 站点发出请求时,我确实收到了异常。

任何帮助将不胜感激!!!

谢谢!!!

我尝试在本地创建运行空间并连接到 Exchange,但我也尝试远程连接并保存该配置,因此我只需要导入会话。

我们也尝试将 .net 核心更新到 3 版本,但没有成功。

// this is how I retrive the sessíon from the singleton --
public Powershell GetSession(string usuario, string password)
{
    PSCredential credenciales = Powershell.CrearCredenciales(usuario, password);
    Powershell powershell;
    if (diccionarioSesionesPowershell.ContainsKey(credenciales))
    {
        powershell = diccionarioSesionesPowershell[credenciales];
        bool sesionValida = powershell.EsValida();
        if (!sesionValida)
        {
            powershell.InicializarRunspace();
        }
    }
    else
    {
        powershell = new Powershell(credenciales, Logger);
        powershell.CrearRunspace();
        diccionarioSesionesPowershell.Add(credenciales, powershell);
    }
    return powershell;
}





// this is the singleton config in startup
services.AddSingleton<PilaPowershell>();




// this is how I save my remote session so I can import it later
private Runspace Runspace{get; set;}

public bool InicializarRunspace()
{
    bool resultado = false;
    try
    {
        Runspace = RunspaceFactory.CreateRunspace();-->this is where I get the error
        Runspace.Open();

        // Create a powershell session for remote exchange server
        using (var powershell = PowerShell.Create())
        {
            var command = new PSCommand();
            command.AddCommand("New-PSSession");
            command.AddParameter("ConfigurationName", "Microsoft.Exchange");
            command.AddParameter("ConnectionUri", new Uri("https://outlook.office365.com/powershell-liveid/"));
            command.AddParameter("Authentication", "Basic");
            command.AddParameter("Credential", Credenciales);
            powershell.Commands = command;
            powershell.Runspace = Runspace;

            var result = powershell.Invoke();
            SesionesPS = result[0];
        }

        // Set ExecutionPolicy on the process to unrestricted
        using (var powershell = PowerShell.Create())
        {
            var command = new PSCommand();
            command.AddCommand("Set-ExecutionPolicy");
            command.AddParameter("Scope", "Process");
            command.AddParameter("ExecutionPolicy", "Unrestricted");
            powershell.Commands = command;
            powershell.Runspace = Runspace;

            powershell.Invoke();
        }

        // Import remote exchange session into runspace
        using (var powershell = PowerShell.Create())
        {
            var command = new PSCommand();
            command.AddCommand("Import-PSSession");
            command.AddParameter("Session", SesionesPS);
            powershell.Commands = command;
            powershell.Runspace = Runspace;

            powershell.Invoke();
        }
        resultado = true;
    }
    catch (Exception ex)
    {
        Logger.LogError(ex, "Powershell error when creating runspace: ");
        resultado = false;
    }
    return resultado;
}





//this is how I try to create the runspace and connect remotelly - I do open and close the runspace when I run my commands
public void CrearRunspace()
{
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(LiveId), SchemaUri, Credenciales);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
    Runspace =  RunspaceFactory.CreateRunspace(connectionInfo);-->this is where I get the error
}

【问题讨论】:

  • 您不需要将Using System.Management.Automation 添加到您的代码中吗?然后还将System.Management.Automation.dll 添加为参考程序集。
  • @RohinSidharth 谢谢你的回答。我确实有 using 语句和引用,但仍然没有运气:(

标签: powershell asp.net-core exchange-server powershell-2.0 powershell-remoting


【解决方案1】:

我找到了解决问题的方法。当我创建 api 时,我使用 nuget 下载了 powershell sdk,但是当我使用该 SDK 发布它时,IIS 无法访问所需的依赖项。所以我卸载了 SDK,而是在我的 PC 上安装了 powershell 6 并链接了程序集中的依赖项,它工作了!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多