UPN Flip for Office 365 Users

One of our Hybrid client has encountered a situation where ones the user is migrated to Exchange online, free busy doesn’t work when user tries to fetch it for Non migrated user but it works if other user is on Exchange online. (this is occurring for many users)

On working with Microsoft, We found that there is some inconsistency between back-end Azure directory and Office 365 directory.

Only solution that has been provided is to flip the UserPrincipalName and than Flip it back to original.

For Example:

Setting Temp UPN

Set-MsolUserPrincipalName -UserPrincipalName VikasS@labtest.com -NewUserPrincipalName VikasS@labtest.onmicrosoft.com

 

Setting back to Orignal UPN

 

Set-MsolUserPrincipalName -UserPrincipalName VikasS@labtest.onmicrosoft.com -NewUserPrincipalName VikasS@labtest.com

 

This has resolved the free busy issue but we want to fix it proactively for all  users so we coordinated and got a list of more than 16000 users from Microsoft that can have this issue in future ones they are migrated.
We have written this script that can be downloaded and extracted from below link which will take input from text file and will do the UPN flip operation in bulk.

 

Update the FlipUPNfromTextFile.ps1 powershell script file –> highlighted variable Section  –> onmicrosoft domain

 

####################Variables/Logs###########################
$log = Write-Log -Name “FlipUPN” -folder “logs” -Ext “log”
$Report = Write-Log -Name “FlipUPN” -folder “Report” -Ext “csv”
$hybdomain = “LABtest.onmicrosoft.com”
$collection = @()
$users = get-content .\Users.txt
########################################################

 

Prerequisites for this script: Exchange 2010 Shell, MSOL module
Update the Users.txt file with UserPrincipalNames that needs UPN flip on the office 365 tenant.

 

Now just run the script in powershell from the script location.
It will ask you for login to office 365 (Please use Global Admin Credential)

 

 

It will start fliping the UserPrincipal Names and will save the report in reporting folder. (Transcript logs are stored in logs folder)
Report Snippet:

 

<#     
    .NOTES 
    =========================================================================== 
     Created with:     VS Code 
     Created on:       8/6/2018 
     Created by:       Vikas Sukhija (http://SysCloudPro.com) 
     Organization:      
     Filename:         FlipUPNfromTextFile.ps1 
    =========================================================================== 
    .DESCRIPTION 
        This solution will take input from text file and Filp the UPN and FLIP is back again. 
#> 
$error.clear() 
try { stop-transcript | out-null } 
catch { $error.clear() } 
function Write-Log { 
    [CmdletBinding()] 
    param 
    ( 
        [Parameter(Mandatory = $true)] 
        [array]$Name, 
        [Parameter(Mandatory = $true)] 
        [string]$Ext, 
        [Parameter(Mandatory = $true)] 
        [string]$folder 
    ) 
     
    $log = @() 
    $date1 = get-date -format d 
    $date1 = $date1.ToString().Replace("/""-") 
    $time = get-date -format t 
     
    $time = $time.ToString().Replace(":""-") 
    $time = $time.ToString().Replace(" """) 
     
    foreach ($n in $name) { 
         
        $log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext" 
    } 
    return $log 
} 
 
function LaunchMSOL 
    { 
        param 
        ( 
            $UserCredential 
        ) 
        import-module msonline 
        Write-Host "Enter MS Online Credentials" -ForegroundColor Green 
        Connect-MsolService -Credential $UserCredential 
    } 
     
    Function RemoveMSOL 
    { 
         
        Write-host "Close Powershell Window - No disconnect available" -ForegroundColor yellow 
    } 
 
    If ((Get-PSSnapin | where { $_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010" }) -eq $null) 
    { 
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
    } 
    if ($error) { Write-host "Error loading Modules and functions" -foregroundcolor green ; exit } 
 
####################Variables/Logs########################### 
$log = Write-Log -Name "FlipUPN" -folder "logs" -Ext "log" 
$Report = Write-Log -Name "FlipUPN" -folder "Report" -Ext "csv" 
$hybdomain = "Labtest.onmicrosoft.com" 
$collection = @() 
$users = get-content .\Users.txt 
############################################################## 
Start-Transcript $log 
LaunchMSOL 
$users | ForEach-Object{ 
    $mcoll = "" | Select User,Alias,tempupn,OrignalUPN,Status 
$checkeolmbx = Get-Mailbox -Identity $_ -ea silentlycontinue #switch to get-Remotemailbox if users are on exchange online. 
if($checkeolmbx){ 
    $Error.clear() 
    try 
    { 
        $alias = $checkeolmbx.Alias 
        $tempupn = $alias + "@" + $hybdomain 
        $tempupn 
        $orignalUPN = $checkeolmbx.UserPrincipalName 
        $orignalUPN 
        Set-MsolUserPrincipalName -UserPrincipalName $orignalUPN -NewUserPrincipalName $tempupn 
        Write-Host "------temp upn Set $tempupn ----------- " -ForegroundColor Yellow 
        timeout 5 
        Set-MsolUserPrincipalName -UserPrincipalName $tempupn -NewUserPrincipalName $orignalUPN 
        Write-Host "------Orignal upn Set $orignalUPN ----------- " -ForegroundColor Green 
        $mcoll.User = $_ 
        $mcoll.Alias = $alias 
        $mcoll.tempupn = $tempupn 
        $mcoll.Orignalupn = $orignalUPN 
        if ($error) 
        { 
            Write-Host "Error has occured" -ForegroundColor Red 
            $mcoll.Status = "Error" 
            $Error.clear() 
        }else{ 
            $mcoll.Status = "Success" 
        } 
         
    } 
        catch{ 
            $_.Exception 
            Write-Host "exception occured Flipping UPN" -ForegroundColor Yellow 
        } 
} 
$collection+$mcoll 
} 
$collection | export-csv $Report -NoTypeInformation 
RemoveMSOL 
Stop-Transcript 
###########################################################################

 

Thanks for downloading
Sukhija Vikas
Advertisements

2 thoughts on “UPN Flip for Office 365 Users

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s