我有一个应用程序,允许用户通过一个简单的web界面更新其他用户在Exchange上的外出信息。
我最初尝试使用Microsoft.Exchange.WebServices来创建OOF,以连接到Exchange,但由于系统帐户上所需的权限(通过EWS更新OOF消息的帐户需要完全的邮箱权限),我不得不放弃它。
因此,为了克服这一问题,我创建了以下类,它使用PowerShell远程处理来实现相同的目标:
public class ExchangeShell
{
private Runspace MyRunSpace;
private PowerShell MyPowerShell;
private Uri ExchangeServer;
public ExchangeShell()
{
var exchangeserverurl = new Uri("http://EXCHANGESERVER/PowerShell");
var psCredential = GetCredentials();
var connectionInfo = new WSManConnectionInfo(exchangeserverurl, "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential);
try
{
MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
}
catch (Exception ex)
{
Email.Send($"Unable to connect \n\n Error: {ex.Message} ");
Environment.Exit(Environment.ExitCode);
}
}
public OofSettings GetOofSettings(string email)
{
using (MyRunSpace)
{
MyRunSpace.Open();
using (var powerShell = PowerShell.Create())
{
powerShell.Runspace = MyRunSpace;
var command = new PSCommand();
command.AddCommand("Get-MailboxAutoReplyConfiguration");
command.AddParameter("Identity", email);
powerShell.Commands = command;
var result = powerShell.Invoke();
var oofSetting = new OofSettings();
oofSetting.State = (OofState)Enum.Parse(typeof(OofState), result[0].Properties["AutoReplyState"].Value.ToString());
oofSetting.Duration = new TimeWindow((DateTime)result[0].Properties["StartTime"].Value, (DateTime)result[0].Properties["EndTime"].Value);
oofSetting.ExternalAudience = (OofExternalAudience)Enum.Parse(typeof(OofExternalAudience), result[0].Properties["ExternalAudience"].Value.ToString());
oofSetting.InternalReply = result[0].Properties["InternalMessage"].Value.ToString();
oofSetting.ExternalReply = result[0].Properties["ExternalMessage"].Value.ToString();
return oofSetting;
}
}
}
private PSCredential GetCredentials()
{
var secureString = new SecureString();
foreach (char c in @"PASSWORD")
{
secureString.AppendChar(c);
}
return new PSCredential("SERVICEACCOUNT", secureString);
}
}当在我的机器上本地运行或作为服务器上的EXE运行时,这也是有效的。
但是,当我在IIS上托管它时,我在这一行上看到了一个错误:
MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);发生了内部错误。
这不是一条非常有用的错误消息,我不知道如何调试它。有人对此有什么建议吗?
更新
我在web.config中附加了一个跟踪器,下面是一些在选择用户以检索他们的外出详细信息之后的跟踪信息:
Category Message From First(s) From Last(s)
aspx.page Begin PreInit
aspx.page End PreInit 0.000025 0.000025
aspx.page Begin Init 0.000035 0.000009
aspx.page End Init 0.000057 0.000022
aspx.page Begin InitComplete 0.000065 0.000008
aspx.page End InitComplete 0.000073 0.000008
aspx.page Begin PreLoad 0.000081 0.000008
aspx.page End PreLoad 0.000093 0.000012
aspx.page Begin Load 0.000101 0.000008 但是我真的不知道如何理解这些信息--它似乎并没有透露出runspace和服务器之间到底发生了什么。
堆栈跟踪:
(在System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri,WSManConnectionInfo connectionInfo)在System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId,WSManConnectionInfo connectionInfo,PSRemotingCryptoHelper cryptoHelper)在System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession会议上,PSRemotingCryptoHelper cryptoHelper,RunspaceConnectionInfo connectionInfo,cryptoHelper cryptoHelper)在System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal,en21#)在en22#,( System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces,Int32 maxRunspaces,TypeTable typeTable,PSHost主机,PSPrimitiveDictionary applicationArguments,RunspaceConnectionInfo connectionInfo )在en3 2#,en28#,主机,),,主机,主机,en35#,PSPrimitiveDictionary applicationArguments) at SetOutOfOffice.ExchangeShell..ctor()
发布于 2016-08-15 11:34:25
造成此问题的原因是我在服务器上安装了错误版本的PowerShell。
遗憾的是,Internal Error消息没有提到这一点,但可能是一个很好的检查何时发生错误。
发布于 2016-08-03 01:59:50
如果您想调试,您应该能够在.NET中使用跟踪,或者只需附加调试器。
一个常见的问题是证书检查失败,所以您可能想绕过那些例如。
connectionInfo.SkipCACheck =真;connectionInfo.SkipCNCheck =真;connectionInfo.SkipCNCheck= 4;
https://stackoverflow.com/questions/38715695
复制相似问题