Programmatically Creating a Scheduled Task in Windows - 0x8004130f Error - scheduled-tasks

I am having major problems getting a scheduled task to run. The task is created fine using the ITaskScheduler interface provided by Microsoft. The task is set to run only if a user is logged on.
The problem is that the task does NOT run and the log file SchedLgU.txt cites the reason as being:
"The attempt to retrieve account information for the specified task failed; therefore, the task did not run. Either an error occurred, or no account information existed for the task.
The specific error is:
0x8004130f: No account information could be found in the Task Scheduler security database for the task indicated."
I know for a fact that a scheduled task can be created with no account information because the Google Updater scheduled task does this, and it runs fine.
All I can gather from web searches is that Windows has a "scheduled task database" that needs to have credential information for each task for it to be able to run.
Does anyone know how to modify this credential database or any other way to get my task to run?

The GoogleUpdateTaskMachine Task uses the "NT AUTHORITY\SYSTEM" to Run the task. You can do this by using schtasks command and the /ru switch with "System" or "" as the parameter. Like this:
schtasks /create /tn "My App" /tr c:\apps\myapp.exe /sc monthly /d 15 /ru "System"
It does not prompt for a password.
See technet for more info.

you could use batch scripting.
schtasks /create /tn UNO /tr YOURAPP.EXE /sc HOURLY /mo 2
there you go.
read: How to use Schtasks.exe to Schedule Tasks in Windows Server 2003

As far as I know, and the documentation backs this up, an account is required for a scheduled task. If you set the task to run only when a user is logged in that only means a password is not necessary, you still need an account. This account can be set to a group so that the task will be run when it is triggered as long as anyone in the group is logged on. For instance you can set the account to 'Users' so that the task can run when anyone is logged on.

Related

Powershell Script to create Scheduled Task on non domain computers

We are trying to execute a powershell script that will import a scheduled task to non domain and domain joined computers in our organization. The task is simply to disable wifi adapter when an ethernet cable is connected. I exported the task as a .xml and then can get the task to run as admin and complete successfully. However when we attempt to run logged in as normal user, the UAC box pops up asking for admin username and password. I created the task as both domain admin and local admin accounts and tried both, with same result when running the .bat as normal user.
This is my script so far (I have no previous experience in writing powershell):
Copy-Item -Path D:\WiFi -Destination C:\PCM\Utils -Recurse;
schtasks /create /tn "Ethernet On-Disable Wifi" /xml "C:\PCM\Utils\WiFi\Ethernet On-Disable Wifi.xml" /ru Domain\admin /rp domainpw;
schtasks /create /tn "Ethernet Off-Enable Wifi" /xml "C:\PCM\Utils\WiFi\Ethernet Off-Enable Wifi.xml" /ru Domain\admin /rp domainpw;
*
I'd like to be able to run this as %computername%\localadmin as that account is on all laptops...
The first line copies a folder with the .xml from a thumb drive to a folder on laptop. Then it creates the tasks. If I am logged into the laptop as admin, the batch runs fine, but as a local user, it fails with the following:
Error Image
Basically, it copies the folder fine, then ERROR: Access is denied.
I'm pretty sure its because the user logged in does not have rights to create the task. Is there a way to have the task run as localadmin and complete?
We run this .bat file to fire off the .ps1 from the folder that gets copied to laptop.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'D:\WifiTask\WifiTask.ps1'"
pause
Sorry if the description of this issue is vague or confusing, just trying to learn as I go....thanks
No, a normal user will not be able to create a system scheduled task without getting prompted for elevation by UAC.
For domain computers, you could try:
Group policy to create the scheduled task
Group policy to run your script as the local system user
Script that connects to computers as Admin user and pushes your xml and wifitask.ps1 like Invoke-Command
Off-domain PCs are more difficult, you would want to do it the same way you currently install other software. Manually run as-admin? Software deployment agent? Remote management tools?

How to deploy .exe to scheduler task using Powershell script

I developed a console app and scheduled it to run on Mon-Friday at 9:AM using windows task scheduler, it works fine.
But I was asked to write Powershell scripts or command-line scripts for deployment, like the script should copy the "Release" folder to the server1(deployment env like dev, test or staging or prod) and schedule it in server1's schedule task. Also I have to set "Start in(optional)".
I don't know anything about command-line and PS scripts. All it should do is create the windows schedule task just like the way I did using UI.
If I understand the question correctly you're just trying to create a scheduled task using powershell?
If so try starting here:
https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-tasks/
I had a requirement to schedule an exe, this scheduled task should run every day Mon to Fri at desired time. I never wrote any batch files in last 15 years of my career :), took it up as it came in my way from my manager. Thanks to him or else I would have not known in this new world.
echo off
title My 1st batch file for Task scheduler
SchTasks /Create /RU "system" /Sc WEEKLY /D MON,TUE,WED,THU,FRI /TN "Leads" /TR "C:/Some.exe" /ST 09:00

Scheduled powershell script run by system does not run in foreground

I am trying to schedule a script to run when I'm not present at my computer. The script has to run with administrator rights AND run in the foreground so that I can see any message or window that is created when I get back to the computer.
The problem is that scheduled system scripts run in the background, this resulting in no visible window or console to show me any results.
here is the command i use to create my scheduled task (using powershell):
SchTasks /Create /SC ONIDLE /I 1 /TN MyScheduledTask /TR "Powershell scheduler.ps1" /RU "System"
All I need is some way to make some (or all if that's easier) of the commands in the script to become visible in the foreground to the user.
Thanks for any feedback.
When running scheduled tasks under the system account, they will run in the background.
You are running the task under System, /RU "System" use /RU with your username and /RP for your user password.
Please also take a look at this question: Powershell Task Scheduler Stuck Running

Windows scheduled tasks - run task as soon as possible after missed schedule

Hopefully a nice simple question but I haven't been able to find the solution online. How do I enable the option Run task as soon as possible after a scheduled start is missed via a command line schtasks /create.
The documentation does not seem to show this option as a command line option and neither does the documentation when using schtasts /create /?.
Over in the technet forums, this recommendation was posted:
As a suggestion, when I've come across missing parameters for this
I've made a task manually with the properties I want, exported to XML
and then I create the task with something like this: schtasks /create
/TN "My New Task Name" /xml "C:\TEMP\My Saved Task.xml" /RU
DOMAIN\username /RP password

How to administer scheduledjobs in powershell 3?

According to documentation, get-scheduledjob only returns scheduled jobs where owner is the current user. Other scheduled job commandlets like set-scheduledjob also only work for scheduled jobs where owner is current user. This makes it impossible for non owners to get job status, modify the job (such as setting other credentials), etc.
In a proper IT organization, I'm going to say its crucial for these jobs to allow adminstration from various administrators.
Am I missing some way to administer, review results, etc (other than looking directly at the powershell output files in the owner's appdata)?
To clarify - I'm looking for a method to work with Powershell created and administered ScheduledJobs. If you modify the scheduled task that executes the scheduled job through the UI, schtask or other scheduled task specific tool, you'll get unexpected results. If you change owner/credentials, the scheduled task will fail. You can use UI/schtasks to change schedule without causing any problems. In addition to changing owner, I want to get at the results of get-job in order to monitor the jobs progress.
The only way I have ever been able to get this to work using Powershell was by invoking the schtask.exe utility:
Note: "/U" is for local administration and "/RU" is for remote administration, also "/S" is not needed with working locally.
Create by importing an XML previously exported from Task Scheduler
[string]$string = 'schtasks.exe /create /RU yourdomain\username /RP $password /TN Task-Name /XML "D:\Path\To\ExportedXML.xml" /S ServerName'
Delete:
[string]$string = 'schtasks.exe /delete /RU yourdomain\username /P $password /TN Task-Name /S ServerName /F'
Query:
[string]$string = 'schtasks.exe /query /RU yourdomain\username /P $password'
Run Locally:
Invoke-Expression -Command $string
Run Remotely:
Invoke-Command -ScriptBlock {$string}
I've written a few PowerShell scripts for managing scheduled tasks (they use the TaskService COM object):
Rename Scheduled Tasks in Windows 7, Windows Server 2008, and Windows Vista
How-To: Use PowerShell to Report on Scheduled Tasks
Updating a Scheduled Task's Credentials
Bill