Powershell运行空间实例化错误:找不到System.Management.Automation

时间:2019-06-17 13:59:25

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

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

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

我将api托管在Windows 10的本地IIS中,并且使用.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
}

1 个答案:

答案 0 :(得分:0)

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