仅在IIS托管应用程序中远程连接到Exchange时引发异常

时间:2013-10-15 20:42:26

标签: exchange-server remoting credentials

我希望有人可以帮助我,我有以下功能来远程调用PowerShell服务器:

    private static string DoCall()
    {        
        string resultPs = string.Empty;
        string serverName = "myserver.com";
        string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
        System.Uri serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", serverName));
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, (PSCredential)null);
        //connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, PSCredential.Empty);                        
        //connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
        try
        {
            using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                PowerShell powershell = PowerShell.Create();
                powershell.Runspace = runspace;
                runspace.Open();
                powershell.AddScript("Get-DatabaseAvailabilityGroup");
                Collection<PSObject> results = powershell.Invoke();
                if (powershell.Streams.Error.Count > 0)
                {
                    foreach (ErrorRecord err in powershell.Streams.Error)
                    {
                        resultPs += (err.ErrorDetails.Message != null ? err.ErrorDetails.Message : "");
                    }
                }

                foreach (PSObject result in results)
                {
                    foreach (PSPropertyInfo propertyInfo in result.Properties)
                    {
                        resultPs += "Property:" + propertyInfo.Name + " Value:" + propertyInfo.Value;
                    }
                }
                powershell.Runspace.Close();
            }
        }
        catch (Exception ex)
        {
            resultPs = ex.Message + " " + ex.StackTrace + "     innerException: " + (ex.InnerException != null ? (ex.InnerException.Message ?? "") : "");
        }            
        return resultPs;        
    }
}

如果我在控制台应用程序中的Visual Studio中运行此代码,则会使用我的凭据并按预期完成交换调用,但是,如果我在WCF服务中发布此代码并将应用程序池设置为使用我的凭据我收到以下异常

"An internal error occurred. \0    at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)\r\n   at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper)\r\n   at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)\r\n   at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)\r\n   at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)\r\n   at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo)\r\n   at System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo)\r\n   at System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable, RunspaceConnectionInfo connectionInfo, PSHost host, PSPrimitiveDictionary applicationArguments)\r\n   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments)\r\n   at TestingRemote.Service.DoCall() in "

我已经检查了几个关于传递凭据的博客,但我没有找到具有相同问题的人,这解决了我的问题。我知道我可以使用重载方法来初始化凭据,但我想使用应用程序池中的那些。

0 个答案:

没有答案