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

问题描述 投票:0回答:1

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

#Install-module AzureAD
#Connect-AzureAD
$Users = Get-azureADUser -filter "userPrincipalName eq '[email protected]'" 
$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属性。

azure powershell azure-active-directory office365
1个回答
0
投票

很好,在我们添加了一个大写字母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)      
        }
     }
  }
   }
 }

我会否在bwit.blog上创建与此相关的博客文章。

© www.soinside.com 2019 - 2024. All rights reserved.