Last active
January 12, 2023 20:46
-
-
Save clutch70/6a71701ad30caafb417eeeda9ee27554 to your computer and use it in GitHub Desktop.
Sets the required params for AD to Azure user sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#EXAMPLE | |
#.\Fix-AzureUserSync.ps1 -azSam azureUsername -adSam adUsername -emailDomain test.com | |
param ( | |
[Parameter(Mandatory=$false)][string]$azSam, | |
[Parameter(Mandatory=$false)][string]$adSam, | |
[Parameter(Mandatory=$false)][string]$emailDomain, | |
[Parameter(Mandatory=$false)][switch]$help | |
) | |
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | |
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator | |
if ($currentPrincipal.IsInRole($adminRole)) { | |
Write-Host "Script is running as an administrator" | |
} else { | |
Write-Error "Script is not running as an administrator" | |
return | |
} | |
$test = 0 | |
if ($help) { | |
Write-Host "This script connects to Azure AD and On-Premises Active Directory, | |
searches for a user with the specified samAccountName, | |
and then sets the specified alternate email address and immutableId for the Azure AD user. | |
EXAMPLE | |
.\Fix-AzureUserSync.ps1 -azSam azureUsername -adSam adUsername -emailDomain test.com | |
" | |
return | |
} | |
if (!$azSam -or !$adSam -or !$emailDomain) { | |
Write-Error "Please provide all the required parameters: azSam, adSam, emailDomain" | |
return | |
} | |
#Check if MSOlService is already connected and don't sign in again if we don't have to | |
$test = get-msoldomain -ErrorAction SilentlyContinue | |
IF ($test.count -eq 0) | |
{ | |
Connect-MsolService | |
} | |
#Connect-MsolService | |
$azUser = Get-MsolUser -SearchString $azSam | |
if ($azUser -eq $null) { | |
Write-Error "Azure AD user with username $azSam not found." | |
return | |
} | |
$user = get-aduser -Identity $adSam -Properties ObjectGUID | |
if ($user -eq $null) { | |
Write-Error "On-premises AD user with username $adSam not found." | |
return | |
} | |
$newId = [system.convert]::ToBase64String(([GUID]($user.ObjectGUID)).tobytearray()) | |
$azUser | Set-MsolUser -ImmutableId $newId | |
if ($?) { | |
Write-Host "Successfully set immutableId for Azure AD user $($azUser.UserPrincipalName)." | |
} else { | |
Write-Error "Failed to set immutableId for Azure AD user $($azUser.UserPrincipalName)." | |
} | |
$email = $adSam + "@" + $emailDomain | |
$azUser | Set-MsolUser -AlternateEmailAddresses $email | |
if ($?) { | |
Write-Host "Successfully set alternate email address for Azure AD user $($azUser.UserPrincipalName)." | |
} else { | |
Write-Error "Failed to set alternate email address for Azure AD user $($azUser.UserPrincipalName)." | |
} | |
Get-AdUser -Identity $adSam | Set-AdUser -UserPrincipalName $email |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment