我有一個應用程序,它允許用戶通過簡單的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()
感謝您的回答格倫,我試着跳過那些'connectionInfo'檢查但沒有成功。在檢查完'trace.axd'後,我確實看不到任何有用的信息(儘管也許我沒有正確閱讀) - 我在我的問題中發佈了一些信息 – Bassie