Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add existing user to he exchange mailbox 2010 in windows server 2008R2

I installed power shell in my XP system

and trying to access exchange mailbox through powershell from my c#.net Application

i am getting the following exception in the below code

The type initializer for 'System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager' threw an exception.
C#
SecureString password = new SecureString();
        string str_password = "123";
        string username = "xyz";

        string liveIdconnectionUri = "http://exchange.abc.com/Powershell?serializationLevel=Full";

        foreach (char x in str_password)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(username, password);


        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;    

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");   
        command.AddParameter("Identity", "Peter England");
        command.AddParameter("Alias", "peter.england");
        command.AddParameter("Database", "MBX_SBG_01");

        powershell.Commands = command;
        try
        {

            runspace.Open();

            powershell.Runspace = runspace;          
            powershell.Invoke();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
        finally
        {

            runspace.Dispose();
            runspace = null;            
            powershell.Dispose();
            powershell = null;
        }

can anybody help
Posted
Updated 2-May-12 20:17pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900