Getting Powershell Return Code 2147942401

Maranya, Damon 326 Reputation points
2022-12-27T20:20:57.513+00:00

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?

Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,667 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,970 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
25,003 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,896 Reputation points
    2022-12-27T22:36:44.643+00:00

    When you run those scripts "manually" are you doing so while logged in as the account under which the scheduled tasks run?

    That "2147942401" error is really 0x80070001.

    Try adding a Start-Transcript and End-Transcript cmdlet to both of those scripts and add Write-Host cmdlets to the script to describe what you're about to do. If the script starts the transcript file will tell you the last Write-Host which should give you an idea of where the problem is happening.


2 additional answers

Sort by: Most helpful
  1. Maranya, Damon 326 Reputation points
    2022-12-28T17:07:53.16+00:00

    The issue here appears to have been something of a perfect storm small problems aligning to break everything.

    The main problem was that the server running the script had not been updated after the TLS 1.0 & 1.1 deprecation. Which was breaking the installation of new modules. That combined with a couple of outdated modules (like PowershellGet), meant that nothing was getting done and nothing was getting logged.

    Adding the Start-Transcript & Stop-Transcript cmdlets showed the errors indicating missing cmdlets and from there I was able to track down the TLS deprecation issue.

    Once Powershell was updated to use TLS 1.2, I was able to import the required modules and it worked as expected once again.

    0 comments No comments

  2. W Likert 0 Reputation points
    2025-05-30T16:46:59.2966667+00:00

    I ran into this problem for scheduled tasks running as system. You know how when you manually run a scheduled task you have to type the corresponding letter to run, not run or suspend? Well I found if I run the cmdlet Unblock-File script_name that it makes it so the script can run unattended.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.