0

我正在开发一种解决方案,用户可以通过网页擦除在 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 交互。

我不知道用户是否需要额外的特权,或者我做错了。

4

1 回答 1

1

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

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

有关详细信息,请参阅http://technet.microsoft.com/en-us/library/dd297932.aspx

示例代码:

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
}

您还应该调查发送一个PSDataCollectionInvoke调用Complete()它的空对象。这将阻止 Powershell 管道阻塞输入请求,这会挂起您的网络服务器进程。

var psInput = new PSDataCollection<PSObject>();
psInput.Complete();
于 2012-03-09T01:26:44.350 回答