Powershell modifies Office365 and AD accounts

Source: Internet
Author: User
Tags samaccountname

Some of the attributes of the ad user, such as mail, UPN, login, and so on, need to be modified to be consistent with Office365 's login account during this time. Wrote a simple script for batch modification.


#Import AD moduleimport-module activedirectory#import Office 365 module$sessions=get-pssessionif ($ Sessions.computername-like "outlook.office365.com") {write-host "detecting current Office365 session, skip:"-foregro Undcolor cyan}else{write-host "starting new Office365 session"-foregroundcolor Cyan $UserCredential = get-cred Ential connect-msolservice-credential $UserCredential $Session = New-pssession-configurationname Microsoft.exchan Ge-connectionuri Https://outlook.office365.com/powershell-liveid/-credential $UserCredential-authentication Basic        -allowredirection import-pssession $Session} #Get Primary SMTP addressfunction get-primarysmtp () {[Cmdletbinding ()] Param (# Param1 Help description [Parameter (mandatory= $true, Valuefrompipelineby Propertyname= $true, position=0)] [string[] $users) $pp = $null [Email protec ted]{' name ' = $null; ' Primarysmtp ' = $null} $obj =new-oBject-typename psobject-property $pp [email protected] () foreach ($user in $users) {$info =get-aduser-filt er {name-eq $user}-properties proxyaddresses $primarySMTPAddress = "" foreach ($address in $info. Proxyaddresse s) {if ($address. LENGTH-GT 5)-and ($address. SubString (0,5)-ceq ' SMTP: ') {$primarySMTPAddress = $address. SubString (5) Break}} $objtemp = $obj | SELECT * $objtemp. name= $info. Name $objtemp. primarysmtp= $primarySMTPAddress $result + = $objtemp} return $result} #Get AD User informtion# $ADU Sers = get-aduser-searchbase "Ou=mango,ou=ddb_group,ou=melbourne,dc=omnicom,dc=com,dc=au"-Properties proxyaddresses, EmailAddress, Displayname-filter *write-host "" $uName =read-host "please input User AD name" $ADUsers =get -aduser $uName-properties proxyaddresses, EmailAddress, Displayname#change sAMAccountName and UPNforeach ($ADUser in $AD Users) {$ADUser. Name$givenname = $ADUser.Givenname$surname = $ADUser. Surnameif (($GivenName-ne $null)-or ($SurName-ne $null)) {$newSAM = $GivenName. ToLower () + ' .‘ + $SurName. ToLower () $oldUPN = $ADUser. userPrincipalName $domainName = $oldUPN. Split (' @ ') [1] $newUPN = $newSAM + ' @ ' + $domainName write-host "Updating adupn: $OLDUPN, $newUPN"-foregroundcolor Cyan #Ch Ange AD UPN and Samaccountset-aduser $ADUser-samaccountname $newSAM-userprincipalname $newUPN #Ch  Ange AD Email $oldEmail = $ADUser. EmailAddress $newEmail = $newSAM + ' @ ' + $oldemail. Split (' @ ') [1] Write-host        "Updating Email: $oldEmail $newEmail"-foregroundcolor Cyan set-aduser $newSAM-emailaddress $newEmail #Change Primary SMTP $primary =get-primarysmtp-users $ADUser. Name | Select-expandproperty primarysmtp write-host "Updating proxyaddress:"-foregroundcolor Cyan #Write-host "C           Urrent Primary address is $primary "-foregroundcolor Cyan     $Aduser. Proxyaddresses.remove ("SMTP:" + $primary) $Aduser. Proxyaddresses.add ("SMTP:" + $primary) $A Duser.proxyaddresses.add ("SMTP:" + $newEmail) set-aduser $newSAM-replace @{proxyaddresses=[string[]] $ADUser. Proxyad Dresses} #Change Cloud UPN.        If Office365 session is not connected properly, follow commands wont ' work! $OLDMSOLUPN =get-msoluser-searchstring $ADUser. Name $OLDMSOLUPN = $OLDMSOLUPN | Select-first 1 | Select-expandproperty userPrincipalName $newmsolupn = $newSAM + ' @ ' + $oldmsolupn. Split (' @ ') [1] write-host "Updat ing MSOLUPN: $oldmsolupn $newmsolupn "-foregroundcolor Cyan set-msoluserprincipalname-userprincipalname $old Msolupn-newuserprincipalname $newmsolupn write-host ""} else{write-warning "either GivenName or Surnam E is Empty "}} #Confirm result write-host" Confirm AD result "-foregroundcolor cyanget-aduser $newSAM-properties p Roxyaddresses,mail | Select NamE, sAMAccountName, userPrincipalName, proxyaddresses, mailwrite-host "Confirm O365 Result"-foregroundcolor Cyanget-msoluser-searchstring $ADUser. Name | Select userPrincipalName


The changes are actually full and simple, and my script doesn't write too many fault-tolerant handles. After the modification, the Windows user may have problems with the profile and registry alignment, so you need to modify some of the registry, as a reference http://beanxyz.blog.51cto.com/5570417/1930788



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

Powershell modifies Office365 and AD accounts

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.