【问题标题】:Disable ActiveSync using C#使用 C# 禁用 ActiveSync
【发布时间】:2015-02-15 09:01:22
【问题描述】:

我正在尝试使用 C# 使用 powershell 禁用 Exchange Server 2007 上的 ActiveSync 邮箱(很快就会是 2013 年,这可能完全不同)。第一个命令用于设置允许的设备 ID。第二个关闭 activesync 没有。我是否指定错误?

RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

PSSnapInException PSException = null;
PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

Command command = new Command("Set-CASMailbox");

command.Parameters.Add("Identity", username);
command.Parameters.Add("ActiveSyncAllowedDeviceIDs", "\"BLOCKED\"");
pipeline.Commands.Add(command);

command = new Command("Set-CASMailbox");
command.Parameters.Add("Identity", username);
command.Parameters.Add("ActiveSyncEnabled", false);

pipeline.Commands.Add(command);

Collection<PSObject> result = pipeline.Invoke();

【问题讨论】:

    标签: c# powershell exchange-server-2007 activesync


    【解决方案1】:

    我在 C# 和 PowerShell 或 Exchange PS cmdlet 方面没有太多经验,所以我在这里可能错了。

    AFAIK 您的样本将等于:

    Set-CASMailbox -Identity 'User1' -ActiveSyncAllowedDeviceIDs "BLOCKED" | Set-CASMailbox -Identity 'User1' -ActiveSyncEnabled $false
    

    您没有使用第一个 cmdlet 返回的对象(如果有),因此它们不属于管道。您应该单独运行它们,例如:

    Set-CASMailbox -Identity 'User1' -ActiveSyncAllowedDeviceIDs "BLOCKED"
    Set-CASMailbox -Identity 'User1' -ActiveSyncEnabled $false
    

    在 C# 中,我猜你需要调用 Invoke() 两次。

    Pipeline pipeline = runspace.CreatePipeline();
    
    Command command = new Command("Set-CASMailbox");
    command.Parameters.Add("Identity", username);
    command.Parameters.Add("ActiveSyncAllowedDeviceIDs", "BLOCKED");
    pipeline.Commands.Add(command);
    
    //Run first cmdlet
    Collection<PSObject> result = pipeline.Invoke();
    
    //Not sure how to reset. Create new pipeline?
    Pipeline pipeline2 = runspace.CreatePipeline();
    
    Command command2 = new Command("Set-CASMailbox");
    command2.Parameters.Add("Identity", username);
    command2.Parameters.Add("ActiveSyncEnabled", false);
    pipeline2.Commands.Add(command);
    
    //Run second cmdlet
    Collection<PSObject> result2 = pipeline2.Invoke();
    

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2010-09-24
      • 1970-01-01
      • 2010-11-12
      • 2011-12-21
      • 2012-01-31
      • 2017-10-25
      • 1970-01-01
      • 2016-12-25
      相关资源
      最近更新 更多