0

I'm trying to run a PowerShell command via Task Scheduler but keep getting the below result when the task tries to run PowerShell on my Windows Server 2008 R2 environment.

action "powershell.exe" with return code 1.

When I run the script in PowerShell manually, the script runs. But for some reason, when I call it from the Task Scheduler it doesn't run.

A screenshot of how my action is set up is attached below.

I've made sure to do the following:

  1. Set ExecutionPolicy to RemoteSigned
  2. Set task to run with highest priveleges
  3. Set task to run whether user logged on or not
  4. Set task configuration for Windows 7, Windows Server 2008 R2

I'm fresh out of ideas.

enter image description here

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Harif87
  • 126
  • 1
  • 10
  • Does the user account the task runs under have the Log on as Batch special permission on this computer? Is the D: drive a mapped drive? – Bacon Bits Aug 05 '18 at 17:10
  • Yes, user has permissions to run batch files, and yes D: is a mapped drive – Harif87 Aug 05 '18 at 17:14
  • 1
    Try specifying the parameters: `-File ".\csv-convert.ps1"` – Maximilian Burszley Aug 05 '18 at 17:26
  • @TheIncorrigible1 thanks for the suggestion. I tried modifying but it didnt help. – Harif87 Aug 05 '18 at 17:33
  • Note - when i run this with the exact same setup on a different machine it works, so this must be an issue with settings as opposed to task setup. – Harif87 Aug 05 '18 at 17:34
  • 3
    As a best practice, I'd try full-pathing your file parameter, then. When you don't use `-File`, it actually interprets it as `-Command` – Maximilian Burszley Aug 05 '18 at 17:34
  • 3
    When you run a task whether the user is logged on or not, the task does not see the mapped drive and the working directory is set to the SYSTEM32 directory. You will have to use UNC paths to any network drives within your script. – Squashman Aug 05 '18 at 19:04
  • 1
    did you checked executionPolicy? try to add `-executionPolicy Bypass` as the first argument, then put `-file fullfilepath` – Avshalom Aug 05 '18 at 19:54
  • [Related](https://stackoverflow.com/a/41635982/1630171). – Ansgar Wiechers Aug 05 '18 at 21:46

2 Answers2

0

Figured this one out thanks to some help from @TheIncorrigible1.

The issue was that the Start In field on the task scheduler was set to a path with a root directory other than C:

To resolve, I left Start In blank and in the Add Arguments field I called the script file with a full file path.

Instead of the below in the Add Arguments field

.\csv-convert.ps1

I referenced the script as follows :

D:\mypath\csv-convert.ps1

Bottom line, dont start powershell in a path with a directory other than C:

Harif87
  • 126
  • 1
  • 10
0

In my case, the issue was that I copied the arguments from another Task Scheduler on Windows Server 2016 to set it up on Windows 2008 R2 - expecting it to work.

In 2016, I had:

-ExecutionPolicy Bypass "C:\Test\SendEmail.ps1"

For 2008 R2 "-File" flag is required:

-ExecutionPolicy Bypass -File "C:\Test\SendEmail.ps1"
user1533274
  • 33
  • 1
  • 5