创建运行空间时发生内部错误
Internal Error Occurred When Creating RunSpace
我有一个应用程序,允许用户通过一个简单的 Web 界面在 Exchange 上更新其他用户的外出消息。
我最初尝试使用 Microsoft.Exchange.WebServices
连接到 Exchange 服务来创建它,但由于系统帐户所需的权限(帐户更新通过 EWS
的 OOF 消息需要 FULL 邮箱权限)。
所以为了克服这个问题,我创建了以下 class,它使用 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);
An internal error occurred.
这不是一条非常有用的错误消息,我不确定如何调试它。有人对此有什么建议吗?
更新
我在 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)
at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper)
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)
at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)
at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)
at 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 host, PSPrimitiveDictionary applicationArguments)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments)
at SetOutOfOffice.ExchangeShell..ctor()
如果您想进行调试,您应该能够在 .NET 中使用跟踪或仅附加调试器
一个常见的问题是证书检查失败,因此您可能想尝试绕过那些,例如
connectionInfo.SkipCACheck = 真;
connectionInfo.SkipCNCheck = 真;
connectionInfo.MaximumConnectionRedirectionCount = 4;
造成此问题的原因是我在服务器上安装了错误版本的 PowerShell。
遗憾的是 Internal Error
消息没有提到这一点,但可能是一个很好的消息来检查何时发生此错误。
我有一个应用程序,允许用户通过一个简单的 Web 界面在 Exchange 上更新其他用户的外出消息。
我最初尝试使用 Microsoft.Exchange.WebServices
连接到 Exchange 服务来创建它,但由于系统帐户所需的权限(帐户更新通过 EWS
的 OOF 消息需要 FULL 邮箱权限)。
所以为了克服这个问题,我创建了以下 class,它使用 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);
An internal error occurred.
这不是一条非常有用的错误消息,我不确定如何调试它。有人对此有什么建议吗?
更新
我在 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) at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper) at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler) at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler) at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable) at 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 host, PSPrimitiveDictionary applicationArguments) at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) at SetOutOfOffice.ExchangeShell..ctor()
如果您想进行调试,您应该能够在 .NET 中使用跟踪或仅附加调试器
一个常见的问题是证书检查失败,因此您可能想尝试绕过那些,例如
connectionInfo.SkipCACheck = 真; connectionInfo.SkipCNCheck = 真; connectionInfo.MaximumConnectionRedirectionCount = 4;
造成此问题的原因是我在服务器上安装了错误版本的 PowerShell。
遗憾的是 Internal Error
消息没有提到这一点,但可能是一个很好的消息来检查何时发生此错误。