Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Performs hard matching for all users within the "Office 365 Users" security group.
  2. $credential = Get-Credential  -Message "Please enter credentials for an Azure Active Directory Global Admin"
  3. Connect-MsolService -Credential $credential
  4.  
  5. $UPNSuffix = "contoso.com"
  6.  
  7. # Only match the users which are being synced with O365
  8. $users = Get-ADGroupMember -Identity "Office 365 Users"
  9. ForEach ($user in $users) {
  10.     $ADUser = $user.SamAccountName
  11.     $365User = "$ADUser@$UPNSuffix"  # Note that UPN Suffixes must match!
  12.     $guid =(Get-ADUser $ADUser).Objectguid
  13.     $immutableID=[system.convert]::ToBase64String($guid.tobytearray())
  14.     try {
  15.         $MsolUser = Get-MsolUser -UserPrincipalName "$365User"
  16.     }
  17.     catch {
  18.         Write-Host ("* Could not find the user $ADUser in Azure Active Directory! Check the recent sync results.")
  19.         continue
  20.     }
  21.     if ($MsolUser.ImmutableId -eq $immutableID) {
  22.         Write-Host ("* ID already matches for user: $ADUser. Skipping...")
  23.     }
  24.     else {
  25.         Write-Host ("* ID does not match for user: $ADUser. Changing AAD to match on-prem AD.")
  26.         Set-MsolUser -UserPrincipalName "$365User" -ImmutableId $immutableID
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement