I'm trying to run a capture of devices in AAD that have not checked in for 90+ days then remove the corresponding AD devices. Using scripts deployed through the Task Scheduler.
The process is currently two separate scripts. The first script connects to AAD, searches for devices that have not checked in for 90+ days, and then writes the output to a file.
The second script pulls the DeviceIDs from the file and uses them to remove matching devices from our local AD.
Both of the scripts work when run manually using the -WhatIf parameter and dummy data. But when I load them into a scheduled task, the process fails to make the scripted changes.
In the TaskScheduler logs I see the following events.
Task Triggered by User (Event ID 110)
Action Complete (Event ID 201)
Created Task Process (Event ID 129)
Action Started (Event ID 200)
Action Completed (Event ID 201)
Task Completed (Event ID 102).
Both of the Action Completed entries contain the following (redacted).
Task Scheduler successfully completed task "Task Name", instance "{Instance ID}", action "Powershell.exe" with return code 2147942401.
The Created Taks process shows the following (redacted).
Task Scheduler launch task "Task Name", instance "Powershell.exe" with process ID 5808.
The task is being run on a Windows Server 2016 utility server, using a service account that has been delegated Create and Remove Device permissions in AD, as well as local admin permissions to make sure it's not getting stopped by local security policies. In the Last Run Result column, it shows 0x1,
As far as I can tell the task has everything it needs to run, and it even says that it's succeeding. But the changes are not being made.
Does anyone have any idea why this scheduled task is not performing as advertised?