【问题标题】:(C#) Getting "Access Denied" error when creating remote powershell session(C#) 创建远程 powershell 会话时出现“拒绝访问”错误
【发布时间】:2019-03-07 00:25:44
【问题描述】:

我对 C# 和 powershell 有疑问。我有一个在服务器上运行的 ASP.net WebApi,我需要它来打开与 Microsoft Exchange 的远程 powershell 会话,以从房间邮箱中获取一些数据。我使用此代码进行连接:

PowerShell instance = PowershellHelper.Instance;

instance.AddScript("$passUnsafe = \"" + Password + "\";" +
"$pass = $passUnsafe | convertto-securestring -AsPlainText -Force;" +
"$UserCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist \"" + Username + "\",$pass;" +
"if (!(Get-PSSession | Where { $_.ConfigurationName -eq \"Microsoft.Exchange\" })) { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection; }" +
"else { $Session = (Get-PSSession | Where { $_.ConfigurationName -eq \"Microsoft.Exchange\" })[0]; }" +
"Import-PSSession $Session -DisableNameChecking;");

PowershellHelper.Instance.Streams.Error.Clear();
instance.Invoke();

这在我的本地机器上运行良好,但在远程服务器上不行。脚本在尝试执行“New-PSSession”时崩溃,并出现以下错误:

[outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

经过一番研究,我发现这是因为不允许非管理员用户执行远程会话命令。如果我将 IIS 应用程序池用户添加到管理员组,它工作正常,但我想尽可能避免这种情况。 有人说您可以将用户添加到内置的“远程管理用户”组,但这对我不起作用。我什至尝试直接更改用户权限,使其具有完全访问权限,但它也不起作用。仅当我将其添加到管理员组时才有效。似乎有其他东西阻止了执行,或者 IIS 忽略了用户组。凭据很好。有谁知道是否有其他方法可以在没有管理员权限的情况下执行远程 powershell 命令?

【问题讨论】:

  • 听起来像是 Microsoft.Exchange 配置的限制。
  • PowerShell 远程处理有自己的设置。你检查过 REST API 吗? docs.microsoft.com/en-us/previous-versions/office/… ?我认为这更适合细粒度的权限检查。
  • Outlook REST API 使用强大的身份验证系统,需要注册我的应用程序以及用户交互来输入凭据。我想避免这种情况,因为我有纯文本的用户名和密码(我正在制作的应用程序不需要任何类型的安全措施)。我想用 Powershell 和 SMTP 做所有事情。我发现的唯一有用的 API 是 Exchange Web Services Managed API,但它会在几年后被弃用,所以我不能使用它。
  • 我不确定您是否尝试以管理员身份运行脚本并启用远程处理模式。

标签: c# powershell iis permissions exchange-server


【解决方案1】:

根据 Simon Li 的 cmets,您可以使用以下 PowerShell 脚本为您的帐户授予远程 PowerShell 执行权限:

Set-User "XXXXX" -RemotePowerShellEnabled $true

https://docs.microsoft.com/en-us/powershell/exchange/exchange-server/control-remote-powershell-access-to-exchange-servers?view=exchange-ps

【讨论】:

  • 对我没有任何影响。我也没想到会这样,因为将应用程序池标识添加到本地管理员解决组可以解决问题。这意味着这是 Web 服务器上的问题,而不是 Exchange 服务器上的问题。我也不清楚“您的帐户”在这里指的是什么。是应用程序池标识还是您用来针对远程 Exchange 服务器进行身份验证的用户或其他什么?
【解决方案2】:

上周我在将基于 Web 的用户配置应用程序从其 Exchange 2010 目标迁移到 Exchange 2016 时遇到了这个问题。最终,问题是应用程序服务器上的权限问题。 IIS APPPOOL\DefaultAppPool 身份试图将值写入HKU:\.DEFAULT\Software\Microsoft\CurrentVersion\WSMAN\Client\ConnectionCookies

我授予了IIS APPPOOL\DefaultAppPool 身份对 ConnectionCookies 键的完全控制权限,并且应用程序能够成功调用 New-PSSession

我在一个名为 Mike's Ramblings 的博客上发现了这个解决方案 (Blogspot):http://codermike.blogspot.com/2016/06/new-pssession-access-denied.html

Mike 发现了 Process Monitor 的问题。我可以通过运行 Process Monitor、重现问题并搜索具有 ACCESS DENIED 结果的任何事件来做到这一点。双击一个事件并单击“进程”选项卡将显示调用用户。

我仍然不明白为什么当我的应用程序使用具有服务帐户身份的专用应用程序池时,IIS APPPOOL\DefaultAppPool 身份会尝试创建此值。

用 Mark Russinovich 的话来说:“当有疑问时,进程监视器。”

【讨论】:

    猜你喜欢
    • 2017-10-25
    • 2011-10-28
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多