gpt4 book ai didi

c# - 启用 exchange 2010 邮箱

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:14 30 4
gpt4 key购买 nike

我正在尝试通过 C# 代码在 Exchange 2010 服务器上创建/启用邮箱。我到处都看到人们使用下面显示的代码。

但是我得到以下错误:

术语“Enable-Mailbox”未被识别为 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;
}

最佳答案

这可能是与 powershell 相关的错误。如果您从远程计算机(而不是交换服务器)运行代码,则必须为相关用户启用远程 powershell 访问并确保防火墙允许连接到端口 80 上的交换服务器。在交换服务器:

Set-User –identity username –RemotePowershellEnabled $True

用户还必须是允许创建邮箱的交换管理角色的成员。

如果您使用负载均衡器和/或有 DAG,您可能必须设置备用服务帐户以启用 Kerberos 身份验证。参见 http://technet.microsoft.com/en-us/library/ff808313.aspx了解详情。我必须启用它才能使代码在我的环境中运行。我稍微修改了代码以测试我是否能够运行 exchange powershell 命令。如果成功,以下代码将返回 USERIDENT 用户的全名。

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;
}

}

关于c# - 启用 exchange 2010 邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361520/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com