[英]How do I programatically create an exchange 2010 mailbox using C#
I have been given a task to write a program to automatically create a 2010 exchange mailbox. 我被赋予了编写程序以自动创建2010交换邮箱的任务。 My research tells me to use powershell but I can't seem to find the namespace to reference and would like some sample code.
我的研究告诉我使用powershell但我似乎无法找到要引用的命名空间,并且想要一些示例代码。 I found some code on the web but I don't know what the namespace is for PowerShell.
我在网上找到了一些代码,但我不知道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.
我认为它可能是System.Management.Automation但是当我尝试引用命名空间时,它不存在于dotnet列表中。 All I have is System.Management and System.Management.Instrumentation.
我所拥有的只是System.Management和System.Management.Instrumentation。
Any help would be appreciated? 任何帮助,将不胜感激?
When I did it I had to download Powershell separately, not sure if this is still the case though. 当我这样做时,我不得不单独下载Powershell,但不确定是否仍然如此。 You can get it from here .
你可以从这里得到它。
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;
}
This is an old question but it might help future visitors... 这是一个老问题,但它可能有助于未来的访客......
w69rdy's answer did not work for me. w69rdy的回答对我不起作用。 But I got it working and blogged about it here http://pedroliska.wordpress.com/2011/07/22/running-exchange-management-shell-commands-powershell-with-c/
但我得到了它的工作,并在这里写博客http://pedroliska.wordpress.com/2011/07/22/running-exchange-management-shell-commands-powershell-with-c/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.