简体   繁体   English

从C#禁用邮箱cmdlet

[英]Disable-Mailbox cmdlet from C#

I have created a C# Console Application that Creates/Deletes exchange accounts for a user. 我创建了一个C#控制台应用程序,该应用程序为用户创建/删除交换帐户。 I have "Microsoft Server Exchange 2010" installed on the dev machine. 我在开发机器上安装了“ Microsoft Server Exchange 2010”。 Psuedo code looks like: 伪代码看起来像:

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();
}

Issue is the createMailBox method creates exchange accouts without any issues, but deleteMailBox method doesn't disables the Exchange account and neither it throws any error/exception. 问题是createMailBox方法创建交流帐户时没有任何问题,但是deleteMailBox方法不会禁用Exchange帐户,也不会引发任何错误/异常。

Problem I suspect is with the 'Confirm' parameter. 我怀疑问题与“确认”参数有关。 I have tried 我努力了

powershell.AddScript("Disable-Mailbox -Identity " + login + " -Confirm:$false")

But this ain't working either. 但这也不起作用。

检查错误流中是否存在错误。

powerShell.Streams.Error 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM