2010-08-03 59 views
4

我已经获得编写程序以自动创建2010 Exchange邮箱的任务。我的研究告诉我使用PowerShell,但我似乎无法找到要引用的命名空间,并且需要一些示例代码。我在网上找到了一些代码,但我不知道PowerShell的命名空间是什么。我认为它可能是System.Management.Automation,但是当我尝试引用名称空间时,它不存在于dotnet列表中。我所拥有的是System.Management和System.Management.Instrumentation。如何以编程方式创建使用C#的Exchange 2010邮箱

任何帮助将不胜感激?

回答

4

当我这样做时,我不得不单独下载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; 
} 
+0

PowerShell的命名空间是:[Microsoft.PowerShell](http://msdn.microsoft.com/en-us/library/microsoft.powershell(VS.85) .aspx) – 2010-08-03 15:37:53

+0

'return = powershell.Invoke();'不能编译,你错过了什么吗? – 2013-04-17 07:44:39

相关问题