向用户 Exchange 2010 批量添加 SmtpAddress

Bulk Add SmtpAddress to users Exchange 2010

我们最近刚刚在我们的接受域和电子邮件地址政策中添加了一个域。虽然我们的许多用户都遵循该政策,但我们有第三个域,一些用户将其作为不遵循地址政策的主要地址。我需要获取这些用户并以 firstname.lastname@domain2.com 的格式添加 SMTP 地址。

例如,john.doe@domain3.com(主要)有 john.doe@domain1.com 作为别名,需要有 john.doe@domain2.com 添加为 SMTP 地址。

我有以下代码,但收到错误消息:

$Users = Get-Mailbox -ResultSize Unlimited | Where-Object {($_.PrimarySMTPAddress -like "*domain3.com*)}
foreach ($a in $Users) {
    $b = Get-User $a.Primary.SMTPAddress
    $a.EmailAddresses.Add("$($b.Firstname + "." + $b.Lastname)@domain2.com")
}
$Users |%{Set-Mailbox $_.PrimarySMTPAddress -EmailAddresses $_.EmailAddresses

错误如下:

Cannot process argument transformation on parameter 'Identity'. Cannot convert the "john.doe@domain3.com" value of type "Microsoft.Exchange.Data.SmtpAddress" to type "Microsoft.Exchange.Configuration.Tasks.UserIdParameter". + CategoryInfo : InvalidData: (:) [Get-User], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-User

Exception calling "Add" with "1" argument(s): "The address '.@domain2.com' is invalid: ".@domain2.com" isn't a valid SMTP address. The domain name can't contain spaces and it has to have a prefix and a suffix, such as example.com." At C:\_scripts\SmtpAdd.ps1:4 char:23 + $a.emailaddresses.Add <<<< ("$($b.Firstname + "." + $b.LastName)@domain2.com") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Cannot process argument transformation on parameter 'Identity'. Cannot convert the "john.due@domain3.com" value of type "Microsoft.Exchange.Data.SmtpAddress" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox

谢谢

我认为问题是从这一行开始的:

$b = Get-User $a.Primary.SMTPAddress

Get-User 无法成为 return 有效用户,因为 $a.Primary.SMTPAddress 没有 return Get-User 可以使用的类型。然后导致 EmailAddresses.Add 失败,因为 $b 为空。 Set-Mailbox 失败的原因与 Get-User

相同

改为尝试此操作(您还需要在 Set-Mailbox 行执行此操作):

$b = Get-User $a.Primary.SMTPAddress.ToString()