【发布时间】:2014-06-15 13:49:33
【问题描述】:
我正在使用Exchange 2010。我目前正在使用 c# 中的 PowerShell cmdlet New-Mailbox 创建一个新邮箱帐户。
可以动态创建新邮箱(c#)吗?这是我正在使用的代码:
// Prepare the credentials that will be used when connecting
// to the server. More info on the user to use on the notes
// below this code snippet.
string runasUsername = @"xxxxx";
string runasPassword = "xxxxx";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword)
ssRunasPassword.AppendChar(x);
PSCredential credentials =
new PSCredential(runasUsername, ssRunasPassword);
// Prepare the connection
var connInfo = new WSManConnectionInfo(
new Uri("http://yourip/PowerShell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credentials);
connInfo.AuthenticationMechanism =
AuthenticationMechanism.Basic;
// Create the runspace where the command will be executed
var runspace = RunspaceFactory.CreateRunspace(connInfo);
// generate the command parameters
var testNumber = 18;
var firstName = "Test";
var lastName = "User" + testNumber;
var username = "tuser" + testNumber;
var domainName = "dom.dom.ca";
var password = "qwerty123";
var ssPassword = new SecureString();
foreach (char c in password)
ssPassword.AppendChar(c);
// create the PowerShell command
var command = new Command("New-Mailbox");
command.Parameters.Add("Name", firstName + " " + lastName);
command.Parameters.Add("Alias", username);
command.Parameters.Add(
"UserPrincipalName", username + "@" + domainName);
command.Parameters.Add("SamAccountName", username);
command.Parameters.Add("FirstName", firstName);
command.Parameters.Add("LastName", lastName);
command.Parameters.Add("Password", ssPassword);
command.Parameters.Add("ResetPasswordOnNextLogon", false);
command.Parameters.Add("OrganizationalUnit", "NeumontStudents");
// Add the command to the runspace's pipeline
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(command);
// Execute the command
var results = pipeline.Invoke();
runspace.Dispose();
if (results.Count > 0)
MessageBox.Show("SUCCESS");
else
MessageBox.Show("FAIL");
(Source) Here's the complete tutorial
我的目标是在邮箱创建期间设置帐户属性:
- 地址(街道、城市等)
- 电话号码
- 说明
- 传真
- 等
但看起来命令 cmdlet New-Mailbox 缺少所有这些参数。 New-Mailbox documentation
是否可以在创建邮箱的过程中设置这些参数?
【问题讨论】:
-
仅供参考,我更正了术语——术语“cmdlet”并不是指 PowerShell 本身,而是指 PowerShell 的动词-名词样式命令。 New-Mailbox 和 Set-User 是 cmdlet。
标签: c# powershell active-directory exchange-server exchange-management-shell