I'm trying to create/enable a mailbox on an exchange 2010 server from C# code. Everywhere I look I see people using the code shown below.
However I get the following error:
The term 'Enable-Mailbox' 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.
What am I doing wrong?
SecureString password = new SecureString();
string str_password = "myPassword";
string username = "myUsername";
//FQDN is ofcourse the (fully qualified) name of our exchange server..
string liveIdconnectionUri = "http://FQDN/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", "domainname.ltd/OUName/TestAcc Jap");
command.AddParameter("Alias", "TestAccJap");
command.AddParameter("Database", "DB-Name");
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;
}