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!
Thank you. It worked amazingly!
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!! 🙂
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
Hi, do you get a popup box asking for you to authenticate?
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.
It should list a tenant ID after “Connected to Intune tenant” so something is failing. Are you running in PS5 or 7?
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
Try PS5 first, it has better compatibility for modules.
You can also try just connect-mggraph to see if the general connection works
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?
Yes, I could add it on, I’ll have to check if there are any throttling implications though as the requests will all be coming from the same place across all tenants
👍
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.
Thank you, just added to version 3.6
Stops at 1000, you know how to work around that?
I’ve just added a new version to Github with support for pagination, can you see if that works?