【发布时间】: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