Skip to content

Can't be used on non-Windows devices // and a script error #2

@christianahug

Description

@christianahug

This is very neat. Unfortunately, it only works for Windows devices as the Update-MgDevice command doesn't work with other OS (a known API issue - I hope it will be resolved at some point soon).

I ran into an issue with the script. If there are no stale (or pending) devices, it throws an error because the .count parameter doesn't return a value. I had to adjust the script a bit to make it work.

Starting at line 135

#Get Pending Devices to disable
try{$pendingdevices = Get-MgDevice -All -Filter "ApproximateLastSignInDateTime le $($disableDate) AND ApproximateLastSignInDateTime ge $($deleteDate)"
if ($pendingdevices -ne $null){
$pendingDevicesNumber = ($pendingdevices | Measure).Count
write-verbose "$(Get-Date -Format 'yyyy-MM-dd'),$(Get-Date -format 'HH:mm:ss'),Success to get $($pendingDevicesNumber) Pending Devices to disable"
}
else {
write-verbose "$(Get-Date -Format 'yyyy-MM-dd'),$(Get-Date -format 'HH:mm:ss'),No Pending Devices to disable"
}
}
catch{write-Error "$(Get-Date -Format 'yyyy-MM-dd'),$(Get-Date -format 'HH:mm:ss'),Failed to get Pending Devices with error: $_"}

#Get Stale Devices to delete
try{$staledevices = Get-MgDevice -All -Filter "ApproximateLastSignInDateTime le $($deleteDate)"
if ($staledevices -ne $null){
$staleDevicesNumber = ($staledevices | Measure).Count
write-verbose "$(Get-Date -Format 'yyyy-MM-dd'),$(Get-Date -format 'HH:mm:ss'),Success to get $($staleDevicesNumber) Stale Devices to disable"
}
else {
write-verbose "$(Get-Date -Format 'yyyy-MM-dd'),$(Get-Date -format 'HH:mm:ss'),No Stale Devices to disable"
}
}
catch{write-Error "$(Get-Date -Format 'yyyy-MM-dd'),$(Get-Date -format 'HH:mm:ss'),Failed to get Stale Devices with error: $_"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions