【问题标题】:Set the Active Directory account properties during a New-Mailbox creation with PowerShell在使用 PowerShell 创建新邮箱期间设置 Active Directory 帐户属性
【发布时间】: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

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

  1. 地址(街道、城市等)
  2. 电话号码
  3. 说明
  4. 传真

但看起来命令 cmdlet New-Mailbox 缺少所有这些参数。 New-Mailbox documentation

是否可以在创建邮箱的过程中设置这些参数?

【问题讨论】:

  • 仅供参考,我更正了术语——术语“cmdlet”并不是指 PowerShell 本身,而是指 PowerShell 的动词-名词样式命令。 New-MailboxSet-User 是 cmdlet。

标签: c# powershell active-directory exchange-server exchange-management-shell


【解决方案1】:

我不懂 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 属性的某个子集。它会执行您想要的大部分操作,但 description 不会通过此 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 的 ADSI 提供程序。有点笨拙,因为您不能使用一个命令设置多个属性,并且您必须在最后提交更改,但它确实使您能够修改任何可设置的属性。这是一个例子:

    $user = [adsi]("LDAP://" + (Get-User barack.obama).DistinguishedName)
    $user.telephoneNumber = '202-456-1111'
    $user.streetAddress = '1600 Pennsylvania Avenue NW'
      [etc...]
    $user.SetInfo()
    
  • Set-ADUser cmdlet。这个可以修改你想要设置的任何属性,并且可以在一个命令中设置多个属性,并且可能是最容易使用的,但当然有一个问题:你需要导入 ActiveDirectory 模块,它不会在 Exchange 2010 服务器上开箱即用。

【讨论】:

  • 非常好的答案。只是一件事,当您的意思是“Exchange Management Shell”时,您的意思是 Power-Shell 吗? ty
  • 是的。 Exchange 命令行管理程序是具有 Exchange 特定扩展的 PowerShell,与 2007 年及更高版本的 Exchange 版本捆绑在一起。如果您使用 Get-Mailbox,则您使用的是 EMS(除非您已使用 Add-PSSnapin 将 Exchange 管理管理单元添加到普通 PowerShell 会话)。
猜你喜欢
  • 2017-10-11
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多