I am working on a solution where users can erase mobile devices registered in Exchange 2010 through a web page using Outlook Web Access is not an option. I installed the Exchange management tools on my development computer, and the application pool uses an identifier that has the necessary rights to execute the commands (the designated group of roles is "Recipient Management"). I use the following code to execute napkins
string deviceId = "deviceid"; string username = "username"; RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException = null; PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException); if(snapInException != null) throw snapInException; using(var runspace = RunspaceFactory.CreateRunspace(new MyPowershellHost(), rsConfig)) { runspace.Open(); using(var pipeline = runspace.CreatePipeline()) { pipeline.Commands.AddScript(@". ""C:\Program files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1"""); pipeline.Commands.AddScript("Connect-ExchangeServer -auto"); pipeline.Invoke(); } ActiveSyncDeviceConfiguration actualDevice; using(var pipeline = runspace.CreatePipeline()) { pipeline.Commands.AddScript(string.Format("Get-ActiveSyncDeviceStatistics -Mailbox {0}", username)); var result = pipeline.Invoke(); actualDevice = result.Select(x => x.BaseObject as ActiveSyncDeviceConfiguration).Where(x => x.DeviceID.EndsWith(deviceId)).SingleOrDefault(); } if(actualDevice != null) { var identity = actualDevice.Identity as ADObjectId; using(var pipeline = runspace.CreatePipeline()) { var cmd = new Command("Clear-ActiveSyncDevice"); cmd.Parameters.Add("Identity", identity.DistinguishedName); pipeline.Commands.Add(cmd); pipeline.Invoke(); } } }
I can get this to work when a user account is added as a local administrator on the machine, and is also written to windows. I can accept if the user should be a local administrator, but a constant user login is not suitable for the server application. The MyPowershellHost class is just a basic host implementation that allows the RemoteExchange.ps1 script to run from the moment it interacts with the user interface.
I can’t understand if the user needs additional privileges or I’m just doing it wrong.
source share