Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Script not running with the task scheduler

So I created a PowerShell script that installs Windows Updates,

As you may know, when installing updates, it's possible that the PC has to reboot (while there are still updates left) in order for installing the scripts.

What I implemented was a task that get's imported at the start of the script and runs the script after each reboot (and installs the remaining script at the logon screen) untill there are no more scripts left.

After the last updates, the script will remove the task out of the task scheduler.

My implementations (downloading/installing/adding the task /removing the task) work perfectly.

It's only that my script won't run after a reboot and I have no idea why!

This is my task: enter image description here

After the pc get's rebooted though, the script doesn't run anymore and the error in the task scheduler states 0x1

I have done some Googling and found that the argument might be wrong (third picture WITH the path being correct of course) but I have tried (in the arguments field):

  • (no quotes) -file path
  • -ExecutionPolicy Bypass -file path
  • -NoProfile -ExecutionPolicy Bypass -file path -force
  • mixing above combinations with quotes added or left out

What could be the problem of the task scheduler not launching my script?

Thanks

EDIT

Added screenshot of Log on as a batch job properties:

enter image description here

like image 419
Kahn Kah Avatar asked Mar 10 '17 16:03

Kahn Kah


People also ask

Can not run script on PowerShell?

To fix this issue, we have to set the execution policy, so that the PowerShell script runs on the particular machine. Here is how: Open PowerShell Console by selecting “Run as Administrator” and get the execution Policy with the command: Get-ExecutionPolicy to get the current policy applied, such as “Restricted”.

How do I enable script running in PowerShell?

To allow PowerShell to run scripts, you need to use the Set-ExecutionPolicy -ExecutionPolicy <PolicyName> command. By default, this command applies your chosen policy to the Local machine scope. If you would like to specify a different scope, you must use the parameter -Scope and provide the name of the scope.

How do I run a PowerShell script silently in Task Scheduler?

Create a shortcut that calls the PowerShell script and set the Run option to Minimized. This will prevent a window from flashing although you will still get a momentary blip of the script running on the Task Bar. Save this answer.


2 Answers

The problem had to do with my START IN -being empty!

My script was at C:\Scripts\Scripts.ps1 so i had to fill in C:\Scripts\.

This is what you need to do apt or else the PS will start in the folder it's in (Windows folder).

Since the script uses other files, i had to specify the START IN path, it was solved after this .

like image 189
Kahn Kah Avatar answered Sep 16 '22 18:09

Kahn Kah


Try this for the argument, replacing it with your actual path (in case your path has spaces):

-ExecutionPolicy Bypass -file "C:\Path to\script.ps1"

From the error, it seems like it's failing to run the script. Other than that, @TheMadTechnician 's comment is accurate. From your pic, it looks like you're scheduling the task as the local admin account though, so ensuring Logon As Batch is enabled shouldn't be required for that account (if it's the true local admin and that account is still in the Administrators local group on the server)

Also to note -- if the scheduled task is trying to remove itself while running, you may have an issue. I haven't tested personally, but you could build a scheduled task that sleeps for a few minutes and try to unregister it while it's running with your tested command to see if that fails.

like image 33
scrthq Avatar answered Sep 18 '22 18:09

scrthq