CLR检测到带有powershell的无效程序



我正试图将Powershell脚本发送到服务器并执行它。服务器收到了该脚本,但当我试图创建PS实例时,它崩溃了。我已经四处寻找模拟案例,但大多数时候似乎都是特定的。

public void processMsg(TcpClient client, NetworkStream stream, byte[] bytesReceived, int length)
        {    
            mstrMessage = Encoding.ASCII.GetString(bytesReceived, 0, bytesReceived.Length);
            mscClient = client;
            mstrMessage = mstrMessage.Substring(0, length);
            Console.WriteLine(mstrMessage);
            if (mstrMessage.Length > 8)
            {
                if (mstrMessage.Substring(0, 8) == "%SCRIPT%")
                {
                    Console.WriteLine("Script recieved.");
                    try
                    {
  • 在下面的行中,我得到了以下异常:公共语言运行时检测到一个无效程序。

                        PowerShell powerShellInstance = PowerShell.Create();
                        using (powerShellInstance)
                        {
                            powerShellInstance.AddScript(mstrMessage);
                            powerShellInstance.Invoke();
                            Console.WriteLine("Script executed.");
                        }
                    }
                    catch (InvalidProgramException)
                    {
                        throw;
                    }
                }
            }
    

重要的部分是:A)您使用哪个版本的System.Management.Automation.dll进行编译,B)您使用什么版本的.NET完成编译,以及C)服务器上安装了什么版本的PowerShell和.NET。

您的服务器可能运行的PowerShell版本已过时,因此您编译的内容与服务器上可用的内容不同。

最新更新