我如何编程方式使用C#创建Exchange 2010邮箱 [英] How do I programatically create an exchange 2010 mailbox using C#
问题描述
我一直在考虑一个任务编写一个程序来自动创建一个2010 Exchange邮箱。我的研究告诉我使用PowerShell,但我似乎无法找到引用的命名空间,并希望一些样本code。我在网上找到了一些code,但我不知道该命名空间是什么的PowerShell。我想可能是System.Management.Automation但是当我尝试引用该命名空间不会在dotnet的名单存在。我想说明的是System.Management和System.Management.Instrumentation。
I have been given a task to write a program to automatically create a 2010 exchange mailbox. My research tells me to use powershell but I can't seem to find the namespace to reference and would like some sample code. I found some code on the web but I don't know what the namespace is for PowerShell. I think it might be System.Management.Automation but when I try to reference the namespace it does not exist in the list of dotnet. All I have is System.Management and System.Management.Instrumentation.
任何帮助将是AP preciated?
Any help would be appreciated?
推荐答案
当我做到了,我不得不单独下载PowerShell中,不知道这是否仍是如此,虽然。你可以从这里得到它>。
When I did it I had to download Powershell separately, not sure if this is still the case though. You can get it from here.
下面是例子code,将创建一个邮箱:
Here is example code that will create a Mailbox:
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#创建Exchange 2010邮箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!