我有一个应用程序,允许用户通过简单的Web界面更新Exchange上其他用户的外出消息。
我最初尝试使用Microsoft.Exchange.WebServices
连接到Exchange服务来创建此项,但由于系统帐户所需的权限(更新OOF消息的帐户),我不得不放弃此操作(即使它确实有效)通过EWS
需要完整邮箱权限)。
为了克服这个问题,我创建了以下类,它使用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
与服务器之间实际发生的事情......
堆栈追踪:
at 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 session,PSRemotingCryptoHelper cryptoHelper,RunspaceConnectionInfo connectionInfo,URIDirectionReported uriRedirectionHandler) 在System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool,URIDirectionReported uriRedirectionHandler) 在System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool,TypeTable typeTable) 在System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces,Int32 maxRunspaces,TypeTable typeTable,PSHost host,PSPrimitiveDictionary applicationArguments,RunspaceConnectionInfo connectionInfo) at System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces,Int32 maxRunspaces,TypeTable typeTable,PSHost host,PSPrimitiveDictionary applicationArguments,RunspaceConnectionInfo connectionInfo) 在System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable,RunspaceConnectionInfo connectionInfo,PSHost host,PSPrimitiveDictionary applicationArguments) 在System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo,PSHost主机,TypeTable typeTable,PSPrimitiveDictionary applicationArguments) 在SetOutOfOffice.ExchangeShell..ctor()
答案 0 :(得分:1)
如果要调试,您应该能够在.NET中使用跟踪或只是附加调试器
一个常见问题是证书检查失败,因此您可能希望尝试绕过那些例如
connectionInfo.SkipCACheck = true; connectionInfo.SkipCNCheck = true; connectionInfo.MaximumConnectionRedirectionCount = 4;
答案 1 :(得分:0)
导致此问题是因为我在服务器上安装了错误的PowerShell版本。
令人遗憾的是Internal Error
消息没有提到这一点,但可能是检查何时发生此错误的好消息。