简体   繁体   English

使用C#禁用ActiveSync

[英]Disable ActiveSync using C#

I am trying to use C# to disable ActiveSync mailboxes on Exchange Server 2007 (and soon to be 2013 which is probably completely different) using powershell. 我正在尝试使用C#使用Powershell在Exchange Server 2007上禁用ActiveSync邮箱(很快将在2013年,这可能是完全不同的)。 The first command works to set the Allowed Device IDs. 第一个命令用于设置允许的设备ID。 The second to turn off activesync does not. 第二个没有关闭activesync的。 Am I specifying it incorrectly? 我指定不正确吗?

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

I don't have much experience with C# and PowerShell or the Exchange PS cmdlets for that matter, so I might be wrong here. 我在C#和PowerShell或Exchange PS cmdlet方面没有太多经验,因此在这里我可能是错的。

AFAIK your sample would equal: 相信您的样本将等于:

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

You're not using the object (if any) that the first cmdlet returns, so they don't belong in a pipeline. 您没有使用第一个cmdlet返回的对象(如果有),因此它们不属于管道。 You should run them separately, like: 您应该分别运行它们,例如:

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

In C# I guess you need to call Invoke() twice. 在C#中,我猜您需要Invoke()两次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();

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

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