在使用PowerShell创建新邮箱期间设置Active Directory帐户属性
本文关键字:Active 设置 Directory 属性 PowerShell 创建 新邮箱 | 更新日期: 2023-09-27 18:00:39
我正在使用Exchange 2010
。我当前正在c#中使用PowerShellcmdlet 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");
(来源)以下是完整的教程
我的目标是在创建邮箱期间设置帐户属性:
- Adress(街道、城市等)
- 电话号码
- 说明
- 传真
- Etc
但命令cmdlet New-Mailbox
似乎缺少所有这些参数。新邮箱文档
是否可以在创建邮箱期间设置这些参数?
我不知道C#,但我可以告诉你如何从Exchange命令行管理程序中执行,并让你从C#中调用命令,这似乎你没有问题。
最简单的方法是使用Exchange命令行管理程序的Set Usercmdlet:
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-Mailboxcmdlet直接发送到Set-User,因为它返回它创建的邮箱对象:
New-Mailbox [...] | Set-User -StreetAddress [...]
参数名称并不总是与AD属性名称匹配。例如,-Phone映射到officePhoneAD属性;还有一个-HomePhone参数,它映射到HomePhone性。
但是,设置用户仅限于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 [...] | 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-ADUsercmdlet。这个可以修改您想要设置的任何属性,也可以在一个命令中设置多个属性,而且可能是最容易使用的,但当然有一个问题:您需要导入ActiveDirectory模块,这在Exchange 2010服务器上是不现成的。