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

通过powershell为AzureAD更新用户电子邮件地址,azure,powershell,azure-active-directory,office365,Azure,Powershell,Azure Active Directory,Office365,我想知道是否有人可以帮忙,我们组织中有一些用户使用大写的电子邮件地址,某些应用程序无法识别,因此失败。我开发了下面的脚本,将用户的UPN和显示名称更改为小写,但这不会更改他们的电子邮件地址。我不想创建新邮箱,因为有几百个用户的格式不正确。如果有人能给我指出正确的方向,那将是一个很大的帮助 #Install-module AzureAD #Connect-AzureAD $Users = Get-azureADUser -filter "userPrincipalName eq 'User1@My

我想知道是否有人可以帮忙,我们组织中有一些用户使用大写的电子邮件地址,某些应用程序无法识别,因此失败。我开发了下面的脚本,将用户的UPN和显示名称更改为小写,但这不会更改他们的电子邮件地址。我不想创建新邮箱,因为有几百个用户的格式不正确。如果有人能给我指出正确的方向,那将是一个很大的帮助

#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

我正在查看Set-AzureAduser属性,但是我无法更改此变量中的mail属性。

很好,在添加了大写字母W的域后,我遇到了相同的问题。它不会更改电子邮件地址,因为它不区分大小写

我创建此文件是为了解决此问题:

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)      
        }
     }
  }
   }
 }

我会看看我是否可以就此创建一篇博文。

@Matthew-这很好,但它不允许您更新邮件字段,只是不断出错,说它认为我在使用mail昵称。我已经尝试了几种方法来更新它,但是没有一种有效,如果有人能建议如何使用它,那肯定是可能的。谢谢你,我能问一下变量$Recipient中有什么吗?我现在正试图使用这个脚本作为上面的子脚本,这样我就可以获得primarySMTPAddress来继续你的脚本。到目前为止,我有“$Global:SM=Get Mailbox-Identity$Account'|FL'”——但这不允许您的脚本找到$\ PrimarySMTPAddress。谢谢你的帮助