创建RunSpace时发生内部错误
我有一个应用程序,它允许用户通过简单的Web界面更新Exchange上的其他用户的不在办公室消息。创建RunSpace时发生内部错误
我在创造用于使用Microsoft.Exchange.WebServices
连接到Exchange服务,但我不得不放弃这个(即使它没有工作)由于对系统帐户所需的权限(通过EWS
更新OOF邮件帐户需要最初的尝试FULL邮箱权限)。
因此,要克服这一点,我创建了下面的类,它使用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 ..构造函数(GUID runspacePoolInstanceId,WSManConnectionInfo connectionInfo,PSRemotingCryptoHelper cryptoHelper) 在System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession会话,PSRemotingCryptoHelper cryptoHelper,RunspaceConnectionInfo connectionInfo,URIDirectionReported uriRedirectionHandler) 在System.Man agement.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) at System.Management。Automation.RemoteRunspace..ctor(TypeTable typeTable,RunspaceConnectionInfo connectionInfo,PSHost主机,PSPrimitiveDictionary applicationArguments) 在System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo,PSHost主机,TypeTable typeTable,PSPrimitiveDictionary applicationArguments) 在SetOutOfOffice.ExchangeShell ..ctor()
此问题是由于我在服务器上安装了错误版本的PowerShell而导致的。
这是一个耻辱,Internal Error
消息没有提到这一点,但可能是一个很好的检查何时发生此错误。
如果你想调试你应该能够使用.NET跟踪或只是附加调试
一个常见的问题是没有证书检查,所以你可能想尝试绕过那些例如
connectionInfo.SkipCACheck = true; connectionInfo.SkipCNCheck = true; connectionInfo.MaximumConnectionRedirectionCount = 4;
感谢您的回答格伦,我试着跳过那些'connectionInfo'检查但没有成功。在检查完'trace.axd'后,我确实看不到任何有用的信息(尽管也许我没有正确阅读) - 我在我的问题中发布了一些信息 – Bassie