3

我正在使用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");

(来源)这是完整的教程

我的目标是在邮箱创建期间设置帐户属性:

  1. 地址(街道、城市等)
  2. 电话号码
  3. 描述
  4. 传真
  5. ETC

但看起来命令 cmdletNew-Mailbox缺少所有这些参数。新邮箱文档

是否可以在创建邮箱期间设置这些参数?

4

1 回答 1

2

我不知道 C#,但我可以告诉您如何从 Exchange 命令行管理程序执行此操作,并让您从 C# 调用命令,看起来您没有问题。

最简单的方法是使用 Exchange 命令行管理程序的Set-User cmdlet:

Set-User -Identity barack.obama -StreetAddress '1600 Pennsylvania Ave NW' -City 'Washington' -StateOrProvince 'D.C.' -PostalCode '20500' -Phone '202-456-1111' -Fax '202-456-2461'

-Identity可以是您可以与Get-Mailbox一起使用的任何身份参数,包括 SamAccountName、UserPrincipalName、SmtpAddress、Identity、Alias 等。您还可以通过管道将邮箱对象传递给Set-User并省略-Identity。实际上,您可以将New-Mailbox cmdlet 直接通过管道传递给Set-User,因为它会返回它创建的邮箱对象:

New-Mailbox [...] | Set-User -StreetAddress [...]

参数名称并不总是与 AD 属性名称匹配。例如,-Phone映射到officePhone AD 属性;还有一个-HomePhone参数,它映射到homePhone属性。

但是,Set-User仅限于 Active Directory 属性的某个子集。它将执行您想要的大部分操作,但描述不会通过此 cmdlet 公开。您的“Etc”中可能还有其他一些未公开的属性。要设置其他属性,您需要使用其他更新 Active Directory 的方法。

但是,将其集成到 EMS 脚本中并不难。您将始终在 Exchange Server 上使用 AD 管理工具,因此您可以使用dsmod,它可以更新不同的属性子集,包括描述:

dsmod user (Get-Mailbox barack.obama).DistinguishedName -desc 'Description'

对象类型 ( user ) 之后的第一个参数是可分辨名称,您可以使用 . 从邮箱中读取该名称(Get-Mailbox barack.obama).DistinguishedName

同样,参数名称与 AD 属性不匹配,但您可以通过键入获取完整列表dsmod user /?。直接从New-Mailbox管道:

New-Mailbox [...] | select -ExpandProperty DistinguishedName | %{dsmod user $_ -desc 'Description'}

其他选项:

  • PowerShell's ADSI provider. A little clunkier, because you can't set multiple attributes with one command, and you have to commit the changes at the end, but it does enable you to modify any settable attribute. Here's an example:

    $user = [adsi]("LDAP://" + (Get-User barack.obama).DistinguishedName)
    $user.telephoneNumber = '202-456-1111'
    $user.streetAddress = '1600 Pennsylvania Avenue NW'
      [etc...]
    $user.SetInfo()
    
  • The Set-ADUser cmdlet. This one can modify any attribute you'll want to set and can set multiple attributes in a single command, and is probably the easiest to use, but of course there's a catch: You need to import the ActiveDirectory module, which won't be available out of the box on an Exchange 2010 server.

于 2014-04-29T19:46:36.560 回答