This script connects to Microsoft Graph, retrieves all Windows devices, and sends a synchronization request to each device. The script:
- Prompts for Tenant ID: It asks the user to input the Tenant ID or primary domain.
- Connects to Microsoft Graph: Authenticates to Microsoft Graph using the provided tenant ID and the required scopes, including privileges for managing devices.
- Fetches Windows Devices: Retrieves all managed devices running Windows OS.
- Syncs Devices: Sends a synchronization request to each Windows device found, displaying the progress.
Here is the Script:
$TenantId = Read-Host "Please enter Tenant Id or Primary domain"
$Scopes = "DeviceManagementManagedDevices.ReadWrite.All, DeviceManagementManagedDevices.PrivilegedOperations.All"
Connect-MgGraph -TenantId $TenantId -Scopes $Scopes
$Devices = Get-MgDeviceManagementManagedDevice -All | Where-Object {$_.operatingSystem -eq 'Windows'}
Write-host " Number of Devices found: $($Devices.id.Count)" -ForegroundColor cyan
Foreach ($Device in $Devices) {
$DeviceId = $Device.id
Sync-MgDeviceManagementManagedDevice -ManagedDeviceId $DeviceId
Write-Host "Sending Sync request to Device $($Device.deviceName)" -ForegroundColor Yellow
}