PowerShell Script to copy Exchange GUID from Office 365 to Exchange On-prem User.

When users are been migrated from On-Prem to Office 365 using some third party tool then the on-prem user object’s Exchange GUID gets rested to “00000000-0000-0000-0000-000000000000" . This will cause problem when we need to move back the mailbox to on-prem for some reason.  Below is the code which helps to validate the On-prem users which Exchange GUID and copy back the Exchange GUID properties from Online mailbox to the Exchange On-prem user.

Set-ADServerSettings -ViewEntireForest 1
"Remotemailbox" > c:\temp\myremotemailbox.csv
get-remotemailbox  -resultsize unlimited  | %{
$upn = $_.UserPrincipalName
$proxy = $_.EmailAddresses.ProxyAddressString
$exchGuid = $_.ExchangeGuid

$mailboxlist = @()
$found = $false
    foreach($pro in $Proxy)
    {
        If($pro -like "X500:/o=ExchangeLabs/*")
        {
        $found = $true
        }
    }
    if($found -eq $true)
    {
        $upn >> c:\temp\myremotemailbox.csv
    }
    if($exchGuid -eq "00000000-0000-0000-0000-000000000000")
    {
       
        $upn >> c:\temp\myremotemailbox.csv
   
    }
}

$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session –allowclobber

$csvimport = Import-Csv C:\temp\myremotemailbox.csv
"userprincipalname,legacyExchangeDN,ExchangeGuid" > c:\temp\rmbresult.csv
Foreach($csv in $csvimport)
{
$rmaibox = $csv.remotemailbox
$mailbox = get-mailbox $rmaibox | select userprincipalname,legacyExchangeDN,ExchangeGuid
$mailbox.userprincipalname + "," + $mailbox.legacyExchangeDN + "," + $mailbox.ExchangeGuid >> c:\temp\rmbresult.csv
}

remove-PSSession $Session

$finalRM = Import-csv C:\temp\rmbresult.csv
foreach($final in $finalRM)
{
$upn = $final.userprincipalname
$eguid = $final.ExchangeGuid
$x = "X500:"  +  $final.legacyExchangeDN

if($upn -ne "")
{
get-remotemailbox $upn | Set-reMotemailbox -exchangeguid $eguid -CustomAttribute3 "Account Verified for X500-GUID" -EmailAddresses @{Add=$x}

}
}

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s