我正在尝试执行以下操作:
StringBuilder errorList = new StringBuilder();
RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
PSSnapInException snapEx = null;
PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);
Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
runSpace.Open();
Pipeline pipeLine = runSpace.CreatePipeline();
我收到以下错误:
尚未为 Windows PowerShell 版本 2 注册任何管理单元。
我是 PoweShell 的新手,不确定该错误的确切含义。这是我需要安装的东西吗?
编辑:完整代码
///
/// Creates mailbox for the given user.
///
/// Email address of user.
public void EnableMailbox(string userEmail)
{
StringBuilder errorList = new StringBuilder();
RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
PSSnapInException snapEx = null;
PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);
Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
runSpace.Open();
Pipeline pipeLine = runSpace.CreatePipeline();
if (!MailBoxAlreadyExist(userEmail, runSpace))
{
Command createMailbox = new Command("Enable-Mailbox");
createMailbox.Parameters.Add("identity", userEmail);
createMailbox.Parameters.Add("database", "Mailbox Database Name");
pipeLine.Commands.Add(createMailbox);
pipeLine.Invoke();
if (pipeLine.Error != null && pipeLine.Error.Count > 0)
{
foreach (object item in pipeLine.Error.ReadToEnd())
{
errorList.Append(item.ToString());
errorList.Append(System.Environment.NewLine);
}
Console.WriteLine(errorList.ToString());
}
}
else
{
Console.WriteLine("Mailbox of user " + userEmail + " already exists on exchange server.");
}
pipeLine.Dispose();
runSpace.Close();
runSpace.Dispose();
}
最佳答案
32 位和 64 位管理单元之间存在区别。 Echange 可能只是 32 位的,在这种情况下,将您的 C# 项目设置为目标平台 x86。如果 Exchange 是 64 位,则仅将 C# 项目设置为目标平台 x64。
关于c# - C# 中的 PowerShell 管理单元问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811754/