PowerShell replaces Office365 User Primary Email address

Source: Internet
Author: User
Tags adsi edit

A recent reorganization of the company's department, the owner of all the main mail needs to be changed to a new suffix. We are using Office365, where the user information is synchronized locally to the cloud via dirsync, so it cannot be modified directly using Exchange commands and needs to be synchronized to Azure ad after the local ad is changed.


Basic ideas, modify the user and group proxyaddresses parameters, this is a large string of strings; uppercase SMTP represents the primary address, and the lowercase one represents the alias. The O365 that are synchronized after the modification is OK.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/C4/wKiom1cr2fOyorjPAABZMSh53cU958.png "title=" add1. PNG "alt=" Wkiom1cr2foyorjpaabzmsh53cu958.png "/>


The new e-mail addresses of these users have been added as aliases in the following way, so it is only necessary to exchange them at the specified time and the current primary address. These changes can be viewed through ADSI edit's interface to view the properties.

Set-aduser user-add @{proxyaddresses= "Smtp:xxxxxx"}


The Exchange code is as follows

#首先我得获取一个名单, this list is part of the existing ad, and part of it is sent to me via Excel [Email protected] () $users =get-aduser -filter { proxyaddresses -like  "*old.com.au*"} -properties proxyaddresses -searchbase  "ou= Old,ou=melbourne,dc=test,dc=com,dc=au "foreach (  $user  in  $users) {foreach  ($address  in   $user. proxyaddresses) {   if ($address  -like  "* @old. com.au*") {        $rappaddress = $address. Substring (5)    break;      }   } $temp =[ pscustomobject]@{"Full name" = $user. Name; " Current email "= $rappaddress} $result + = $temp}# $result $a=import-csv c:\temp\newuserlist.csv |  select  "Full name",  "current email" $oo = $result + $a # After getting the list, for the list of current users   I wanted to get their primary email address, so I wrote a functionfunction get-primarysmtp () {    [cmdletbinding ()]         Param     (        # Param1 help description         [parameter (mandatory= $true,                    valuefrompipelinebypropertyname= $true,                     position=0)]        [string[]]          $users     ) $pp = $null [email protected]{' name ' = $null; ' Primarysmtp ' = $null} $obj =new-object -typename psobject -property  $pp  [email  Protected] () foreach ($user  in  $users) {$info =get-aduser -filter {name -eq  $user}  -Properties proxyaddresses$primarySMTPAddress =  "" foreach  ($address  in $ info.proxyaddresses) {    if  (($address. length -gt 5)  -and  ($address. SubString (0,5)  -ceq  ' SMTP: ')  )     {          $primarySMTPAddress  =  $address. SubString (5)                  BREAK   &NBSP}} $objtemp = $obj  | select * $objtemp. Name= $info. name$objtemp.primarysmtp= $primarySMTPAddress $result+= $objtemp} $result  } #执行看看   The current user's primary SMTP address get-primarysmtp -users  $users # for each user's address to cycle, if it is uppercase and then become lowercase, if the lowercase is exactly what I need, then change to uppercase $users = $oo  | sort  "full name" |select -expandproperty  "Full name" foreach ($user  in  $users) {$info =get-aduser -filter {name -eq  $user} -properties  Proxyaddresses$filter= "SMTP:" + $info. Givenname+ "." + $info. surname+ "@new. com" [email protected] () foreach ($address  in  $info. proxyaddresses) {$temp = $addressif ($address  -clike  "smtp*") {$temp = $address. ToLower ()}if ($address  -like  $filter) {$temp = $address. Substring (0,4). ToUpper () + $address. Substring (4). ToLower ()} $new + = $temp}write-host  "---------------------------"  -ForegroundColor  cyan   $newset-aduser  $info. samaccountname -replace @{proxyaddresses= $new} -confirm} #然后同步一下DCrepadmin  /syncall  syddc01 dc=omnicom,dc=com,dc=au /d /e /a  #再看看新的的SMTP主地址Get-primarysmtp -users   $users # Sync to o365import-module dirsyncstart-onlinecoexistencesync# via azure ad  Module Query O365 's mailbox to see if it has been updated $users | get-mailbox | select name, primarysmtpaddress



This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1770618

PowerShell replaces Office365 User Primary Email address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.