简体   繁体   English

通过 AzureAD 的 powershell 更新用户电子邮件地址

[英]Update users email address through powershell for AzureAD

I was wondering if someone could help, we have some users in our organisation who are using uppercase email address, certain applications can't recognize this and fail.我想知道是否有人可以提供帮助,我们组织中有一些用户使用大写电子邮件地址,某些应用程序无法识别并失败。 I have developed the script below to change the users UPN and display names to lowercase, however this doesn't change their email address.我开发了下面的脚本来将用户 UPN 和显示名称更改为小写,但这不会更改他们的电子邮件地址。 I don't want to create new mailboxes as there are several hundred users that have the incorrect format.我不想创建新邮箱,因为有数百个用户的格式不正确。 If anyone could point me in the right direction, that would be a great help.如果有人能指出我正确的方向,那将是一个很大的帮助。

#Install-module AzureAD
#Connect-AzureAD
$Users = Get-azureADUser -filter "userPrincipalName eq 'User1@MyDomain.com'" 
$ErrorActionPreference = 'Stop'
$OutputObj  = New-Object -TypeName PSobject
$OutputObj1 = New-Object -TypeName PSobject
$SuccessOutput = @() 
$ErrorOutput = @() 
$Case = "toLower"
Function ChangeToLowerCase{

Try{
foreach ($user in $users) {
 $user | Set-AzureADUser -userPrincipalName $user.userPrincipalName.$Case()
 $user | Set-AzureADUser -identity $user.SipProxyAddress.$Case()


 #$User | Select-Object DisplayName, userPrincipalName,Mail
 $OutputObj  | Add-Member -memberType NoteProperty -name "Display Name" -value $($user.DisplayName)
 $OutputObj  | Add-Member -memberType NoteProperty -name "UPN" -value $($user.userPrincipalName)
 $OutputObj  | Add-Member -memberType NoteProperty -name "Email Address" -value $($user.Mail)
 $SuccessOutput +=$OutputObj
 $SuccessOutput | Export-csv "C:\Temp\Powershell\Completed\UPN Changes.csv" -NoTypeInformation -NoClobber -Append
  }
}
Catch{
$OutputObj1 | Add-Member -memberType NoteProperty -name "Display Name" -value $($user.DisplayName)
$OutputObj1 | Add-Member -memberType NoteProperty -name "Error Message" -value $($_.Exception.Message)
$ErrorOutput +=$OutputObj1
$ErrorOutput | Export-csv "C:\Temp\Powershell\Errors\UPN Changes Error.csv" -NoTypeInformation -NoClobber -Append
}
}
ChangeToLowerCase

I am looking at the Set-AzureAduser property, however I am unable to change the mail property in this variable.我正在查看 Set-AzureAduser 属性,但是我无法更改此变量中的邮件属性。

Nice, I had the same issue after we added a domain with a capital W. It wont change the email address because it's not case sensitive.很好,在我们添加了一个大写 W 的域后,我遇到了同样的问题。它不会更改电子邮件地址,因为它不区分大小写。

I created this to resolve the issue:我创建了这个来解决这个问题:

function ConvertTo-LowerW
{

  foreach ($Recipient in $Recipients)
  { 
  "1"
  $Recipient.PrimarySmtpAddress

  $SplitEmail = $Recipient.PrimarySmtpAddress.Split('@')
  $BeforeA = $SplitEmail | Select-Object -First 1
  $AfterA = ($SplitEmail | Select-Object -Last 1).ToLower()
  $NewPrimarySmtpAddress = $BeforeA + '@' + $AfterA

  $NewPrimarySmtpAddress

  switch ($Recipient.RecipientType)
  {

     'UserMailbox'
     {
        try
        {
           $Addresses = (Get-Mailbox -Identity $Recipient.PrimarySmtpAddress  -ErrorAction Stop).EmailAddresses
           $Addresses = $Addresses.Replace("SMTP:$($recipient.PrimarySmtpAddress)", "")

           Set-Mailbox -ErrorAction Stop `
              -Identity $Recipient.PrimarySmtpAddress `
              -EmailAddresses $NewPrimarySmtpAddress 

           if ($Addresses.Length -ne 0)
           {
              foreach ($Address in ($Addresses | Where-Object { $_.Length -ne 0 }) )
              {
                 $Address
                 if ($Address.Length -ne 0 )
                 {
                    Set-Mailbox -ErrorAction Stop `
                       -Identity $Recipient.PrimarySmtpAddress `
                       -EmailAddresses @{add = "$Address" } 
                 }
              }
           }
        }

        Catch
        {
           $ErrorList.Add($Recipient.PrimarySmtpAddress)
           $ErrorList.Add($_.Exception.Message)      

        }

     }

     'MailUniversalDistributionGroup'
     {
        try
        {
           $Addresses = (Get-DistributionGroup -Identity $Recipient.PrimarySmtpAddress -ErrorAction Stop).EmailAddresses
           $Addresses = $Addresses.Replace("SMTP:$($recipient.PrimarySmtpAddress)", "")

           Set-DistributionGroup -ErrorAction Stop `
              -Identity $Recipient.PrimarySmtpAddress `
              -EmailAddresses $NewPrimarySmtpAddress 

           if ($Addresses.Length -ne 0)
           {
              foreach ($Address in  ($Addresses | Where-Object { $Addresses.Length -ne 0 }))
              {
                 if ($Address.Length -ne 0 )
                 {
                    Set-DistributionGroup -ErrorAction Stop `
                       -Identity $Recipient.PrimarySmtpAddress `
                       -EmailAddresses @{add = "$Address" } 
                 }
              }
           }
        }
        catch
        {
           $ErrorList.Add($Recipient.PrimarySmtpAddress)
           $ErrorList.Add($_.Exception.Message)      
        }

     }

     'DynamicDistributionGroup'
     {
        try
        {
           $Addresses = (Get-DynamicDistributionGroup -Identity $Recipient.PrimarySmtpAddress -ErrorAction Stop).EmailAddresses
           $Addresses = $Addresses.Replace("SMTP:$($recipient.PrimarySmtpAddress)", "")

           Set-DynamicDistributionGroup -ErrorAction Stop `
              -Identity $Recipient.PrimarySmtpAddress `
              -EmailAddresses $NewPrimarySmtpAddress 

           if ($Addresses.Length -ne 0)
           {
              foreach ($Address in  ($Addresses | Where-Object { $Addresses.Length -ne 0 }))
              {
                 $Address
                 if ($Address.Length -ne 0 )
                 {
                    Set-DynamicDistributionGroup -ErrorAction Stop `
                       -Identity $Recipient.PrimarySmtpAddress `
                       -EmailAddresses @{add = "$Address" } 
                 }
              }
           }
        }
        catch
        {
           $ErrorList.Add($Recipient.PrimarySmtpAddress)
           $ErrorList.Add($_.Exception.Message)      
        }
     }

     Default
     {
        try
        {

           $Recipient.RecipientType
           $Recipient.RecipientTypeDetails
           'User has an unrecognized Recipienttype'

           Send-Email `
              -To $to `
              -Body 'Test' `
              -Subject 'User has an unrecognized Recipienttype' 
        }
        catch
        {
           $ErrorList += "$($($Recipient).PrimarySmtpAddress) has an unrecognized RecipientType $($($Recipient).Recipienttype)"
           $ErrorList.Add($Recipient.PrimarySmtpAddress)
           $ErrorList.Add($_.Exception.Message)      
        }
     }
  }
   }
 }

I'll see if I can create a blog post on bwit.blog regarding this.我会看看我是否可以在bwit.blog上创建一篇关于此的博客文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM