Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CLR检测到powershell中的程序无效_C#_Powershell_Clr - Fatal编程技术网

C# CLR检测到powershell中的程序无效

C# CLR检测到powershell中的程序无效,c#,powershell,clr,C#,Powershell,Clr,我正在尝试将Powershell脚本发送到服务器并执行它。服务器接收到脚本,但当我尝试创建PS实例时,它崩溃了。我已经到处找了很多类似的案例,但大多数时候似乎都很具体 public void processMsg(TcpClient client, NetworkStream stream, byte[] bytesReceived, int length) { mstrMessage = Encoding.ASCII.GetString(byte

我正在尝试将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,因此您编译的内容与服务器上可用的内容不同。

我不知道如何以这种方式调用PowerShell,但是:这是否与需要不同的.NET Framework版本有关?例如,您是否以2.0运行,但Powershell正在尝试使用4.0中的某些内容?@DarkFalcon我正在将Powershell 3.0与4.5.1框架一起使用。检查此链接:看起来我做得不错。@yavolo编译主机应用程序时使用的System.Management.Automation.dll的版本是(1.0还是3.0)?该应用程序是根据-4.5.1编译的.NET的哪个版本?服务器上安装了什么版本的.NET?@yavolo在您引用的链接上,您是否看到此警告
,但是,由于Microsoft.NET Framework 4.0、Windows PowerShell主机程序中运行时激活策略的更改,这些程序是为Windows PowerShell 2.0编写的,并使用公共语言运行时(CLR)编译的Windows PowerShell 3.0是用CLR 4.0编译的,如果不进行修改,2.0将无法运行。
@KeithHill如果您是正确的,问题是Windows管理框架已经过时。当我安装3.0时,它工作得非常好。如果你想回答,我会记下来。:)很抱歉对一个问题提出了一个旧的答案,但是如果您必须支持PowerShell v2+,如何使用System.Management.Automation?我想根据.NET 4.0(在服务器上保证)进行编译,并将System.Management.Automation包含在任何版本的PowerShell 2或更高版本中。我的选项是什么?@ferventcoder如果需要支持v2,则必须根据SMA.dll的1.0版本进行编译。您应该能够在4.0中运行PS引擎。如果你想要支持v3+,请查看这篇博文-谢谢!找到1.0 SMA可能很有趣。我认为,如果人们同意自动将电脑升级到豪华v3,那就不会有问题。不过,大多数人都不会同意的。