C# 公共语言运行库检测到无效程序

C# 公共语言运行库检测到无效程序,c#,wpf,powershell,C#,Wpf,Powershell,wpf应用程序在其开发的计算机上运行良好。bin/Release中的文件用于运行应用程序。 System.Management.Automation.dll用于在Exchange Server 2010上远程使用powershell创建新邮箱。它的设置为Local=True,因此dll被添加到bin/release中的文件中 但是,如果将bin/release中的相同文件复制到另一台计算机上,则除邮箱创建部分外,Active Directory上的所有功能都工作正常,因为它会出现错误:公共语言运

wpf应用程序在其开发的计算机上运行良好。bin/Release中的文件用于运行应用程序。 System.Management.Automation.dll用于在Exchange Server 2010上远程使用powershell创建新邮箱。它的设置为Local=True,因此dll被添加到bin/release中的文件中

但是,如果将bin/release中的相同文件复制到另一台计算机上,则除邮箱创建部分外,Active Directory上的所有功能都工作正常,因为它会出现错误:公共语言运行库检测到无效程序

我可以找到一些建议,比如取消选中“优化代码”并在VS中重建它,但似乎没有帮助

正如我之前所说的,在开发者的计算机上一切都很好。因此,我在另一台也安装了VS 2013的计算机上运行了该应用程序,但出现了一个错误:“术语‘启用邮箱’无法识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。” 代码如下:

public static class  ManageMailBox
{
    public static bool CreateMailBox(string strUserID, string admin_user, string pword) //, string str_upn)
    {
        try
        {
            string strExchangeServer = Constants.ExchangeServer; 
            Uri uri = new Uri(@"http://" + strExchangeServer + @"/powershell?serializationLevel=Full"); // orig works

            /// must pass secure string
            char[] passwordChars = pword.ToCharArray();
            SecureString password = new SecureString();
            foreach (char c in passwordChars)
            {
                password.AppendChar(c);
            }
            PSCredential credential = new PSCredential("DOMAIN\\" + admin_user, password);
            Runspace runspace = RunspaceFactory.CreateRunspace();
            PowerShell powershell = PowerShell.Create();
            PSCommand command = new PSCommand();

            command.AddCommand("New-PSSession");
            command.AddParameter("ConfigurationName", "Microsoft.Exchange");
            command.AddParameter("ConnectionUri", uri);
            command.AddParameter("Credential", credential);
            command.AddParameter("Authentication", "Default");
            PSSessionOption sessionOption = new PSSessionOption();
            sessionOption.SkipCACheck = true;
            sessionOption.SkipCNCheck = true;
            sessionOption.SkipRevocationCheck = true;
            command.AddParameter("SessionOption", sessionOption);

            powershell.Commands = command;
            try
            {
                // open the remote runspace
                runspace.Open();

                // associate the runspace with powershell
                powershell.Runspace = runspace;

                // invoke the powershell to obtain the results
                Collection<PSSession> result = powershell.Invoke<PSSession>();

                foreach (ErrorRecord current in powershell.Streams.Error)
                {
                    throw new Exception("Exception: " + current.Exception.ToString());
                    throw new Exception("Inner Exception: " + current.Exception.InnerException);
                }

                if (result.Count != 1)
                    throw new Exception("Unexpected number of Remote Runspace connections returned.");

                // Set the runspace as a local variable on the runspace
                powershell = PowerShell.Create();
                command = new PSCommand();
                command.AddCommand("Set-Variable");
                command.AddParameter("Name", "ra");
                command.AddParameter("Value", result[0]);
                powershell.Commands = command;
                powershell.Runspace = runspace;

                powershell.Invoke();

                // First import the cmdlets in the current runspace (using Import-PSSession)
                powershell = PowerShell.Create();
                command = new PSCommand();
                //command.AddScript("Set-ExecutionPolicy -Unrestricted");
                command.AddScript("Import-PSSession -Session $ra");
                powershell.Commands = command;
                powershell.Runspace = runspace;
                powershell.Invoke();

                // Now run get-ExchangeServer
                powershell = PowerShell.Create();
                command = new PSCommand();

                command.AddCommand("Enable-Mailbox");
                command.AddParameter("Identity", strUserID);
                command.AddParameter("Alias", strUserID);
                command.AddParameter("Database", "IAP Mailbox Database 0948752629");
                powershell.Commands = command;
                powershell.Runspace = runspace;
                powershell.Invoke(); // ERROR !!! The term 'Enable-Mailbox' is not recognized as the name of a cmdlet, function

                return true;
            }
            catch(Exception ex) { 
                    throw new Exception(ex.Message.ToString()); }
            finally
            {
                // dispose the runspace and enable garbage collection
                runspace.Dispose();
                runspace = null;

                // Finally dispose the powershell and set all variables to null to free
                // up any resources.
                powershell.Dispose();
                powershell = null;
            }
        }
        catch (Exception argex)
        {
            throw new ArgumentException(argex.Message.ToString());
        }

    }
}
公共静态类ManageMailBox
{
公共静态bool CreateMailBox(string strUserID,string admin\u user,string pword)/,string str\u upn)
{
尝试
{
字符串strExchangeServer=Constants.ExchangeServer;
Uri=newURI(@“http://“+strExchangeServer+@”/powershell?serializationLevel=Full”);//源代码有效
///必须通过安全字符串
char[]passwordChars=pword.tocharray();
SecureString密码=新SecureString();
foreach(passwordChars中的字符c)
{
密码。AppendChar(c);
}
PSCredential=新的PSCredential(“域\\”+管理员用户,密码);
Runspace Runspace=RunspaceFactory.CreateRunspace();
PowerShell PowerShell=PowerShell.Create();
PSCommand=新PSCommand();
command.AddCommand(“新PSSession”);
command.AddParameter(“ConfigurationName”、“Microsoft.Exchange”);
AddParameter(“ConnectionUri”,uri);
AddParameter(“凭证”,凭证);
AddParameter(“身份验证”、“默认值”);
PSSessionOption sessionOption=新的PSSessionOption();
sessionOption.SkipCACheck=true;
sessionOption.skipncheck=true;
sessionOption.skiprovocationcheck=true;
AddParameter(“SessionOption”,SessionOption);
powershell.Commands=command;
尝试
{
//打开远程运行空间
Open();
//将运行空间与powershell关联
powershell.Runspace=运行空间;
//调用powershell以获取结果
收集结果=powershell.Invoke();
foreach(错误记录powershell.Streams.Error中的当前值)
{
抛出新异常(“异常:+current.Exception.ToString());
抛出新异常(“内部异常:+current.Exception.InnerException”);
}
如果(result.Count!=1)
抛出新异常(“返回了意外数量的远程运行空间连接”);
//将运行空间设置为运行空间上的局部变量
powershell=powershell.Create();
command=新PSCommand();
AddCommand(“设置变量”);
AddParameter(“名称”、“ra”);
AddParameter(“值”,结果[0]);
powershell.Commands=command;
powershell.Runspace=运行空间;
powershell.Invoke();
//首先在当前运行空间中导入cmdlet(使用import-PSSession)
powershell=powershell.Create();
command=新PSCommand();
//AddScript(“Set ExecutionPolicy-Unrestricted”);
AddScript(“导入PSSession-会话$ra”);
powershell.Commands=command;
powershell.Runspace=运行空间;
powershell.Invoke();
//现在运行GetExchangeServer
powershell=powershell.Create();
command=新PSCommand();
AddCommand(“启用邮箱”);
AddParameter(“标识”,strUserID);
AddParameter(“别名”,strUserID);
AddParameter(“数据库”,“IAP邮箱数据库0948752629”);
powershell.Commands=command;
powershell.Runspace=运行空间;
powershell.Invoke();//错误!!!术语“启用邮箱”未被识别为cmdlet函数的名称
返回true;
}
捕获(例外情况除外){
抛出新异常(例如Message.ToString());}
最后
{
//处置运行空间并启用垃圾回收
Dispose();
运行空间=null;
//最后处置powershell并将所有变量设置为null以释放
//用尽一切资源。
powershell.Dispose();
powershell=null;
}
}
捕获(argex例外)
{
抛出新ArgumentException(argex.Message.ToString());
}
}
}

看起来这可能是一些事情,很难知道您的场景中的原因是什么。首先,我会尝试以管理员的身份运行,如果失败,我会尝试以下内容:


重新安装.net framework(确保版本正确)


对于C#项目,请转到项目属性和构建下的