Bassie Bassie - 1 year ago 151
PowerShell Question

Internal Error Occurred When Creating RunSpace

I have an app which allows user's to update other user's out of office message on Exchange via a simple web interface.

My initial attempt at creating this used using

to connect to Exchange services, but I had to abandon this (even though it did work) due to the permissions required on the system account (an account updating OOF messages via
requires FULL mailbox permissions).

So to overcome this, I created the following class, which uses
remoting to achieve the same thing:

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, "", psCredential);

MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
catch (Exception ex)
Email.Send($"Unable to connect \n\n Error: {ex.Message} ");

public OofSettings GetOofSettings(string email)
using (MyRunSpace)

using (var powerShell = PowerShell.Create())
powerShell.Runspace = MyRunSpace;

var command = new PSCommand();

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")
return new PSCredential("SERVICEACCOUNT", secureString);

This also works when run locally on my machine or as an EXE on the server.

However, when I host this over IIS I am seeing an error on this line:

MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);

An internal error occurred.

This is not a very helpful error message and I am not sure how I can go about debugging this. Does anyone have any suggestions on this?


I attached a tracer in
and here is some trace information after selecting a user to retrieve their out of office details:

Category Message From First(s) From Last(s) Begin PreInit End PreInit 0.000025 0.000025 Begin Init 0.000035 0.000009 End Init 0.000057 0.000022 Begin InitComplete 0.000065 0.000008 End InitComplete 0.000073 0.000008 Begin PreLoad 0.000081 0.000008 End PreLoad 0.000093 0.000012 Begin Load 0.000101 0.000008

But I really don't know what to make of this information - it doesn't seem to reveal much about what is actually going on between the
and the server..

Stack Trace:

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()

Answer Source

This issue was being caused because I had the wrong version of PowerShell installed on the server.

It is a shame that the Internal Error message does not mention this, but probably a good one to check when this error occurs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download