wpf应用程序在开发它的计算机上运行良好。来自bin / Release的文件用于运行应用程序。 System.Management.Automation.dll用于在Exchange Server 2010上远程处理PowerShell以创建新邮箱。使用Local = True进行设置,以便将dll添加到bin / release中的文件中。
但是,如果bin / release中的相同文件被复制到另一台计算机上,则Active Directory上的所有功能都正常工作,但邮箱创建部分除外,因为它给出错误:公共语言运行时检测到无效程序。
我可以找到一些建议来取消选中'优化代码'并在VS中重建它,但似乎没有帮助。
正如我之前所说,在开发人员的计算机上工作得很好。因此,我在另一台安装了VS 2013的计算机上运行该应用程序并收到错误:"术语“启用邮箱”#39;不被识别为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());
}
}
}
答案 0 :(得分:1)
看起来这可能是一些事情,很难知道你的场景中的原因是什么。首先,我会尝试以管理员身份运行,但未能尝试这些事情:
重新安装.net框架(确保版本正确)
Common Language Runtime detected an invalid program in Visual Studio
对于C#项目,转到项目属性,在Build选项卡下取消选中“Optimize Code”。
Common Language Runtime detected an invalid program?
尝试在应用程序池高级设置中启用32位应用程序。
或
我终于设法解决了这个问题。我取消选中代码优化 在C#Express中解决了这些问题。
InvalidProgramException / Common Language Runtime detected an invalid program
答案 1 :(得分:0)
它已经解决了。 PowerShell脚本的权限是远程使用cmdlet命令。
编辑:虽然可能无法创建邮箱的情况,但此错误发生在具有不同配置的另一台计算机上。
在我们收到“公共语言运行时错误”的计算机上,问题是PowerShell的旧版本。
答案 2 :(得分:0)
我在Xamarin Forms应用程序中遇到了这种情况,因为我在一个不存在的事件上有一个按钮单击事件。从按钮中删除click事件解决了该问题。
答案 3 :(得分:0)
我遇到了此问题,并在“项目”>“属性”>“生成”中选中了“优化代码”,并正常工作,然后重新发布,并且未选中“优化代码”,然后又可以正常工作。