首页 > 解决方案 > 在跨域的 IIS 上的 C# 应用程序中运行 Powershell cmdlet

问题描述

我们必须域:NT1 和 NT2。

我们有一个 C# 应用程序,它在 Exchange 服务器(域 NT2)上调用 Powershell cmdlet。它使用NT2\User的凭据。

摘录:

using System.Management.Automation;
using System.Management.Automation.Runspaces;
...
var connectionInfo = nw WSManConnectionInfo(
    exchangeUri,
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
    new PSCredential(nt2User, password));
connectionInfo.SkipRevocationCheck = true;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;

using var runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
using var pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Set-User -Identity Contoso\Jill -DisplayName Jill");
pipeline.Invoke();

当我简单地在我的计算机(域 NT1)上运行这个应用程序时,一切都很好。

当应用程序部署在域 NT1 中的服务器上并托管在 IIS 上时,它会停止工作。IIS 应用程序池配置有域 NT1 ( NT1\User ) 的标识。

它在 System.Managemnt.Automation.RemoteRunspace.Open() 处引发 System.Managemnt.Automation.Remoting.PSRemotingTransportException:

Connecting to remote server mail.test.brightfulsolutions.com failed with the following error message: Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

似乎 IIS 应用程序池身份用户 NT1\User 和 Powershell 预期用户 NT2\User 冲突,但不知道该怎么做。

欢迎任何建议(代码、服务器配置)。

标签: c#powershelliispowershell-remotingsystem.management

解决方案


推荐阅读