I have a very simple application which I am using for testing purposes and it is acting strangely:
using System;
using System.Windows.Forms;
namespace PSMod
{
class Program
{
static void Main(string[] args)
{
MessageBox.Show("START");
var shell = new ExchangeShell();
var oof = shell.GetOofSettings("test.user");
MessageBox.Show(oof.InternalReply);
MessageBox.Show("END");
}
}
}
I can run this program on my PC without any issues whatsoever. However, when I put it on our server it crashes immediately with the following problem signature:
Problem signature:
Problem Event Name: APPCRASH
Application Name: PSMod.exe
Application Version: 1.0.0.0
Application Timestamp: 57a1c784
Fault Module Name: KERNELBASE.dll
Fault Module Version: 6.1.7601.23455
Fault Module Timestamp: 573a54fc
Exception Code: e0434352
Exception Offset: 000000000001a06d
OS Version: 6.1.7601.2.1.0.272.7
Locale ID: 2057
Additional Information 1: 367e
Additional Information 2: 367e805d0e7c1ec3f63b05bb5ce5c416
Additional Information 3: c826
Additional Information 4: c8268c80d2e62fec799d399d46a4e09f
This is occurring before the first MessageBox
appears, but surely MessageBox
should run before the error-causing line, which I have deduced is the following:
var oof = shell.GetOofSettings("test.user");
MessageBox.Show(oof.InternalReply);
as the program does run when I remove these lines. How is this even possible? It must be something on the server on which it is crashing, but how do I even begin to narrow this down? Usually I would use a MessageBox
to help me identify where the issue is occurring, but I can't even do that here!
ExchangeShell.cs
public class ExchangeShell
{
private Runspace MyRunSpace;
private PowerShell MyPowerShell;
private Uri ExchangeServer;
public ExchangeShell()
{
var exchangeserverurl = new Uri("http://EXCHANGE-SERVER/PowerShell");
var psCredential = GetCredentials();
var connectionInfo = new WSManConnectionInfo(exchangeserverurl, "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential);
//connectionInfo.SkipCACheck = true;
//connectionInfo.SkipCNCheck = true;
//connectionInfo.SkipRevocationCheck = true;
//connectionInfo.MaximumConnectionRedirectionCount = 4;
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
}
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("ADMINUSER", secureString);
}
}
Update
I tried this with the same results:
static void Main(string[] args)
{
try
{
MessageBox.Show("START");
var shell = new ExchangeShell();
var oof = shell.GetOofSettings("seb.test");
MessageBox.Show(oof.InternalReply);
MessageBox.Show("END");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}