5

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

Kahn Kah
  • 1,389
  • 7
  • 24
  • 49
  • 1
    From your bullet points at the bottom I use #2 regularly: `-ExecutionPolicy Bypass -File C:\Path\To\File.ps1`. That should work fine for running scheduled tasks. The only thing is that the account might have to be allowed to 'Logon as a batch job' which you can find instructions for [here.](https://www.brooksnet.com/faq/granting-logon-as-batch-privilege) – TheMadTechnician Mar 10 '17 at 17:32
  • It didn't work sadly with the same 0x1 error, I have added a screenshot of the log on as job properties and don't assume there are any problems with it – Kahn Kah Mar 14 '17 at 11:51

3 Answers3

8

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 .

Kahn Kah
  • 1,389
  • 7
  • 24
  • 49
5

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.

scrthq
  • 1,039
  • 11
  • 17
  • Thanks for your answer. I have tried using your argument but it failed and gave the same error as in my question `(0x1)` I don't think there are problems with the enabling of 'logon as batch' however. I have added a screenshot in my question of the settings (asuming i'm the only user/admin) – Kahn Kah Mar 14 '17 at 11:49
  • 1
    This failed because of the " " on either side of the script I used the following as argument: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\_MaintenanceScripts\ProcessPrices.ps1 – AquaAlex Apr 15 '19 at 11:53
5

Call the Powershell.exe in your schedulded task:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Set this Parameters :

-noprofile -executionpolicy unrestricted -noninteractive -file "C:\Path\To\MyScript.ps1"
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
jiten jethva
  • 96
  • 1
  • 5
  • This failed because of the " " on either side of the script I used the following as argument: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\_MaintenanceScripts\ProcessPrices.ps1 – AquaAlex Apr 15 '19 at 11:54