PowerShell Script Running successfully in ISE but Failing as a Scheduled Task

Maranya, Damon 321 Reputation points
2023-01-10T21:25:26.453+00:00

I am working on a script that takes a list of DIDs/GUIDIds from a locally stored file generated by another script. It then removes the listed devices from Active Directory. But it won't run as a scheduled task.

The Last Run Result status is 0x1. Which as far as I can tell is a vague catchall value meaning "I can't". There is nothing in the History tab at all.

I am logged in with the account that will be used to run task in the Task Scheduler. I've already delegated delete device permissions to the account that will run the script, and it currently completes without error when I run it manually in ISE. During the manual runs I am using the -whatif parameter, so that I don't lose the devices that the first script found. But that shouldn't affect the result when running it without the parameter in a scheduled task.

The Task Scheduler logs show task started>action started>action completed> task completed. The PowerShell logs show PowerShell Console Startup>PowerShell Named Pipe IPC>PowerShell Console Startup. There are no events after PowerShell Console Startup.

Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,533 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,708 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,572 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,676 Reputation points
    2023-01-10T23:36:14.543+00:00

    Add a transcript to the script so that you can see the errors that Powershell generates.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1

    Start-Transcript -path C:\Logs\MyScriptLog.txt
    
    .... the rest of your script
    
    
    Stop-Transcript 
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.