[英]Disable ActiveSync using C#
我正在尝试使用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 PS cmdlet方面没有太多经验,因此在这里我可能是错的。
相信您的样本将等于:
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()
两次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.