Script that sets Immutable ID for O365

Viewed 688

I hope you can help and I apologize right off the bat if this has been answered before but I cannot find any info.

I want to export a list of users from on-prem AD and convert their ObjectGUID to and Immutable ID. (This I have accomplished)

I then want to take that exported info and use it so set the Immutable ID for each of the users's O365 accounts in preparation for Azure integration. I want to do this so I can enforce a hard match to avoid duplicate accounts etc.

Below is the code I have thus far and basically need help in completing it using for loops etc. (I am guessing)

Import-Module ActiveDirectory
$Users=Get-ADUser -Filter *
 
function guidtobase64
{
    param($str);
    $g = new-object -TypeName System.Guid -ArgumentList $str;
    $b64 = [System.Convert]::ToBase64String($g.ToByteArray());
    return $b64;
}

$Users | Select UserPrincipalName,@{Expression={(guidtobase64($_.ObjectGUID))}; Label="ImmutableID"} | Export-Csv c:\AzureSync_ProcessingOps.csv -Delimiter ';' -NoTypeInformation
1 Answers

I managed to get it working so I thought I would post the answer should anyone else need to ever do hard matching for O365 syncing.

Import-Module ActiveDirectory
$Users=Get-ADUser -Filter *

function guidtobase64
{
    param($str);
    $g = new-object -TypeName System.Guid -ArgumentList $str;
    $b64 = [System.Convert]::ToBase64String($g.ToByteArray());
    return $b64;
}

$Users | Select UserPrincipalName,@{Expression={(guidtobase64($_.ObjectGUID))}; Label="ImmutableID"}

Connect-MsolService

foreach ($User in $Users)
{
    try 
    {
        $UPN = $User.UserPrincipalName
        $ImmutableID = $User.ImmutableID
        Get-MsolUser -UserPrincipalName $UPN | Set-MsolUser -ImmutableId $ImmutableId
    }
    
    catch
    {
    Write-Output $_.Exception.Message
    }

}
Related