在ASP.NET / IIS中执行Exchange Online PowerShell命令

时间:2019-05-30 20:33:20

标签: asp.net vb.net exchange-server powershell-remoting

使用System.Management.Automation(v。10.0.10586)我可以从Windows应用程序成功执行Exchange Online PowerShell命令,但是我需要从ASP.NET应用程序执行该命令。尝试在Web应用程序中打开运行空间时遇到“访问被拒绝”错误,除非我让Web应用程序的应用程序池在本地服务等帐户下运行。有没有一种方法可以在不向Web应用程序授予此类扩展权限的情况下进行?

Microsoft建议将Exchange用户添加为管理员角色,但这不是我的问题,因为我可以使用Windows应用程序和相同的Exchange凭据(https://support.microsoft.com/en-za/help/2905767/access-is-denied-error-when-you-connect-to-exchange-online-by-using-re)执行PowerShell命令。

我也尝试过微软建议在代码中模拟特定用户的建议,但这似乎行不通(https://support.microsoft.com/en-us/help/306158/how-to-implement-impersonation-in-an-asp-net-application)。

        Dim userName As String = ConfigurationManager.AppSettings("ExchangeUserName")
        Dim password As String = ConfigurationManager.AppSettings("ExchangePassword")
        Dim securePassword As System.Security.SecureString = New System.Security.SecureString()
        For Each c As Char In password
            securePassword.AppendChar(c)
        Next
        Dim credentials As PSCredential = New PSCredential(userName, securePassword)
        Dim connection As WSManConnectionInfo = New WSManConnectionInfo(New Uri("https://outlook.office365.com/powershell-liveid"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials) With {
                .AuthenticationMechanism = AuthenticationMechanism.Basic,
                .SkipCACheck = True,
                .SkipCNCheck = True,
                .SkipRevocationCheck = True,
                .NoEncryption = False,
                .MaximumConnectionRedirectionCount = 4
            }
        Using runSpace As Runspace = RunspaceFactory.CreateRunspace(connection)
            runSpace.Open()
        End Using

该过程在runSpace.Open()处失败,并显示以下错误:

System.Management.Automation.Remoting.PSRemotingTransportException:连接到远程服务器exchangeserver.admin.com失败,并显示以下错误消息:访问被拒绝。有关更多信息,请参见about_Remote_Troubleshooting帮助主题。

0 个答案:

没有答案