Force domain controller to sync with AzureAD
This PowerShell script performs the following actions:
- It retrieves the Windows identity and security principal of the current user account.
- It then retrieves the security principal for the Administrator role.
- It checks if the current user is running as an administrator. If the user is not running as an administrator, the script relaunches itself as an elevated process.
- If the user is running as an administrator, the script displays a menu with three options: "Delta Sync", "Full Sync", and "Exit". The user is prompted to select an option by entering the corresponding number.
- Based on the user's selection, the script runs the appropriate command using the Start-ADSyncSyncCycle cmdlet with either the Delta or Initial policy type. If the user selects "Exit", the script exits.
- Finally, the script displays a message indicating that it is running and to check the "miisclient" to confirm. It then pauses for 10 seconds using the Start-Sleep cmdlet.
1$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
2$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID)
3$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
4
5if (-not $myWindowsPrincipal.IsInRole($adminRole)) {
6 $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"
7 $newProcess.Arguments = $myInvocation.MyCommand.Definition
8 $newProcess.Verb = "runas"
9 [System.Diagnostics.Process]::Start($newProcess)
10 exit
11}
12
13Write-Host '1) Delta Sync (Recommended, unless told to do a full sync)'
14Write-Host '2) Full Sync'
15Write-Host '3) Exit'
16
17$selected_menu_item = Read-Host 'Which number would you like to run (1 or 2)? (Enter Number and Press Enter)'
18
19switch ($selected_menu_item) {
20 1 { Start-ADSyncSyncCycle -PolicyType Delta }
21 2 { Start-ADSyncSyncCycle -PolicyType Initial }
22 3 { Write-Host 'Exit'; exit }
23 default { Write-Host 'Incorrect Input' -ForegroundColor Red }
24}
25
26Write-Host 'Running Now.... Check miisclient to confirm'
27Start-Sleep -s 10