我们最近刚刚在我们接受的域名和电子邮件地址政策中添加了域名。虽然我们的许多用户都遵循该政策,但我们有一个第三个域,某些用户将其作为主要地址,而不遵循地址策略。我需要以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
由于
答案 0 :(得分:1)
我认为问题始于这一行:
$b = Get-User $a.Primary.SMTPAddress
Get-User
无法返回有效用户,因为$a.Primary.SMTPAddress
没有返回Get-User
可以使用的类型。这会导致EmailAddresses.Add
失败,因为$b
为空。 Set-Mailbox
失败的原因与Get-User
请尝试使用此功能(您还需要在Set-Mailbox
行执行此操作):
$b = Get-User $a.Primary.SMTPAddress.ToString()