PowerShell远程调用。 Web服务器拒绝访问

时间:2017-05-24 16:46:04

标签: vb.net exchange-server powershell-v3.0

我从asp.net应用程序编写了一个远程访问交换powershell的代码,以使用vb.net和交换2016启用远程邮件 该命令从我的visual studio调试成功运行 但当我放入iis web服务器时,它正在给我

  

连接到远程服务器“”失败,出现以下错误   消息:访问被拒绝

这是代码

Function enableRemoteEmail(ByVal samaccount As String, ByVal email As String) As String
    Dim ServerUri As String = "http://{mailserver}/powershell" 
    Dim SchemaUri As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
    Dim userName As String = AccountOperatorLogon 
    Dim password As New System.Security.SecureString
    For Each x As Char In AccountOperatorPassword
        password.AppendChar(x)
    Next

    Dim PSCredential As New PSCredential(userName, password)
    Dim ConnectionInfo As WSManConnectionInfo = New WSManConnectionInfo(New Uri(ServerUri), SchemaUri, PSCredential)
    ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic
    Dim RemoteRunspace As Runspace = RunspaceFactory.CreateRunspace(ConnectionInfo)
    Dim RemotePowerShell As PowerShell = PowerShell.Create
    RemotePowerShell.AddCommand("Enable-RemoteMailbox")
    RemotePowerShell.AddParameter("Identity", samaccount)
    RemotePowerShell.AddParameter("RemoteRoutingAddress",email )
    RemotePowerShell.AddParameter("Credential", PSCredential)

    ' Open the remote runspace on the server.
    RemoteRunspace.Open()
    ' Associate the runspace with the Exchange Management Shell.
    RemotePowerShell.Runspace = RemoteRunspace
    Dim TheResult As Collection(Of PSObject)
    Dim TheResultString As String = ""
    TheResult = RemotePowerShell.Invoke
    For Each RSLT As PSObject In TheResult
        TheResultString += RSLT.ToString() + "<br/>"
    Next
    RemoteRunspace.Close()
    ' Invoke the Exchange Management Shell to run the command.
    Return TheResultString
End Function

1 个答案:

答案 0 :(得分:1)

我发现任何人的解决方案都有同样的问题

  1. 将用户创建为(收件人管理组)
  2. 的成员
  3. IIS更改(对于Exchange服务器)导航到IIS管理器|默认网站| PowerShell将物理路径从:C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\PowerShell更改为:C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\PowerShell
  4. 3.更改后:IISRESET

    它会正常工作