Home > Exchange 2010 AKA E14 > Exchange 2010 using C# and ASP.net to create mailboxes using remote shell connect

Exchange 2010 using C# and ASP.net to create mailboxes using remote shell connect


here is a nice code snippest:
SecureString password = new SecureString();

string str_password = “pass”;

string username = “userr”;

string liveIdconnectionUri = “http://exchange.Domain.local/Powershell?serializationLevel=Full”;

foreach (char x in str_password)

{

password.AppendChar(x);

}

PSCredential credential = new PSCredential(username, password);

// Set the connection Info

WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), “http://schemas.microsoft.com/powershell/Microsoft.Exchange”,

credential);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

// create a runspace on a remote path

// the returned instance must be of type RemoteRunspace

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

PowerShell powershell = PowerShell.Create();

PSCommand command = new PSCommand();

command.AddCommand(“Enable-Mailbox”);

command.AddParameter(“Identity”, usercommonname);

command.AddParameter(“Alias”, userlogonname);

command.AddParameter(“Database”, “MBX_SBG_01”);

powershell.Commands = command;

try

{

// open the remote runspace

runspace.Open();

// associate the runspace with powershell

powershell.Runspace = runspace;

// invoke the powershell to obtain the results

return = powershell.Invoke();

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

finally

{

// dispose the runspace and enable garbage collection

runspace.Dispose();

runspace = null;

// Finally dispose the powershell and set all variables to null to free

// up any resources.

powershell.Dispose();

powershell = null;

}

Categories: Exchange 2010 AKA E14
  1. christof
    October 27, 2010 at 4:34 pm

    i use this code from a windows sever 2003 32-bit , i connect succesfully to my exchange 2010 which runs on a 64-bit machine but the only commands that run succesfully are :
    command.AddCommand(“Get-Group”); and
    command.AddCommand(“Get-mailbox”);

    every other command that i try to use for example Get-Group i have an error like:

    The term ‘Get-Groups’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    could you please explain me why this happens??????????????

  2. November 1, 2010 at 3:08 pm

    are you sure that user connecting has the correct permissions

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: