Quickly Sync all Intune Devices

A very quick script today but one which I use regularly. There is nothing worse than deploying a new policy or app and then waiting for the machines to check-in, especially if you’ve just missed a cycle.

This script runs through all devices and nudges them along.

As usual it’s on GitHub and PS Gallery

Install-Script -Name SyncAllIntuneDevices

Once logged in and authenticated to MS Graph, it’s fairly basic:

A function to sync a device:

    function SyncDevice {
        param
(
    $DeviceID
)
        $Resource = "deviceManagement/managedDevices('$DeviceID')/syncDevice"
        $uri = "https://graph.microsoft.com/Beta/$($resource)"
        write-verbose $uri
        Write-Verbose "Sending sync command to $DeviceID"
        Invoke-MSGraphRequest -Url $uri -HttpMethod POST
    }

Wrapped in a loop to go through the devices

$graphApiVersion = "beta"
$Resource = "deviceManagement/managedDevices"
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"

$devices = (Invoke-MSGraphRequest -Url $uri -HttpMethod Get).Value
foreach ($device in $devices) {
    SyncDevice -Deviceid $device.id
    $devicename = $device.deviceName
    write-host "Sync sent to $devicename"
}

Simple, but does the job!

14 thoughts on “Quickly Sync all Intune Devices”

  1. Not sure if it was just me or there was an app permissions update for Graph, but:

    I had to add “DeviceManagementManagedDevices.PrivilegedOperations.All” to line 173 “Scopes” parameter. Kept getting a 403 forbidden error, mentioning that permission. After that it worked perfectly.

    Reply
  2. Hi Andrew!

    Thanks for this script and all your work for the community.

    I was wondering if it would be possible to add this functionally to intunebackup.com?

    Reply
  3. Lines 157 and 172 throw errors for me, saying authentication needed, please call connect-MgGraph — but before the errors appear, the script says ‘Connected to Intune tenant’
    Perhaps you could be willing and able to provide guidance what I need to learn and do to make lines 157 and 172 work for me??
    Thank you, Tom

    Reply
      • That’s the odd thing…here is the code snippet showing what happens.
        Thank you for being willing to look at this

        Installing Microsoft Graph modules if required (current user scope)
        Microsoft Graph Already Installed
        Version 2 module detected
        Connected to Intune tenant
        Invoke-MgGraphRequest: D:\software\scripts\SyncAllIntuneDevices.ps1:172
        Line |
        172 | $devices = (Invoke-MgGraphRequest -Uri $uri -Method Get -OutputType P …
        | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        | Authentication needed. Please call Connect-MgGraph.
        Invoke-MgGraphRequest: D:\software\scripts\SyncAllIntuneDevices.ps1:157
        Line |
        157 | Invoke-MgGraphRequest -Uri $uri -Method Post -Body $null
        | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        | Authentication needed. Please call Connect-MgGraph.
        Sync sent to
        Disconnect-MgGraph: D:\software\scripts\SyncAllIntuneDevices.ps1:194
        Line |
        194 | Disconnect-MgGraph
        | ~~~~~~~~~~~~~~~~~~
        | No application to sign out from.

        I am not a PS expert but if the script says it’s connected to the tenant (I assume it’s connecting to ‘my’ tenant) why does it say to connect to Mg-Graph??

        I am running the script as a global admin account — is there someplace I have to look to see what MS Graph permissions are given to this account??

        Thank you for being willing to look at this.

        Reply
          • PS7 — thank you for telling me about the tenant ID part, I did not know that. First step for me is to know whether to use PS7 or PS5, next step is learning how to verify my account can connect to the tenant…
            Thank you, Tom

  4. Hello, I finally got to try this with PS 5.x within PS ISE as administrator and script runs correctly, I had had to install MgGraph for something else and the script ran correctly!! 🙂 Thank you!! 🙂

    Reply

Leave a Comment