1

I am attempting to create a scheduled task via powershell, to run another powershell script which will call a number of other scripts on a daily basis.

When running any of these scripts from Powershell directly/manually, there are no issues - each script performs it's function with no issues whatsoever.

However, despite this, when attempting to run the task, I am hitting a brick wall with exit code 0x1.

I have researched this and tried a number of various arguments:

  • Have attempted running with "Powershell" as the program, with the file path to the system folder storing powershell, or simply as PowerShell, or Powershell.exe.
  • Have attempted the above (in every variation) with the -ExecutionPolicy Bypass switch.
  • Have attempted the above with -File "C:\Test\V2\Master.Script.ps1" and combined this with -ExecutionPolicy Bypass switch.
  • Attempted with "-NonInteractive" switch.
  • Running as System
  • Running with Highest Priveleges
  • Running whether user is logged on or not
  • Tested using Domain User account which is Local Admin (On live environment, will have access to local admin or system only)
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
AndyB
  • 15
  • 4
  • 2
    Start by debugging if it's about your script or invoking Powershell in general. Create a simple script that updates a file in c:\temp. Try running that as scheduled task. If it runs, there's something in the main script. If it fails, there's something in the task definition. – vonPryz Nov 30 '18 at 16:50
  • Possible duplicate of [PowerShell script won't execute as a Windows scheduled task](https://stackoverflow.com/questions/13015245/powershell-script-wont-execute-as-a-windows-scheduled-task) –  Nov 30 '18 at 17:18

2 Answers2

1

I have had issues with this in the past as well. What I have found works is by using a BAT File to call the ps1 file. Hopefully this help with your scenario.

BAT File will change execution policy and then run the ps1 file. BAT and ps1 file need to be named the same except the file extension

REG ADD "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /T REG_SZ /V ExecutionPolicy /D Unrestricted /F

Start PowerShell.exe -Command "& '%~dpn0.ps1'"

Here is some code I have used to make a new task to run additional ps1 file.

$Task_Action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-File C:\File.ps1'
$Task_Principal = New-ScheduledTaskPrincipal -UserId SYSTEM -RunLevel Highest
$Task_Settings = New-ScheduledTaskSettingsSet -Hidden
$Task_Trigger = New-ScheduledTaskTrigger -AtStartup

Register-ScheduledTask `
    -TaskName "Your Task Name" `
    -Action $Task_Action `
    -Principal $Task_Principal `
    -Trigger $Task_Trigger `
    -Settings $Task_Settings `
    -Force

On the last script I set the execution policy back with Set-ExecutionPolicy Restricted -Scope LocalMachine

Clayton Lewis
  • 394
  • 2
  • 16
0

I solved this issue using the answer from @briantist in PowerShell script won't execute as a Windows scheduled task, but I wanted to isolate exactly which switch was solving the problem.

It had nothing to do with -ExecutionPolicy, -Noninteractive, -NoLogo, -NoProfile or any other system privilege, user account running the script, etc.

Just needed to add -File in front of the script path in the Task Scheduler > Actions > Arguments field. Without this switch PowerShell was launching and the task history was showing Action Completed, but the script was not executing.

McQuestion
  • 51
  • 1
  • 9