我创建了一个为用户创建/删除交换帐户的 C# 控制台应用程序。我在开发机器上安装了“Microsoft Server Exchange 2010”。伪代码如下所示:
public Runspace getRunspace()
{
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException;
PSSnapInInfo snapinInfo = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);
runspace.Open();
return runspace;
}
public void createMailBox(string login)
{
PowerShell powershell = PowerShell.Create();
powershell.Runspace = getRunspace();
PSCommand command = new PSCommand();
command.AddCommand("Enable-Mailbox");
command.AddParameter("Identity", login);
command.AddParameter("Database", "Database01");
powershell.Commands = command;
powershell.Invoke();
powershell.Dispose();
}
public void deleteMailBox(string login)
{
PowerShell powershell = PowerShell.Create();
powershell.Runspace = getRunspace();
PSCommand command = new PSCommand();
command.AddCommand("Disable-Mailbox");
command.AddParameter("Identity", login);
command.AddParameter("Confirm", false);
powershell.Commands = command;
try
{
powershell.Invoke();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
powershell.Dispose();
}
问题是 createMailBox 方法创建交换帐户没有任何问题,但 deleteMailBox 方法不会禁用 Exchange 帐户,也不会引发任何错误/异常。
我怀疑问题出在“确认”参数上。我试过了
powershell.AddScript("Disable-Mailbox -Identity " + login + " -Confirm:$false")
但这也行不通。