gpt4 book ai didi

c# - 如何使用 C# 以编程方式创建 Exchange 2010 邮箱

转载 作者:太空狗 更新时间:2023-10-30 01:11:04 25 4
gpt4 key购买 nike

我接到一个任务,要编写一个自动创建 2010 Exchange 邮箱的程序。我的研究告诉我使用 powershell,但我似乎无法找到要引用的命名空间并想要一些示例代码。我在网上找到了一些代码,但我不知道 PowerShell 的命名空间是什么。我认为它可能是 System.Management.Automation 但是当我尝试引用命名空间时它不存在于 dotnet 列表中。我只有 System.Management 和 System.Management.Instrumentation。

如有任何帮助,我们将不胜感激?

最佳答案

当我这样做时,我不得不单独下载 Powershell,但不确定是否仍然如此。你可以从here得到它.

下面是创建邮箱的示例代码:

SecureString password = new SecureString();
string str_password = "pass";
string username = "userr";

string liveIdconnectionUri = "http://exchange.wenatex.com/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;
}

关于c# - 如何使用 C# 以编程方式创建 Exchange 2010 邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3398056/

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