C# 调用exchange activesync cmdlet';来自ASP.NET应用程序的

C# 调用exchange activesync cmdlet';来自ASP.NET应用程序的,c#,asp.net,powershell,exchange-server-2010,C#,Asp.net,Powershell,Exchange Server 2010,我正在开发一个解决方案,用户可以通过网页擦除在Exchange 2010中注册的移动设备,但Outlook Web Access不是一个选项。 我已在我的开发人员计算机上安装了Exchange管理工具,应用程序池正在使用具有执行命令所需权限的标识(分配的角色组“收件人管理”)。我正在使用以下代码执行擦拭 string deviceId = "deviceid"; string username = "username"; RunspaceConfiguration rsConf

我正在开发一个解决方案,用户可以通过网页擦除在Exchange 2010中注册的移动设备,但Outlook Web Access不是一个选项。 我已在我的开发人员计算机上安装了Exchange管理工具,应用程序池正在使用具有执行命令所需权限的标识(分配的角色组“收件人管理”)。我正在使用以下代码执行擦拭

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();
            }
        }
    }
当用户帐户以本地管理员的身份添加到计算机上并登录到windows时,我可以实现这一点。如果用户必须是本地管理员,但让用户不断登录服务器应用程序并不实际,我可以接受。 MyPowershellHost类只是允许RemoteExchange.ps1脚本运行的基本主机实现,因为它与UI交互


我无法确定用户是否需要额外的特权,或者我只是做错了。

您将遇到的关键问题之一是连接到Exchange的方式。您不需要像控制台那样加载管理工具脚本,只需使用PowerShell远程处理。您甚至不需要在web服务器上安装管理工具

此外,Exchange 2010不支持直接加载管理单元

有关详细信息,请参阅

示例代码:

using (var ps = PowerShell.Create()) {
    ps.AddScript("$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI 'http://[host]/PowerShell/' ; Import-PSSession -Session $session");
    ps.Invoke();
    // ... further powershell pipelines - now connected to Exchange and cmdlets are loaded
}
您还应该研究将调用了
Complete()
的空
PSDataCollection
发送到
Invoke
。这将阻止Powershell管道阻塞输入请求,这将挂起您的Web服务器进程

var psInput = new PSDataCollection<PSObject>();
psInput.Complete();
var psInput=new PSDataCollection();
psInput.Complete();