C#, Powershell, and Exchange
-
If this is the wrong place to post this question, please move it. There's lots of overlap, so I wasn't sure where to put it. Background: I'm trying to write a simple web application in C# that utilizes the Exchange Management Shell (EMS, 2007), which is itself a Windows Powershell snapin. I found a nice wrapper at here[^], which is what I'm using thus:
public void AD_SetForwardAddress()
{
//Initialize the ExchangeManagementShellWrapper
ExchangeManagementShellWrapper ems = ExchangeManagementShellWrapper.Instance;
ICollection<PSObject> results;//Use the RunspaceInvoke command with a command string results = ems.RunspaceInvoke("Get-Command"); foreach (PSObject item in results) { Response.Write(item.Members\["Name"\].Value.ToString()); }
}
... which yeilds
Server Error in '/' Application.
Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Management.Automation.Runspaces.PSSnapInException: Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[PSSnapInException: Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.]
System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadCustomPSSnapIn(PSSnapInInfo mshsnapinInfo) +3816533
System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) +95
System.Management.Autom -
If this is the wrong place to post this question, please move it. There's lots of overlap, so I wasn't sure where to put it. Background: I'm trying to write a simple web application in C# that utilizes the Exchange Management Shell (EMS, 2007), which is itself a Windows Powershell snapin. I found a nice wrapper at here[^], which is what I'm using thus:
public void AD_SetForwardAddress()
{
//Initialize the ExchangeManagementShellWrapper
ExchangeManagementShellWrapper ems = ExchangeManagementShellWrapper.Instance;
ICollection<PSObject> results;//Use the RunspaceInvoke command with a command string results = ems.RunspaceInvoke("Get-Command"); foreach (PSObject item in results) { Response.Write(item.Members\["Name"\].Value.ToString()); }
}
... which yeilds
Server Error in '/' Application.
Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Management.Automation.Runspaces.PSSnapInException: Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[PSSnapInException: Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.]
System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadCustomPSSnapIn(PSSnapInInfo mshsnapinInfo) +3816533
System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) +95
System.Management.AutomMy two cents: Certainly a privilieged account with exchange permission problem.