[英]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.