【发布时间】:2012-01-11 18:56:00
【问题描述】:
我正在尝试使用 C# 代码在 Exchange 2010 服务器上创建/启用邮箱。 我到处都能看到人们使用下面显示的代码。
但是我得到以下错误:
“启用邮箱”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。
我做错了什么?
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;
}
【问题讨论】:
-
How do I programatically create an exchange 2010 mailbox using C# 的可能副本。请阅读已接受答案的第一段 - 这有帮助吗?
-
当我尝试接受答案的代码时,我收到以下错误:没有为 Windows PowerShell 版本 2 注册管理单元。
-
这就是你的答案。 :) Exchange 管理单元尚未注册 PowerShell,因此它无法识别
Enable-Mailbox。所以你的新问题(serverfault 更好的是“如何为 PowerShell 2 注册 Exchange 2010 管理单元?”。 -
但它只是说当我从 C# 调用该函数时它尚未注册,当我尝试从 PS 启用邮箱时,我可以打开与 exch 的会话。服务器,然后成功调用 enable-mailbox 命令。当我尝试这个时:pedroliska.wordpress.com/2011/07/22/… 它显示所有命令都没有错误,但是当我将 'get-command' 替换为 'Enable-Mailbox' 和附加参数时,它没有给出错误,但它也不起作用。我认为这是因为链接中的代码我没有连接到交换服务器(?)
标签: c# asp.net email powershell exchange-server