问题
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;
}
回答1:
This is probably a powershell related error. If you are running the code from a remote machine (not the exchange server), you have to enable remote powershell access for the user in question and make sure the firewall(s) allows connections to the exchange server on port 80. On the exchange server:
Set-User –identity username –RemotePowershellEnabled $True
The user also has to be a member of an exchange management role allowing mailbox creation.
If you are using a load balanser and/or have a DAG you may have to set up an Alternate Service Account to enable Kerberos authentication. See http://technet.microsoft.com/en-us/library/ff808313.aspx for details. I had to enable this to make the code run in my environment. I modified the code a bit to just test if I was able to run exchange powershell commands. The following code responds with the full name of the USERIDENT user if successful.
static void Main(string[] args)
{
SecureString password = new SecureString();
string str_password = "PASS";
string username = "domain\\user";
//FQDN is ofcourse the (fully qualified) name of our exchange server..
string liveIdconnectionUri = "http://SERVERFQDN/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);
Runspace runspace = null;
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("Get-Mailbox");
command.AddParameter("Identity", "USERIDENT");
powershell.Commands = command;
try
{
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
powershell.Runspace = runspace;
Collection<PSObject> commandResults = powershell.Invoke<PSObject>();
foreach (PSObject result in commandResults)
{
Console.WriteLine(result.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
runspace.Dispose();
runspace = null;
powershell.Dispose();
powershell = null;
}
}
来源:https://stackoverflow.com/questions/8361520/enable-exchange-2010-mailbox